ProgressBar动态着色Android

ProgressBar动态着色Android,android,listview,colors,Android,Listview,Colors,我有一个列表视图,其中填充了从数据库中获取的数据,对于我输入的每个记录,都有一个progreesbar。从数据库获取并检查TextView背景的颜色,我想将其分配给ProgressBar。我该怎么办?在getView()方法中,我添加了一个ProgressBar @Override public View getView(int position, View convertView, ViewGroup parent) { View row = super.getView

我有一个列表视图,其中填充了从数据库中获取的数据,对于我输入的每个记录,都有一个progreesbar。从数据库获取并检查TextView背景的颜色,我想将其分配给ProgressBar。我该怎么办?在getView()方法中,我添加了一个ProgressBar

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
        View row = super.getView(position, convertView, parent);
        tvC.setBackgroundColor(d.colore);//I would also assign color to the ProgressBar
 final ProgressBar mProgress;
             mProgress = (ProgressBar)row.findViewById(R.id.progress_e);
             mProgress.setMax(100);
             final float numero_float = (float) value;
                // run progress
                    new Thread(new Runnable() {
                         int progressStatus = 0;

                            Handler handler = new Handler();
                        public void run() {
                            while (progressStatus < numero_float) {
                                progressStatus += 1;
                                // Update the progress bar and display the current value 
                                handler.post(new Runnable() {
                                    public void run() {
                                        mProgress.setProgress(progressStatus);

                                    }
                                });
                                try {
                                    Thread.sleep(40);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
...
...
@覆盖
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=super.getView(位置、转换视图、父级);
tvC.setBackgroundColor(d.colore);//我还将为ProgressBar指定颜色
最终进度计划;
mProgress=(ProgressBar)row.findViewById(R.id.progress_e);
m程序设置最大值(100);
最终浮点数浮点数=(浮点数)值;
//运行进度
新线程(newrunnable()){
int progressStatus=0;
Handler=newhandler();
公开募捐{
同时(进度状态<数字浮动){
progressStatus+=1;
//更新进度条并显示当前值
handler.post(新的Runnable(){
公开募捐{
mpprogress.setProgress(progressStatus);
}
});
试一试{
睡眠(40);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
...
...

您可以使用ColorMatrixColorFilter动态更改进度条的颜色

    ProgressBar progressBar = (ProgressBar) viewFindById(...);
    int color = Color.RED;  // for example..but use your own color value here
    float[] progressMatrix = {
            0, 0, 0, 0, Color.red(color), //red
            0, 0, 0, 0, Color.blue(color), //blue
            0, 0, 0, 0, Color.green(color), //green
            0, 0, 0, 1, 0  //alpha
    };
    progressMaskColorFilter = new ColorMatrixColorFilter(new ColorMatrix(progressMatrix));
    progressBar.getProgressDrawable().setColorFilter(progressMaskColorFilter);
如果还想给seekbar的拇指上色,请执行上述操作,然后执行以下操作:

    progressBar.getThumb().setColorFilter(scrubberMaskColorFilter);

ColorMatrix将保留图像中的alpha值,但将颜色替换为您自己的颜色。

getPrograssDrawable()用id==android.id.progress替换你的自定义ClipDrawable(剪辑所需的颜色)。对不起,我不明白。你能给我一个例子吗?首先感谢设置progress drawable,将其转换为LayerDrawable并用id android.R.id.progress替换你的自定义ClipDrawable