Android 如何从xml中定义的drawable获取位图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10111073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a Bitmap from a drawable defined in a xml?
提问by kaneda
How can I get the bitmap from a xml shape drawable. What am I doing wrong?
如何从可绘制的 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>
My method to retrieve the bitmap from drawable:
我从drawable中检索位图的方法:
private Bitmap getBitmap(int id) {
return BitmapFactory.decodeResource(getContext().getResources(), id);
}
getBitmap() is returning null when the id passed in is the shadow.xmldrawable id.
当传入的 id 是shadow.xml可绘制 id时,getBitmap() 返回 null 。
采纳答案by JRaymond
a ShapeDrawable doesn't have a bitmap associated with it - its sole purpose is to be drawn on a canvas. Until its draw method is called, it has no image. If you can get a canvas element at the place where you need to draw the shadow, you can draw it as a shapeDrawable, otherwise you might need a separate, empty view in your layout with the shadow as a background.
ShapeDrawable 没有与之关联的位图 - 它的唯一目的是在画布上绘制。在调用它的 draw 方法之前,它没有图像。如果你能在需要绘制阴影的地方得到一个canvas元素,你可以将它绘制为shapeDrawable,否则你可能需要在你的布局中一个单独的空视图,以阴影为背景。
回答by vovahost
This is a fully working solution:
这是一个完全有效的解决方案:
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;
}
And here is an example:
这是一个例子:
Bitmap drawableBitmap = getBitmap(R.drawable.circle_shape);
circle_shape.xml
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>
回答by Selcan
You should add size attribute to your shape drawable for preventing "java.lang.IllegalArgumentException: width and height must be > 0".
您应该向可绘制的形状添加 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>