Create drawable from bitmap

有些话、适合烂在心里 提交于 2019-12-23 07:02:12

问题


ALL,

As suggested here I need to make a drawable out of my bitmap. But when I tried to use:

 Drawable d = new Drawable( my_bmp);

it shows that this constructor is deprecated in favour of:

 Drawable(Bitmap bmp, int resourceId)

How else I can make a drawable out of bitmap?

Thank you.


回答1:


You can use

Drawable d = new BitmapDrawable(getResources(), my_bmp);

A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a BitmapDrawable from a file path, an input stream, through XML inflation, or from a Bitmap object.

BitmapDrawable(Resources res, Bitmap bitmap)

Create drawable from a bitmap, setting initial target density based on the display metrics of the resources.

Also look a the public constructors @

http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html




回答2:


try this to load Bitmap as Bitmap Drawable

Create ImgDrawableFromFile(Resources res, String file_name) like below

Drawable d = null;

    public Drawable ImgDrawableFromFile(Resources res, String file_name) {

        myBitmap = null;
        File imgFile = new File(
                "/data/data/yourpkgname/app_my_sub_dir/images/" + file_name);
        if (imgFile.exists()) {

            myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

            if (myBitmap != null) {
                d = new BitmapDrawable(res, myBitmap);
                return d;
            } else {
                return null;
            }
        }
        return null;

    }

Now called this function like

img.setBackgroundDrawable(ImgDrawableFromFile(getResources(), "1.jpg")); // pass your saved file name as second argument 



回答3:


A little late to the party but I see a clear misunderstanding. The answers provided are indeed correct:

Drawable d = new BitmapDrawable(getResources(),bitmap);

In this case getResources() is a method available in the Context or Activity and is only used to find out the screen density and adjust the Drawable accordingly.

So Igor, even if you are loading the image from the cloud, you can still call getResources().

Happy coding




回答4:


BitmapDrawable(Bitmap bitmap) constructor has been deprecated

you can use

Drawable drawable = new BitmapDrawable(getResources(), bitmap);

source




回答5:


you can use the BitmapDrawable class to get a Drawable from a bitmap

Bitmap Drawable reference




回答6:


If Mauro Banze was late to the party then I don't really know what I'm still doing here. But for the ones that come here desperately looking for a solution, here is what I did:

I stumbled over somewhat the same issue when bringing a Project from JavaFX to Android. I wanted to reuse most of my "data" classes which only manage and provide my data and have no affect on the UI.

Enough story telling, here we go:

Problem:

  • At some point we need a Drawableto get out Bitmap on the screen.
  • We want to download (from the cloud, the internet, or wherever) this Bitmap and save it for later usage as a Drawable
  • We can't do that in a non-Context or non-Activity class, as we need the Resources.

So why don't we just save it as a Bitmap until we need it to be drawn (which certainly will happen in a Context or Activity class).

I have created a class called BitmapImage. It looks like this:

public class BitmapImage {

private final Bitmap bitmap;

public BitmapImage (Bitmap bitmap) {
    this.bitmap = bitmap;
}


public Bitmap getBitmap() {
    return bitmap;
}

public Drawable getDrawable(Resources res) {
    return new BitmapDrawable(res,getBitmap());
}

This very simple class "saves" the Bitmap. Thus, rather then working with a Drawable you work with the BitmapImage until you really desperately need the Drawable.

At that point, you should be in your Activity or Context and there you can call Drawable foo = anyBitmapImage.getDrawable(getResource()).



来源:https://stackoverflow.com/questions/23074946/create-drawable-from-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!