【问题标题】:How to programmatically change the value of the android: tint property in <bitmap xml如何以编程方式更改android的值:<bitmap xml中的tint属性
【发布时间】:2021-05-07 19:42:42
【问题描述】:
我是新来的,需要你的帮助。请告诉我如何从程序级别访问 layer-list xml 结构并从程序级别动态更改位图的“色调”颜色。
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/bg_peper" android:right="65dp">
<bitmap
android:gravity="top|left"
android:tint="@color/red"
android:src="@drawable/ic_favorite" />
</item>
<item....
</layer-list>
【问题讨论】:
标签:
java
android
bitmap
layer-list
【解决方案1】:
使用 this 动态更改色调
ImageView view = (ImageView)findViewById(R.id.tintLayerView); // ImageView where you have added the drawable as src
LayerDrawable layerDrawable = (LayerDrawable)view.getDrawable();
Drawable bpPeperDrawable = layerDrawable.findDrawableByLayerId(R.id.bg_peper);
DrawableCompat.setTint(bpPeperDrawable.mutate(), ContextCompat.getColor(this, R.color.red)); // Use the required color here