ProgressBar动态Color Android

时间:2014-03-16 16:40:52

标签: android listview colors

我有一个Listview填充了从数据库中获取的数据,对于我输入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();
                                }
                            }
                        }
...
...

1 个答案:

答案 0 :(得分:0)

您可以使用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);

如果您想为搜索栏的拇指着色,请执行以上操作,然后按照以下步骤操作:

    progressBar.getThumb().setColorFilter(scrubberMaskColorFilter);

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