如何使用BitmapDrawable的构造函数/制作临时加载图像?



这里说的是这个

位图可绘制() 此构造函数在 API 级别 4 中已弃用。改用 BitmapDrawable(android.content.res.Resources, android.graphics.Bitmap) 来指定要绘制的位图,并确保设置正确的密度。

我正在尝试在加载图像之前设置临时映像。为了达到这一点,我使用了本教程,它给了我这个函数来临时获取颜色而不是使用此函数的图像:

static class DownloadedDrawable extends ColorDrawable {
    private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference;
    public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) {
        super(Color.BLACK);
        bitmapDownloaderTaskReference =
            new WeakReference<BitmapDownloaderTask>(bitmapDownloaderTask);
    }
    public BitmapDownloaderTask getBitmapDownloaderTask() {
        return bitmapDownloaderTaskReference.get();
    }
}

我有了通过这个问题使用BitmapDrawable的想法如果您知道另一种方法,我当然很乐意使用它,但我不喜欢使用库。

如果要

显示位图而不是纯色,请从 BitmapDrawable 扩展:

static class DownloadedDrawable extends BitmapDrawable {
private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference;
public DownloadedDrawable(Resources resources, Bitmap bitmap, BitmapDownloaderTask bitmapDownloaderTask) {
    super(resources, bitmap);
    bitmapDownloaderTaskReference =
        new WeakReference<BitmapDownloaderTask>(bitmapDownloaderTask);
}
public BitmapDownloaderTask getBitmapDownloaderTask() {
    return bitmapDownloaderTaskReference.get();
}
}

要使用它:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.your_drawable);
DownloadedDrawable draw = new DownloadedDrawable(getResources(), bitmap, yourBitmapDownloaderTask);
yourImageView.setImageDrawable(draw);

尝试使用new BitmapDrawable(context.getResources(), canvasBitmap);

最新更新