【问题标题】:Android SetTint no longer workingAndroid SetTint 不再工作
【发布时间】:2016-05-16 10:48:15
【问题描述】:

我有这个来自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);

不知道如何理解上述内容。也许问题是由我不明白的其他原因引起的。如果图形变暗,人们会认为它有点工作......但无论他是什么颜色,它都是完全相同的变暗 - 至少据我的眼睛可以看出。

【问题讨论】:

  • 我相信我已经尝试了所有这些(wrap、settintlist、settint、invalidateself)还是我错过了什么?
  • @Krishna 你是否建议做这样的事情“ layerDrawable.setDrawable(2,myWrap);” ?它崩溃了(之前没有必要)
  • 我已经用更多信息扩展了这个问题
  • 停止使用:+ gradle 依赖的版本标识符。新版本会破坏构建,就像在本例中那样。

标签: android drawable


【解决方案1】:

我使用这样的东西来设置颜色。也可以在这里工作,从 View 继承的所有对象都应该能够做到这一点。本案例使用 ContextCompat 来获取颜色,但是还有比这更多的方法来获取你想要的颜色。

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

【讨论】:

    【解决方案2】:

    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。您需要为组件设置新的可绘制对象。

      在你的代码中,你需要像这样添加一些想法(就在 setTint 之后):

      layerDrawable.setCompoundDrawablesWithIntrinsicBounds(myWrap, null, null, null);
      

      【讨论】:

      • 我不确定如何修复 vthf.rating.getProgressDrawable() 中的第二个可绘制索引;因为当我尝试 layerDrawable.setDrawable(2,myWrap); 时它崩溃了
      • 另外Android Studio无法解析layerDrawable.setCompoundDrawablesWithIntrinsicBounds ...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 2013-03-16
      • 2012-04-11
      相关资源
      最近更新 更多