简体   繁体   English

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

[英]How to use BitmapDrawable's constructor/ Making a temporary load image?

Here it says this 在这里

BitmapDrawable() This constructor was deprecated in API level 4. Use BitmapDrawable(android.content.res.Resources, android.graphics.Bitmap) instead to specify a bitmap to draw with and ensure the correct density is set. BitmapDrawable()在API级别4中不推荐使用此构造方法。请改用BitmapDrawable(android.content.res.Resources,android.graphics.Bitmap)指定要绘制的位图,并确保设置了正确的密度。

I'm trying to set a temporary image Before an image is loaded. 我正在尝试设置一个临时图像,然后再加载图像。 To get to this point I used this tutorial which gave me this function to get a color temporary instead of an image using this function : 为了达到这一点,我使用了教程, 教程为我提供了此功能,以使用该功能获取临时颜色而不是图像:

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();
    }
}

I got to the idea to use the BitmapDrawable by this question If you know another way to do this I will of course be happy to use it but I prefer not to use a lib. 我想到了通过这个问题使用BitmapDrawable的想法,如果您知道另一种方法,我当然会很乐意使用它,但是我更喜欢不使用lib。

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

If you want to show a bitmap instead of a solid colour, extend from 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();
}
}

To use it: 要使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM