Bitmapdrawable Deprecated Alternative

BitmapDrawable deprecated alternative

Do this instead:

return new BitmapDrawable(context.getResources(), canvasBitmap);

Android - new BitmapDrawable deprecated; alternative Bitmap.createBitmap has to have w/h 0

Ok, instead of

myPopupWindow.setBackgroundDrawable(new BitmapDrawable());

OR

myPopupWindow.setBackgroundDrawable(new BitmapDrawable(
getApplicationContext().getResources(),
Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
));

I just use a background-color (new ColorDrawable(android.graphics.Color color)) that I want to set to the PopupWindow. So for example, one of my PopupWindows just had a few Images without margins in between them, so I used a Transparent background for it:

myPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

And for another one I wanted to use a white background anyway, so I've used:

myPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));

The constructor BitmapDrawable() is deprecated fix

Currently your class has the default constructor MyDrawable() that calls BitmapDrawable(), which is deprecated!

Add a constructor with two arguments (Resources and Bitmap) to your class and call the super constructor:

 BitmapDrawable(context.getResources(), canvasBitmap);

this should fix your problem

BitmapDrawable() method deprecated

popupMessage.setBackgroundDrawable(null) will clear the background.

From the documentation:

Change the background drawable for this popup window. The background
can be set to null.

Custom PopupWindow with BitmapDrawable deprecated method

if want to clean the background, as stated in your comment

//Clear the default translucent background

you can use

popup.setBackgroundDrawable(null);

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

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


Related Topics



Leave a reply



Submit