简体   繁体   中英

How to programmatically change the value of the android: tint property in <bitmap xml

I'm new here and I need your help. Please tell me how to access the layer-list xml structure from the program level and dynamically change the "tint" color of the bitmap from the program level.

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

Change the tint dynamically using 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM