相关文章推荐
谦和的手电筒  ·  flink ...·  5 月前    · 
果断的汽水  ·  react-redux Hook API ...·  11 月前    · 
小眼睛的金鱼  ·  MFC combo box 下拉框 ...·  1 年前    · 
豁达的上铺  ·  c++ - Windows: Handle ...·  1 年前    · 

BitmapFactory.decodeResource()对xml drawable中定义的形状返回null

55 人关注

我翻阅了多个类似的问题,尽管我还没有找到合适的答案。

我有一个drawable,定义在shape.xml中

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="@color/bg_color" />
</shape>

我想把它转换为Bitmap对象,以便执行一些操作,但BitmapFactory.decodeResource()返回null。

This is how I'm doing it:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.shape);

我做错了什么?替换代码1】是否适用于xml定义的drawables?

android
android-drawable
xml-drawable
mol
mol
发布于 2014-06-24
5 个回答
Philipp Jahoda
Philipp Jahoda
发布于 2019-07-24
已采纳
0 人赞同

因为你想加载一个 Drawable ,而不是一个 Bitmap ,所以使用这个。

Drawable d = getResources().getDrawable(R.drawable.your_drawable, your_app_theme);

要把它变成一个Bitmap

public static Bitmap drawableToBitmap (Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable)drawable).getBitmap();
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap); 
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;

摘自。如何将Drawable转换为位图?

我想知道这怎么能行得通......因为drawable是在XML中定义的形状, getIntrinsicWidth() getIntrinsicHeight() 将总是返回-1,并且位图不会被创建。还是我搞错了什么?
This throws IllegalArgumentException: width and height must be > 0
这意味着你的drawable没有一个有效的尺寸。
用新的支持库更新:使用ContextCompat.GetDrawable(context, id)。
Bharat singh
Bharat singh
发布于 2019-07-24
0 人赞同

安卓KTX 现在有一个扩展函数,用于将可画图转换为位图

val bitmap = ContextCompat.getDrawable(context, R.drawable.ic_user_location_pin)?.toBitmap()
if (bitmap != null) {
    markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
    
Ali Bagheri
Ali Bagheri
发布于 2019-07-24
0 人赞同
public static Bitmap convertDrawableResToBitmap(@DrawableRes int drawableId, Integer width, Integer height) {
    Drawable d = getResources().getDrawable(drawableId);
    if (d instanceof BitmapDrawable) {
        return ((BitmapDrawable) d).getBitmap();
    if (d instanceof GradientDrawable) {
        GradientDrawable g = (GradientDrawable) d;
        int w = d.getIntrinsicWidth() > 0 ? d.getIntrinsicWidth() : width;
        int h = d.getIntrinsicHeight() > 0 ? d.getIntrinsicHeight() : height;
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        g.setBounds(0, 0, w, h);
        g.setStroke(1, Color.BLACK);
        g.setFilterBitmap(true);
        g.draw(canvas);
        return bitmap;
    Bitmap bit = BitmapFactory.decodeResource(getResources(), drawableId);
    return bit.copy(Bitmap.Config.ARGB_8888, true);
//------------------------
Bitmap b = convertDrawableResToBitmap(R.drawable.myDraw , 50, 50);
    
在你的答案中加入一些解释
Blackbelt
Blackbelt
发布于 2019-07-24
0 人赞同