【问题标题】:How to change progress bar's progress color in Android如何在Android中更改进度条进度颜色
【发布时间】:2011-01-02 12:21:46
【问题描述】:

我在我的 Android 应用程序中使用水平进度条,我想更改它的进度颜色(默认为黄色)。我如何使用code(不是 XML)来做到这一点?

【问题讨论】:

标签: android material-design android-progressbar progress-indicator


【解决方案1】:

对于我使用的水平样式 ProgressBar:

import android.widget.ProgressBar;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ClipDrawable;
import android.view.Gravity;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;

public void setColours(ProgressBar progressBar,
                        int bgCol1, int bgCol2, 
                        int fg1Col1, int fg1Col2, int value1,
                        int fg2Col1, int fg2Col2, int value2)
  {
    //If solid colours are required for an element, then set
    //that elements Col1 param s the same as its Col2 param
    //(eg fg1Col1 == fg1Col2).

    //fgGradDirection and/or bgGradDirection could be parameters
    //if you require other gradient directions eg LEFT_RIGHT.

    GradientDrawable.Orientation fgGradDirection
        = GradientDrawable.Orientation.TOP_BOTTOM;
    GradientDrawable.Orientation bgGradDirection
        = GradientDrawable.Orientation.TOP_BOTTOM;

    //Background
    GradientDrawable bgGradDrawable = new GradientDrawable(
            bgGradDirection, new int[]{bgCol1, bgCol2});
    bgGradDrawable.setShape(GradientDrawable.RECTANGLE);
    bgGradDrawable.setCornerRadius(5);
    ClipDrawable bgclip = new ClipDrawable(
            bgGradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);     
    bgclip.setLevel(10000);

    //SecondaryProgress
    GradientDrawable fg2GradDrawable = new GradientDrawable(
            fgGradDirection, new int[]{fg2Col1, fg2Col2});
    fg2GradDrawable.setShape(GradientDrawable.RECTANGLE);
    fg2GradDrawable.setCornerRadius(5);
    ClipDrawable fg2clip = new ClipDrawable(
            fg2GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);        

    //Progress
    GradientDrawable fg1GradDrawable = new GradientDrawable(
            fgGradDirection, new int[]{fg1Col1, fg1Col2});
    fg1GradDrawable.setShape(GradientDrawable.RECTANGLE);
    fg1GradDrawable.setCornerRadius(5);
    ClipDrawable fg1clip = new ClipDrawable(
            fg1GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);        

    //Setup LayerDrawable and assign to progressBar
    Drawable[] progressDrawables = {bgclip, fg2clip, fg1clip};
    LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);     
    progressLayerDrawable.setId(0, android.R.id.background);
    progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
    progressLayerDrawable.setId(2, android.R.id.progress);

    //Copy the existing ProgressDrawable bounds to the new one.
    Rect bounds = progressBar.getProgressDrawable().getBounds();
    progressBar.setProgressDrawable(progressLayerDrawable);     
    progressBar.getProgressDrawable().setBounds(bounds);

    // setProgress() ignores a change to the same value, so:
    if (value1 == 0)
        progressBar.setProgress(1);
    else
        progressBar.setProgress(0);
    progressBar.setProgress(value1);

    // setSecondaryProgress() ignores a change to the same value, so:
    if (value2 == 0)
        progressBar.setSecondaryProgress(1);
    else
        progressBar.setSecondaryProgress(0);
    progressBar.setSecondaryProgress(value2);

    //now force a redraw
    progressBar.invalidate();
  }

一个示例调用是:

  setColours(myProgressBar, 
          0xff303030,   //bgCol1  grey 
          0xff909090,   //bgCol2  lighter grey 
          0xff0000FF,   //fg1Col1 blue 
          0xffFFFFFF,   //fg1Col2 white
          50,           //value1
          0xffFF0000,   //fg2Col1 red 
          0xffFFFFFF,   //fg2Col2 white
          75);          //value2

如果您不需要“二次进度”,只需将 value2 设置为 value1。

