如何动态更新SeekBar的颜色?

时间:2010-02-23 02:36:34

标签: android

我希望进度条的颜色(特别是使用setSecondaryProgress更新的辅助进度)在运行时超过某一点时动态更改。

我已经能够在我的Activity的onCreate函数(将颜色从黄色变为绿色)中通过调用setProgressDrawable并替换我从默认Android版本的水平进度中复制的资源(SDK)替换我自己的drawable到栏中/platforms/android-2.1/data/res/drawable/progress_horizo​​ntal.xml)。但是,在运行时更新它(是的,从UI线程通过AsyncTask)会导致整个进度条除了thumbtab之外变黑或透明。

3 个答案:

答案 0 :(得分:2)

我认为这就是你要找的东西

enter image description here

enter image description here

enter image description here

这可以通过为背景和seak bar的进度创建自定义drwable来实现 有关示例代码,请参阅此link

请参阅以下链接 http://javatechig.com/android/android-seekbar-example/

Change the color of a seekbar on onProgressChanged

答案 1 :(得分:1)

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
  );
}
}

答案 2 :(得分:-1)

SeekBar是ProgressBar的扩展,它添加了一个可拖动的拇指。用户可以触摸拇指并向左或向右拖动以设置当前进度级别或使用箭头键。

当SeekBarChange时,将调用OnSeekBarChangeListener的onProgressChanged(),并根据值(SeekBar.getProgress())更新屏幕的背景(LinearLayout)。

main.xml中