在layer-list项中调整可绘制对象的大小

15

我有这个:

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

我该如何调整drawable图像大小?目前它对应的形状太大了。


1
你的方法对你有效,但我仍然遇到了问题。 - Lion789
4个回答

5
我建议使用ScaleDrawable。它可以通过改变子绘制对象的大小来实现缩放。
android:scaleHeight="50%"
android:scaleWidth="50%"

两者都以可绘制对象当前边界的百分比表示。

2
也许这是一个老问题,但这是我如何在图层列表中调整可绘制对象大小的方法。
<?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>

无需使用 android:height 或 android:width。

0

Drawable 指定图像在画布上的绘制方式。您可以告诉 Drawable 它应该有多大,它会将基础图像缩放或裁剪到所需大小。您可以在 Drawable 上 setBounds 或调整 ImageView 的大小。

以下代码片段将可绘制对象绘制到具有所需大小的位图上。

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

-3
你可以尝试这样做:
<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" />

1
android:widthandroid:height仅适用于API 23及以上版本。如何才能在不同设备上计算出android:topandroid:bottom的值呢? - ericn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接