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

Geo*_*nov 9 android imageview android-imageview android-5.0-lollipop

在我构建的应用程序中,我注意到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" />
Run Code Online (Sandbox Code Playgroud)

这是在ImageView中加载的drawable:

<?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>
Run Code Online (Sandbox Code Playgroud)

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

Dmi*_*sev 9

根据@alanv评论,这里有针对这个bug的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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

正如你可能猜到的那样,这个例子有些限制(Drawable在通胀色调不会更新之后设置,只使用默认颜色ColorStateList,也许还有别的东西),但是如果你有了这个想法,你就可以适应你的用例了.


Ali*_*nia 5

这样使用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"
    />
Run Code Online (Sandbox Code Playgroud)

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