Tinting ImageView not working on Android 5.0. Ideas how to make it work again?

后端 未结 3 2387
情书的邮戳
情书的邮戳 2021-02-19 22:15

In an application I\'ve built I noticed that the ImageViews are not tinted on devices running the new Android Lollipop. This is the code that used to work correctly on older ver

相关标签:
3条回答
  • 2021-02-19 22:29

    Use the AppCompatImageView like so:

    <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"
        />
    

    Make sure you have the latest compile 'com.android.support:appcompat-v7:23.4.0' in your app's build.gradle.

    0 讨论(0)
  • 2021-02-19 22:51

    this code work for me in android lollipop

    ImageViewCompat.setImageTintList(imageView,ColorStateList.valueOf(Color.parseColor(chartTable.getReport().getButtonColor())));
    
    0 讨论(0)
  • 2021-02-19 22:56

    As per @alanv comment, here goes the hacky fix to this bug. Basic idea is to extend ImageView and apply ColorFilter right after inflation:

    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);
            }
        }
    }
    

    As you might guess, this example is somewhat limited (Drawable set after inflation tint won't be updated, only default color of ColorStateList is used, and maybe something else), but if you got the idea you can fit it to your use-case.

    0 讨论(0)
提交回复
热议问题