【问题标题】:How to get a Bitmap from a drawable defined in a xml?如何从 xml 中定义的可绘制对象中获取位图?
【发布时间】:2017-07-11 21:58:25
【问题描述】:

如何从可绘制的 xml 形状中获取位图。 我做错了什么?

shadow.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="270.0"
        android:endColor="@android:color/transparent"
        android:startColor="#33000000"
        android:type="linear" />

    <size android:height="7.0dip" />

</shape>

我从drawable中检索位图的方法:

private Bitmap getBitmap(int id) {
    return BitmapFactory.decodeResource(getContext().getResources(), id);
}
当传入的 id 是 shadow.xml 可绘制 id 时,

getBitmap() 返回 null。

【问题讨论】:

    标签: android


    【解决方案1】:

    这是一个完全有效的解决方案

    private Bitmap getBitmap(int drawableRes) {
        Drawable drawable = getResources().getDrawable(drawableRes);
        Canvas canvas = new Canvas();
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        canvas.setBitmap(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
    
        return bitmap;
    }
    

    这是一个例子:

    Bitmap drawableBitmap = getBitmap(R.drawable.circle_shape);
    

    circle_shape.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
        <size
            android:width="15dp"
            android:height="15dp" />
        <solid
            android:color="#94f5b6" />
        <stroke
            android:width="2dp"
            android:color="#487b5a"/>
    </shape>
    

    【讨论】:

    • 抛出 java.lang.IllegalArgumentException: width and height must be > 0
    • 只是一个小的兼容性更新:更改“getResources().getDrawable(drawableRes);” by "ContextCompat.getDrawable(context, drawableRes);"
    • 如果您想按照@Tobliug 的建议使用ContextCompat.getDrawable(context, drawableRes);,则必须添加support-v4 library developer.android.com/topic/libraries/support-library/…
    • @BMU 如果您希望像这样使用它,您需要在可绘制的形状中定义大小。默认情况下,可绘制形状没有定义大小。所以添加类似 之类的东西,你应该很高兴。
    【解决方案2】:

    ShapeDrawable 没有与之关联的位图 - 它的唯一目的是在画布上绘制。在调用它的 draw 方法之前,它没有图像。如果您可以在需要绘制阴影的地方获得画布元素,则可以将其绘制为 shapeDrawable,否则您可能需要在布局中以阴影作为背景单独的空视图。

    【讨论】:

    • 感谢您的精彩解释。我直接在画布上画了,效果很好。
    • @kaneda 你能展示最终工作的代码吗?我在这里面临同样的问题。非常感谢。
    • 这类似于Drawable d = getContext().getResources().getDrawable(R.drawable.id); d.draw(canvas);
    • @JFreebird 对不起。我没有代码了,所以我现在能做的最好的就是猜测一下。如果您可以发布一个描述您的问题的问题,以便其他人可以看到,IMO 最好。如果您想这样做,请在 cmets 中将您的问题链接到此处,以便我查看。也许我可以有一个见解:)
    【解决方案3】:

    你应该为你的 shape drawable 添加 size 属性以防止出现“java.lang.IllegalArgumentException: width and height must be > 0”。

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="oval">
           <solid android:color="@color/colorAccent" />
           <stroke
               android:width="1.3dp"
               android:color="@color/white" />
    
           <size android:height="24dp" android:width="24dp"/>
    </shape>
    

    【讨论】:

      猜你喜欢
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 2014-10-08
      相关资源
      最近更新 更多