开发者

How can you dynamically update the color of a SeekBar?

开发者 https://www.devze.com 2022-12-21 10:14 出处:网络
I would like to have the color of the progress bar (specifically the secondary progress that is updated with setSecondaryProgress) to change dynamically at runtime when it crosses a certain point.

I would like to have the color of the progress bar (specifically the secondary progress that is updated with setSecondaryProgress) to change dynamically at runtime when it crosses a certain point.

I am already able to substitute my own drawable into the bar during my Activity's onCreate function (changing color from yellow to green) by calling setProgressDrawable and substituting a resource I've copied from the default Android version of a horizontal progress (SDK/platforms/android-2.1/data/res/drawable/progress_horizontal.xml). However, updating this at runti开发者_C百科me (yes, from the UI thread via an AsyncTask) causes the entire progress bar except for the thumbtab to go black or transparent.


I think this is what you are looking for

How can you dynamically update the color of a SeekBar?

How can you dynamically update the color of a SeekBar?

How can you dynamically update the color of a SeekBar?

This can be achived by creating custom drwables for background and progress of seak bar See this link for sample code.

Pls see the below links also http://javatechig.com/android/android-seekbar-example/

Change the color of a seekbar on onProgressChanged


SeekColorActivity.java

package com.exercise.seekcolor;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.SeekBar;

public class SeekColorActivity extends Activity {

private int seekR, seekG, seekB;
SeekBar redSeekBar, greenSeekBar, blueSeekBar;
LinearLayout mScreen;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mScreen = (LinearLayout) findViewById(R.id.myScreen);
    redSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_R);
    greenSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_G);
    blueSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_B);
    updateBackground();

    redSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
    greenSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
    blueSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);

}

private SeekBar.OnSeekBarChangeListener seekBarChangeListener
= new SeekBar.OnSeekBarChangeListener()
{

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
  boolean fromUser) {
// TODO Auto-generated method stub
 updateBackground();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
};

private void updateBackground()
{
 seekR = redSeekBar.getProgress();
 seekG = greenSeekBar.getProgress();
 seekB = blueSeekBar.getProgress();
 mScreen.setBackgroundColor(
  0xff000000
  + seekR * 0x10000
  + seekG * 0x100
  + seekB
  );
}
}


A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys.

When SeekBarChange, the onProgressChanged() of OnSeekBarChangeListener will be called, and the background of the screen(LinearLayout) will be updated according to the value (SeekBar.getProgress()).

main.xml

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号