是否可以在23之前的Android API中创建固定大小的drawable?

时间:2016-06-03 11:30:19

标签: android android-drawable

这是一个可用于无线电按钮的选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@color/ColorPrimaryDark" />
                </shape>
            </item>
            <item>
                <layer-list>
                    <item android:gravity="center_vertical|left" android:left="20dp">
                        <shape android:shape="oval">
                            <size android:width="20dp" android:height="20dp" />
                            <stroke android:color="#ffffff" android:width="2dp" />
                        </shape>
                    </item>
                    <item android:gravity="center_vertical|left" android:left="25dp">
                        <shape android:shape="oval">
                            <size android:width="10dp" android:height="10dp" />
                            <solid android:color="#ffffff" />
                        </shape>
                    </item>
                </layer-list>
            </item>
        </layer-list>
    </item>
    <item android:state_checked="false">
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@color/ColorPrimary" />
                </shape>
            </item>
            <item android:gravity="center_vertical|left"
                android:left="20dp">
                <shape android:shape="oval">
                    <size android:width="20dp" android:height="20dp" />
                    <stroke android:color="#ffffff" android:width="2dp" />
                </shape>
            </item>
        </layer-list>
    </item>
</selector>

在Android API 23中,它看起来如下(应用时):

API 23

在Android API 22中,它看起来如下:

API 22

有没有办法在API pre-23中定义可绘制的固定大小矢量(即非位图)?

1 个答案:

答案 0 :(得分:0)

尝试以下方法以编程方式缩放可绘制对象:

// Read your drawable from somewhere
Drawable dr = getResources().getDrawable(R.drawable.somedrawable);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
// Scale it to 100 x 100
Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 100, 100, true));
// Set your new, scaled drawable "d"