Android SetTint不再起作用。

3

我有一个来自RatingBar android - custom draw runtime的代码。

之前它能够改变我的评分控件上图形的颜色:

              vthf.rating.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                  if (event.getAction() == MotionEvent.ACTION_UP) {
                     //--
                     float touchPositionX = event.getX();
                     float width = vthf.rating.getWidth();
                     float starsf = (touchPositionX / width);
                     starsf = starsf * param_final__data.score_max;
                     int starsint = (int) Math.round(starsf);
                     byte starsbyte = (byte) starsint;
                     param_final__data.score_cur = starsbyte;
                     starsf = starsint;
                     vthf.rating.setRating(starsf);
                     //--
                     int color = Color.BLACK;
                     switch (starsbyte % 4) {
                       case 0: color = Color.BLUE; break; // 4 stars
                       case 3: color = Color.GREEN; break;
                       case 2: color = Color.YELLOW; break;
                       case 1: color = Color.RED; break;
                     }
                     final LayerDrawable layerDrawable = (LayerDrawable) vthf.rating.getProgressDrawable();
                     Drawable myWrap = layerDrawable.getDrawable(2);
                     //--
                     //
                     //--
                     myWrap = DrawableCompat.wrap(myWrap);
                     //myWrap = myWrap.mutate();
                     //--
                     // myWrapMutate.setColorFilter(color, PorterDuff.Mode.SRC_IN);
                     // DrawableCompat.setTintList(myWrap, ColorStateList.valueOf(color));
                     //--
                     DrawableCompat.setTint(myWrap, color);
                     //--
                     vthf.rating.invalidate();
                  }
                  else
                  if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    param_final__view.setPressed(true);
                  }
                  else
                  if (event.getAction() == MotionEvent.ACTION_CANCEL) {
                    param_final__view.setPressed(false);
                  }
                  return true;
                }
              });

我相信这个问题是在我升级到Android Studio 1.5.1和所有支持库(不确定版本)后停止工作的 - 我不是100%确定,因为我不确定我是否立即发现了问题或者它已经存在更久了。

我正在华为P6 Android 4.4.2上测试。

我的gradle文件看起来像这样:

compileSdkVersion 23
buildToolsVersion '23'

dependencies {
    compile 'com.android.support:support-v4:+'
    compile 'com.google.android.gms:play-services:+'
    compile 'com.android.support:appcompat-v7:+'
}

我的清单里有这个

<uses-sdk
  android:minSdkVersion="9"
  android:targetSdkVersion="17" 
/>

这是我最好的线索,可以解释为什么代码不再起作用 - 如果有明显的原因导致此问题与API版本无关,我也愿意接受各种建议。
---注---
此代码似乎会使图形略微变暗。
                     final LayerDrawable layerDrawable = (LayerDrawable) vthf.rating.getProgressDrawable();
                     Drawable myWrap = layerDrawable.getDrawable(2);
                     myWrap = DrawableCompat.wrap(myWrap);
                     DrawableCompat.setTint(myWrap, color);

这段代码没有任何作用:

                     final LayerDrawable layerDrawable = (LayerDrawable) vthf.rating.getProgressDrawable();
                     Drawable myWrap = layerDrawable.getDrawable(2);
                     DrawableCompat.setTint(myWrap, color);

对于上述内容,我不确定该如何解释。也许问题是由于我不理解的其他原因引起的。如果图形变暗,人们可能会认为它正在工作...但是,就我眼睛能看到的而言,无论颜色如何,都是完全相同的变暗。


请查看此链接:https://dev59.com/CF0Z5IYBdhLWcg3w8kP3#30876871 - Krishna
我相信我已经尝试了所有的方法(wrap、settintlist、settint、invalidateself),或者是我漏掉了什么? - Tom
@Krishna,你是在建议像这样做吗“layerDrawable.setDrawable(2,myWrap);”?它会崩溃(而且之前也不需要)。 - Tom
我已经补充了一些更多的信息来扩展这个问题。 - Tom
1
停止使用:+版本标识符作为Gradle依赖项。新版本会破坏构建,正如在这种情况下所发生的那样。 - S.D.
3个回答

5

我使用类似这样的代码来设置颜色。它可能也适用于此处,所有从View继承的对象都应该能够做到这一点。此示例使用ContextCompat来获取颜色,但还有其他方法可以获得所需的颜色。

View view = (findViewById(R.id.text_view_location));
view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.wantedColor));

4

在AppCompat 23.3版本中有些问题破坏了我之前的解决方案。不过,只要避免使用DrawableCompat#Wrap,你就可以让你的评分条恢复正常工作:

ratingBarB.setOnTouchListener(new View.OnTouchListener() {
    private int lastColoredProgress = 0;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int progress = ratingBarB.getProgress();
        if (progress != lastColoredProgress) {
            lastColoredProgress = progress;
            int color;
            switch (lastColoredProgress) {
                case 0:
                case 1:
                    color = Color.RED;
                    break;
                case 2:
                case 3:
                    color = Color.YELLOW;
                    break;
                case 4:
                default:
                    color = Color.GREEN;
                    break;
            }
            final Drawable drawable = ratingBarB.getProgressDrawable();
            if (drawable instanceof LayerDrawable) { // Lollipop and newer
                final LayerDrawable layerDrawable = (LayerDrawable) drawable;
                DrawableCompat.setTint(layerDrawable.getDrawable(2), color);
            } else {
                DrawableCompat.setTint(drawable, color);
            }
        }
        return false;
    }
});


3
当你使用DrawableCompat包装你的Drawable时,会创建一个新的DrawableWrapper。你需要将新的Drawable设置给你的组件。
在你的代码中,你需要添加类似于下面的内容(在setTint之后):
layerDrawable.setCompoundDrawablesWithIntrinsicBounds(myWrap, null, null, null);

我不确定如何修复vthf.rating.getProgressDrawable()中第二个索引的可绘制对象,因为当我尝试使用layerDrawable.setDrawable(2,myWrap)时它会崩溃。 - Tom
此外,Android Studio 无法解决 layerDrawable.setCompoundDrawablesWithIntrinsicBounds ... - Tom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接