Home » Android » android – Resize drawable in layer-list item

android – Resize drawable in layer-list item

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">

            <solid android:color="#000000"/>
            <size android:width="70dp" android:height="70dp"/>
        </shape>
    </item>

    <item android:drawable="@drawable/ic_person_white_48dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:layout_width="10dp"
        android:layout_height="10dp"
        />
</layer-list>

How can I resize the drawable image? Currently, it’s too big for the shape.

How to&Answers:

I would suggest using A ScaleDrawable. It essentially changes the size of the child drawable using

android:scaleHeight="50%"
android:scaleWidth="50%"

Both are expressed as a percentage of the drawable’s current bound.

Answer:

Maybe it’s and old question, but this is how I resize a drawable in a layer-list

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list>
            <item>
                <shape android:shape="oval">
                    <solid android:color="#000000"/>
                    <size android:width="10dp" android:height="10dp"/>
                    <padding android:bottom="30dp" android:top="30dp"
                             android:left="30dp" android:right="30dp"/>
                </shape>
            </item>
            <item>
                <bitmap android:src="@drawable/ic_person_white_48dp"/>
            </item>
        </layer-list>
    </item>
</selector>

No need for android:height or android:width

Answer:

Drawable specifies how image to be drawn on the canvas. You tell the drawable how big it should be, and it scales or clips the underlying image to the size. You can setBounds on a drawable or resize the ImageView.

The following code snippet draws the drawable to a bitmap with the desired size.

Drawable yourDrawable = getResources().getDrawable(R.drawable.yourDrawable);
yourDrawable.setBonnds(0, 0, width, height);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
yourDrawable.draw(canvas);

Answer:

You could try something like this:

<item
    android:width="10dp"
    android:height="10dp"
    android:drawable="@drawable/ic_person_white_48dp"
    android:bottom="30dp"
    android:top="30dp"
    android:left="30dp"
    android:right="30dp"
    android:gravity="center" />