着色ImageView无法在Android 5.0上运行.想法如何让它再次运作?

前端之家收集整理的这篇文章主要介绍了着色ImageView无法在Android 5.0上运行.想法如何让它再次运作?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我构建的应用程序中,我注意到 ImageViews没有在运行新 Android Lollipop的设备上着色.这是以前在旧版操作系统上正常工作的代码
  1. <ImageView
  2. android:layout_width="40dp"
  3. android:layout_height="40dp"
  4. android:layout_gravity="bottom|right"
  5. android:contentDescription="@string/descr_background_image"
  6. android:src="@drawable/circle_shape_white_color"
  7. android:tint="@color/intent_circle_green_grey" />

这是在ImageView中加载的drawable:

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

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

解决方法

根据@alanv评论,这里有针对这个bug的hacky修复.基本思路是扩展ImageView并在通胀后立即应用ColorFilter:
  1. public class TintImageView extends ImageView {
  2.  
  3. public TintImageView(Context context,AttributeSet attrs) {
  4. super(context,attrs);
  5.  
  6. initView();
  7. }
  8.  
  9. private void initView() {
  10. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  11. ColorStateList imageTintList = getImageTintList();
  12. if (imageTintList == null) {
  13. return;
  14. }
  15.  
  16. setColorFilter(imageTintList.getDefaultColor(),PorterDuff.Mode.SRC_IN);
  17. }
  18. }
  19. }

正如你可能猜到的那样,这个例子有些限制(在通胀色调不会更新之后的Drawable设置,只使用ColorStateList的默认颜色,也许还有别的东西),但如果你有了这个想法,你可以根据自己的需要使用它 – 案件.

猜你在找的Android相关文章