【讨论】:

    【解决方案2】:

    将此自定义样式应用于进度条。

    <style name="customProgress" parent="@android:style/Widget.ProgressBar.Small">
            <item name="android:indeterminateDrawable">@drawable/progress</item>
            <item name="android:duration">40</item>
            <item name="android:animationCache">true</item>
            <item name="android:drawingCacheQuality">low</item>
            <item name="android:persistentDrawingCache">animation</item>
        </style>
    

    @drawable/progress.xml -

    <?xml version="1.0" encoding="utf-8"?>
    <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/spinner_white"
        android:pivotX="50%"
        android:pivotY="50%" />
    

    使用这种类型的图像作为进度条。

    为了获得更好的结果,您可以使用多个进度图像。并且请不要犹豫使用图片,因为 Android 平台本身使用图片作为进度条。 代码是从sdk中提取的:)

    【讨论】:

      【解决方案3】:

      这里是通过编程改变 ProgressBar 颜色的代码。

      ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb_listProgressBar);
      int colorCodeDark = Color.parseColor("#F44336");
      progressBar.setIndeterminateTintList(ColorStateList.valueOf(colorCodeDark));
      

      【讨论】:

        【解决方案4】:
        ProgressBar freeRamPb = findViewById(R.id.free_ram_progress_bar);
        
        freeRamPb.getProgressDrawable().setColorFilter(
        Color.BLUE, android.graphics.PorterDuff.Mode.SRC_IN);
        

        【讨论】:

        • 太棒了。快速有效的解决方案。谢谢!
        【解决方案5】:
        ProgressBar bar;
        
        private Handler progressBarHandler = new Handler();
        
        GradientDrawable progressGradientDrawable = new GradientDrawable(
                GradientDrawable.Orientation.LEFT_RIGHT, new int[]{
                        0xff1e90ff,0xff006ab6,0xff367ba8});
        ClipDrawable progressClipDrawable = new ClipDrawable(
                progressGradientDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
        Drawable[] progressDrawables = {
                new ColorDrawable(0xffffffff),
                progressClipDrawable, progressClipDrawable};
        LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);
        
        
        int status = 0;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        
            // TODO Auto-generated method stub
            setContentView(R.layout.startup);
        
            bar = (ProgressBar) findViewById(R.id.start_page_progressBar);
            bar.setProgress(0);
            bar.setMax(100);
        
            progressLayerDrawable.setId(0, android.R.id.background);
            progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
            progressLayerDrawable.setId(2, android.R.id.progress);
        
            bar.setProgressDrawable(progressLayerDrawable);
        }
        

        这帮助我通过代码为进度条设置自定义颜色。希望对你有帮助

        【讨论】:

          【解决方案6】:

          如果您正在处理多样式应用程序,使用 attr 非常简单:

          试试这个方法:

          在属性attrs.xml下面声明

           <attr name="circularProgressTheme" format="reference"></attr>
          

          将下面的代码粘贴到styles.xml中

           <style name="ProgressThemeWhite" parent="ThemeOverlay.AppCompat.Light">
                  <item name="colorAccent">#FF0000</item>
              </style>
          
              <style name="circularProgressThemeWhite">
                  <item name="android:theme">@style/ProgressThemeWhite</item>
              </style>
          
          
            <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
          
             <item name="circularProgressTheme">@style/circularProgressThemeWhite</item>
          
           </style>
          

          使用如下进度条

            <ProgressBar
                  style="?attr/circularProgressTheme"
                  android:id="@+id/commonProgress"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center"
                  android:visibility="visible"/>
          

          【讨论】:

            【解决方案7】:

            默认情况下(indeterminate

            添加

            android:indeterminateTint="@color/white"
            

            对于确定

            progressBar.getProgressDrawable().setColorFilter(
                Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
            

            【讨论】:

              【解决方案8】:

              Horizontal ProgressBar 使用 rectangle shape drawable 作为背景,ClipDrawablerectangle shape 构造用于进度(主要和次要)。着色将颜色更改为某种色调。 如果您想要分别为所有三种颜色指定目标颜色,那么您可以使用ProgressBar.setProgressDrawable(),如下所示:

                  LayerDrawable progressBarDrawable = new LayerDrawable(
                      new Drawable[]{
                              new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                          new int[]{Color.parseColor("#ff0000ff"),Color.parseColor("#ff0000ff")}),
              
                              new ClipDrawable(
                                          new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                                  new int[]{Color.parseColor("#ff00ff00"),Color.parseColor("#ff00ff00")}),
                                          Gravity.START,
                                          ClipDrawable.HORIZONTAL),
              
                              new ClipDrawable(
                                      new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                              new int[]{Color.parseColor("#ffff0000"),Color.parseColor("#ffff0000")}),
                                      Gravity.START,
                                      ClipDrawable.HORIZONTAL)
                          });
              
                  progressBarDrawable.setId(0,android.R.id.background);
                  progressBarDrawable.setId(1,android.R.id.secondaryProgress);
                  progressBarDrawable.setId(2,android.R.id.progress);
                  ((ProgressBar)findViewById(R.id.progressBar)).setProgressDrawable(progressBarDrawable);
              

              请注意,在初始化LayerDrawable 时,可绘制层的顺序很重要。第一个drawable应该是背景。根据我的实验,切换 ID 不起作用。如果您将填充设置为progressbar,那么这种方法将不起作用。如果您需要填充,则可以为进度条使用容器,例如 LinearLayout

              【讨论】:

                【解决方案9】:

                由于方法public void setColorFilter(@ColorInt int color, @NonNull PorterDuff.Mode mode)已被弃用,我使用这种形式:

                progressBar.indeterminateDrawable.colorFilter =
                            PorterDuffColorFilter(ContextCompat.getColor(this, R.color.black), PorterDuff.Mode.SRC_IN)
                

                【讨论】:

                  【解决方案10】:

                  material design library 的较新版本中,似乎可以将不确定的线性进度指示器设为多色。

                  制作进度条的动画类型contiguous实现了这一点。

                  • 在具有此属性的布局中:
                    app:indeterminateAnimationType
                    
                  • 或以编程方式使用此方法:
                    setIndeterminateAnimationType()
                    

                  更多信息请参考here

                  【讨论】:

                    【解决方案11】:

                    我知道我迟到了,但如果您只想更改进度条的颜色,这是最简单的解决方案,在您的主题中,只需添加,

                    @color/your_color

                    colorSecondary 会在整个过程中改变你的进度颜色。

                    【讨论】:

                      猜你喜欢
                      • 2016-11-02
                      • 2023-03-29
                      • 1970-01-01
                      • 2014-10-08
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多