【问题标题】:Is it possible to create fixed-size drawables in Android API pre-23?是否可以在 Android API pre-23 中创建固定大小的可绘制对象?
【发布时间】:2016-06-03 11:30:19
【问题描述】:

这是一个可供单选按钮绘制的选择器:

<?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 中(应用时)如下所示:

不过,在 Android API 22 中,它如下所示:

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

【问题讨论】:

    标签: android android-drawable


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 2012-07-27
      • 2010-11-19
      • 1970-01-01
      • 2012-02-20
      相关资源
      最近更新 更多