【问题标题】:Tinting ImageView not working on Android 5.0. Ideas how to make it work again?着色 ImageView 在 Android 5.0 上不起作用。想法如何使它再次工作?
【发布时间】:2015-01-26 07:12:42
【问题描述】:

在我构建的一个应用程序中,我注意到 ImageViews 在运行新的 Android Lollipop 的设备上没有着色。这是过去在旧版本操作系统上正常工作的代码:

<ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="bottom|right"
            android:contentDescription="@string/descr_background_image"
            android:src="@drawable/circle_shape_white_color"
            android:tint="@color/intent_circle_green_grey" />

这是加载到 ImageView 中的可绘制对象:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <gradient android:startColor="@color/white" android:endColor="@color/white"
        android:angle="270"/>
</shape>

再一次,这在运行 JellyBean/Kitkat 的设备上正常工作,但色调对运行 Lollipop 的设备没有影响。任何想法如何解决它?这是操作系统中的错误,还是我应该开始对图像进行不同的着色?

【问题讨论】:

  • 操作系统错误已在未来版本中修复。 ImageView 的 tint 属性已更新为使用 Drawable.setTint() 而不是 Drawable.setColorFilter(),但 GradientDrawable 不支持 setTint()。 XML 并没有很好的解决方法,但您可以从代码中调用 setColorFilter()。
  • 太棒了,很高兴知道您已经处理好了!谢谢你的回答:)

标签: android imageview android-imageview android-5.0-lollipop


【解决方案1】:

像这样使用AppCompatImageView

<android.support.v7.widget.AppCompatImageView
        android:id="@+id/my_appcompat_imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_image"
        android:tint="#636363"
    />

确保您在应用的build.gradle 中有最新的compile 'com.android.support:appcompat-v7:23.4.0'

【讨论】:

    【解决方案2】:

    根据@alanv 的评论,这里是对这个错误的 hacky 修复。基本思想是扩展ImageView并在膨胀后立即应用ColorFilter

    public class TintImageView extends ImageView {
    
        public TintImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            initView();
        }
    
        private void initView() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                ColorStateList imageTintList = getImageTintList();
                if (imageTintList == null) {
                    return;
                }
    
                setColorFilter(imageTintList.getDefaultColor(), PorterDuff.Mode.SRC_IN);
            }
        }
    }
    

    正如您可能猜到的那样,这个例子有些有限(Drawable 在膨胀后设置不会更新,只使用默认颜色ColorStateList,也许还有别的),但如果你明白了可以适合您的用例。

    【讨论】:

      【解决方案3】:

      这段代码在 android lollipop 中对我有用

      ImageViewCompat.setImageTintList(imageView,ColorStateList.valueOf(Color.parseColor(chartTable.getReport().getButtonColor())));
      

      【讨论】:

        【解决方案4】:

        有时在 xml 中可绘制编码颜色和字母数字。您需要更改或删除它。像这样:

        <vector xmlns:android="http://schemas.android.com/apk/res/android"
            android:width="24dp"
            android:height="24dp"
            android:viewportWidth="24"
            android:viewportHeight="24"
            android:tint="#FFFFFF"
            android:alpha="0.8">
          <path
              android:fillColor="@android:color/white"
              android:pathData="M16,1L8,1C6.34,1 5,2.34 5,4v16c0,1.66 1.34,3 3,3h8c1.66,0 3,-1.34 3,-3L19,4c0,-1.66 -1.34,-3 -3,-3zM14,21h-4v-1h4v1zM17.25,18L6.75,18L6.75,4h10.5v14z"/>
        </vector>
        

        到:

        <vector xmlns:android="http://schemas.android.com/apk/res/android"
            android:width="24dp"
            android:height="24dp"
            android:viewportWidth="24"
            android:viewportHeight="24"
            android:tint="@android:color/white">
          <path
              android:fillColor="@android:color/white"
              android:pathData="M16,1L8,1C6.34,1 5,2.34 5,4v16c0,1.66 1.34,3 3,3h8c1.66,0 3,-1.34 3,-3L19,4c0,-1.66 -1.34,-3 -3,-3zM14,21h-4v-1h4v1zM17.25,18L6.75,18L6.75,4h10.5v14z"/>
        </vector>
        

        只需读取 xml 图像并删除 alpha 或更改尖齿的颜色。如果你需要。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多