【问题标题】:ProgressBar dynamically Color AndroidProgressBar 动态上色 Android
【发布时间】:2014-03-16 16:40:51
【问题描述】:

我有一个列表视图,其中填充了从数据库中获取的数据,对于我输入的每条记录,我都输入了一个进度条。从数据库中获取并检查 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();
                                }
                            }
                        }
...
...

【问题讨论】:

  • getPrograssDrawable() 你的 ProgressBar 并用你的自定义 ClipDrawable 替换具有 id == android.id.progress 的图层,该 ClipDrawable 剪辑所需的颜色
  • 对不起,我不明白。能给我举个例子?谢谢
  • 首先获取进度drawable,将其转换为LayerDrawable并用您的自定义ClipDrawable替换id为android.R.id.progress的图层

标签: android listview colors


【解决方案1】:

您可以使用 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 值,但将颜色替换为您自己的颜色。

【讨论】:

    猜你喜欢
    • 2012-11-08
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多