除了使用 BitmapDrawable 之外,是否还有其他方法可以从 XML 获取位图

Are there any alternatives to getting a bitmap from XML other than using BitmapDrawable

我目前在我的应用程序中使用 BitmapDrawable,但不幸的是,此实现在 Lollipop 中似乎有些损坏(在 LP 之前它工作正常,关于该问题有一个单独的问题)。

所以,好的,我有一个 XML 文件,基本上是这样的:

backgrounds.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/backgrounds_xlarge" />

上面是一个 XML 文件(别名),它指向一个实际的 png 文件。

在我的代码中,我得到的位图是这样的:

代码

BitmapDrawable bd = (BitmapDrawable) view.getResources().getDrawable(R.drawable.backgrounds);
backgrounds = bd.getBitmap();

这个问题是想问有没有其他方法可以得到这个位图?请注意 BitmapFactory 对我没有好处,因为我 必须 能够通过上面显示的 xml 别名获得此位图。

我已经阅读了 Offical docs on creating XML Aliases,但不幸的是,它们在解释如何访问位图方面有些欠缺。

感谢任何帮助或建议。

这是使用 CanvasBitmapDrawable 获取 Bitmap 的另一种方法:

    BitmapDrawable bd = (BitmapDrawable) view.getResources().getDrawable(R.drawable.backgrounds);
    Bitmap backgrounds = Bitmap.createBitmap(bd.getIntrinsicWidth(), bd.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(backgrounds); 
    bd.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    bd.draw(canvas);