【问题标题】:DrawableCompat.unwrap is not working pre LollipopDrawableCompat.unwrap 在 Lollipop 之前不工作
【发布时间】:2015-06-19 19:02:32
【问题描述】:

我正在使用 DrawableCompat.wrap 为 pre Lollipop 中的可绘制对象设置色调,它工作正常。 DrawableCompat.unwrap 在 Lollipop 之前不起作用。 我无法在着色之前获得原始可绘制对象。

例如:

 if (v.isSelected()){
                Drawable normalDrawable = getResources().getDrawable(R.drawable.sample);
                Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);
                DrawableCompat.setTint(wrapDrawable, getResources().getColor(R.color.sample_color));
                imageButton.setImageDrawable(wrapDrawable);
 }else{
                Drawable normalDrawable = imageButton.getDrawable();
                Drawable unwrapDrawable = DrawableCompat.unwrap(normalDrawable);
                imageButton.setImageDrawable(unwrapDrawable);
 }

在棒棒糖之前的设备中,DrawableCompact.unwrap 返回带有色调而不是原始颜色的可绘制对象

【问题讨论】:

  • 有这方面的消息吗?你找到解决办法了吗?
  • @chrisonline 没什么新鲜的。由于这种行为,我们没有使用此功能,这很不幸,因为它是一个强大的工具。

标签: android android-5.0-lollipop drawable android-support-library tint


【解决方案1】:

如果您想清除色调,请致电DrawableCompat.setTintList(drawable, null)

Unwrap 不是破坏性功能,它只是让您可以访问原始的 Drawable。

以下是示例代码

Drawable drawable = (Drawable) ContextCompat.getDrawable(getContext(), R.drawable.google_image);
if (condition) {
    drawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(drawable, ContextCompat.getColor(getContext(), R.color.grey700));
    DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SCREEN);
    mImageView.setImageDrawable(drawable);
} else {
    drawable = DrawableCompat.unwrap(drawable);
    DrawableCompat.setTintList(drawable, null);
    mLoginStatusGoogleImageView.setImageDrawable(drawable);
}

你的情况代码应该是:

if (v.isSelected()) {
    Drawable normalDrawable = getResources().getDrawable(R.drawable.sample);
    Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);
    DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(getContext(), R.color.sample_color));
    DrawableCompat.setTint(wrapDrawable, getResources().getColor(R.color.sample_color));
    imageButton.setImageDrawable(wrapDrawable);
} else {
    Drawable normalDrawable = imageButton.getDrawable();
    Drawable unwrapDrawable = DrawableCompat.unwrap(normalDrawable);
    DrawableCompat.setTintList(unwrapDrawable, null);
    imageButton.setImageDrawable(unwrapDrawable);
}

【讨论】:

    猜你喜欢
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    相关资源
    最近更新 更多