【问题标题】:ProgressDrawable setColorFilter doesn't work in Nexus 5ProgressDrawable setColorFilter 在 Nexus 5 中不起作用
【发布时间】:2016-01-31 18:40:15
【问题描述】:

我有一个RatingBar

<RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleX="0.75"
        android:isIndicator="false"
        android:scaleY="0.75"
        android:id="@+id/ratingBar"
        android:stepSize="0.5"
        android:numStars="5" />

我正在使用滤色器使评分栏的星星变成粉红色,如下所示:

    ratingBar = (RatingBar) findViewById(R.id.ratingBar);
    Drawable progressDrawable = ratingBar.getProgressDrawable();
    if (progressDrawable instanceof  LayerDrawable) {
        LayerDrawable stars = (LayerDrawable) progressDrawable;
        stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.ColorSecondary), PorterDuff.Mode.SRC_ATOP);
        stars.getDrawable(1).setColorFilter(getResources().getColor(R.color.ColorSecondary), PorterDuff.Mode.SRC_ATOP);
        stars.getDrawable(0).setColorFilter(getResources().getColor(R.color.ColorSecondary), PorterDuff.Mode.SRC_ATOP);
    }

这适用于所有手机,除了 Nexus 5(Android 版本 6.0),其中 5 颗星都是粉红色的,但默认填充。即使我点击星星,它们也不会改变颜色,所有 5 颗星星都保持填充状态。

但是,当我执行ratingBar.getRating() 时,它会返回用户触摸评分栏的评分,这意味着它正在工作,只是滤色器出现故障。

如果我移除颜色过滤器,RatingBar 可以正常使用默认颜色。

似乎无法在任何地方找到解决方案。提前致谢。

【问题讨论】:

    标签: android colorfilter layerdrawable


    【解决方案1】:

    在 android 6.0 中使用下面的一个而不是 getProgressDrawable()

    LayerDrawable ldRating = (LayerDrawable) ratingBar.getProgressDrawable();
    ldRating.getDrawable(0).setColorFilter(getResources().getColor(R.color.rate_empty_color), PorterDuff.Mode.SRC_ATOP);
    ldRating.getDrawable(1).setColorFilter(getResources().getColor(R.color.rate_half_color), PorterDuff.Mode.SRC_ATOP);
    ldRating.getDrawable(2).setColorFilter(getResources().getColor(R.color.rate_full_color), PorterDuff.Mode.SRC_ATOP);
    

    【讨论】:

      【解决方案2】:

      很抱歉,这不是答案,但是是什么推动了从代码中设置它的要求?如果定义正确, .setProgressDrawable 应该可以工作

      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      
      <item android:id="@android:id/background">
          <shape>
              <corners android:radius="5dip" />
              <gradient
                      android:startColor="#ff9d9e9d"
                      android:centerColor="#ff5a5d5a"
                      android:centerY="0.75"
                      android:endColor="#ff747674"
                      android:angle="270"
              />
          </shape>
      </item>
      
      <item android:id="@android:id/secondaryProgress">
          <clip>
              <shape>
                  <corners android:radius="5dip" />
                  <gradient
                          android:startColor="#80ffd300"
                          android:centerColor="#80ffb600"
                          android:centerY="0.75"
                          android:endColor="#a0ffcb00"
                          android:angle="270"
                  />
              </shape>
          </clip>
      </item>
      
      <item android:id="@android:id/progress">
          <clip>
              <shape>
                  <corners
                      android:radius="5dip" />
                  <gradient
                      android:startColor="@color/progress_start"
                      android:endColor="@color/progress_end"
                      android:angle="270" 
                  />
              </shape>
          </clip>
      </item>
      
      </layer-list>
      

      如果需要有关进度条颜色过滤器的更多详细信息,请查看此链接:http://ambracode.com/index/show/14297

      【讨论】:

      • 这不是我想要的,但由于它是手机特定的东西,我不知道如何自己解决。我已经切换到 XML。谢谢! :)
      猜你喜欢
      • 1970-01-01
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      • 2015-11-29
      • 1970-01-01
      • 2013-09-10
      相关资源
      最近更新 更多