从位图创建可绘制对象

14

大家好,

根据这里的建议,我需要将我的位图转换成可绘制对象。但当我尝试使用以下代码时:

 Drawable d = new Drawable( my_bmp);

它显示这个构造函数已经被弃用,建议使用:

 Drawable(Bitmap bmp, int resourceId)

我怎样才能将位图制作成可绘制的图形?

谢谢。


你能试试这个代码吗?Drawable d=new BitmapDrawable(res, bmp); - M D
@SimplePlan,正如我在原帖中所写的那样,位图来自云端,而不是资源。 - Igor
然后,您需要从云端下载该图像,然后作为Drawable加载。 - M D
@SimplePlan,是的,该图像已作为位图加载。问题在于将其转换为可绘制对象。你能提供一些代码吗? - Igor
让我们在聊天室里继续这个讨论 - M D
显示剩余2条评论
6个回答

26

您可以使用

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

BitmapDrawable是一个包装了位图的Drawable对象,可以进行平铺、拉伸或对齐操作。你可以从文件路径、输入流、XML解析或已有的Bitmap对象创建BitmapDrawable。

BitmapDrawable(Resources res, Bitmap bitmap)

根据资源的显示度量标准,创建一个从位图生成的drawable,并设置初始目标密度。

还可以查看公共构造函数 @

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


你的回答告诉我你没有看原帖。;-) 我不是从资源中加载位图。我从云端获取照片作为位图。因此,我无法在该代码中使用资源引用。还有其他想法吗? - Igor
@Igor,你在帖子中从未提到过这一点。这不是我的错。 - Raghunandan
我提供了一个链接到我的原始问题,其中提到了这个要求。就像我说的那样 - 你没有看。;-) - Igor
@Igor,你本可以在你的帖子中提到这一点。同时,发布外部链接也不好,因为很少有人会查看它们 :) - Raghunandan
@Igor 你有没有查看我发布的链接?首先下载位图,然后使用BitmapDrawable(Resources res, String filepath) filepath是位图的路径。 - Raghunandan

3

尝试以下方法将 Bitmap 加载为 Bitmap Drawable

创建如下的 ImgDrawableFromFile(Resources res, String file_name)

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;

    }

现在调用此函数的方法如下:
img.setBackgroundDrawable(ImgDrawableFromFile(getResources(), "1.jpg")); // pass your saved file name as second argument 

3
有点晚了但我看到一个明显的误解。提供的答案确实是正确的:
Drawable d = new BitmapDrawable(getResources(),bitmap);

在这种情况下,getResources()ContextActivity中可用的方法,仅用于查找屏幕密度并相应地调整可绘制对象。
因此,即使从云端加载图像,您仍然可以调用getResources()
编程愉快!

2

BitmapDrawable(Bitmap bitmap) 构造函数已被 弃用

你可以使用

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

源代码


@Igor 从云端获取位图:Bitmap bitmap = BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream()); - Kinka

0
如果Mauro Banze来晚了,那么我真不知道我还在这里干什么。但是针对那些绝望寻求解决方案的人,这就是我做的事情:
当我将一个JavaFX项目转移到Android时,我遇到了类似的问题。 我想重用大部分“数据”类,这些类只管理和提供我的数据,并且不影响UI。
故事告诉够了,我们开始吧:
问题:
- 某些时候,我们需要Drawable来在屏幕上显示位图。 - 我们想要下载(从云端、互联网或其他地方)这个位图并将其保存以供稍后使用作为Drawable。 - 我们无法在非Context或非Activity类中执行此操作,因为我们需要Resources。
那么,为什么不将其保存为Bitmap,直到我们需要在Context或Activity类中绘制它为止呢?
我创建了一个名为BitmapImage的类。 它看起来像这样:
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());
}

这个非常简单的类可以“保存”位图。因此,你不必使用Drawable,而是使用BitmapImage,直到你真正需要Drawable

在那时,你应该在ActivityContext中调用Drawable foo = anyBitmapImage.getDrawable(getResource())


0

您可以使用 BitmapDrawable 类从位图中获取 Drawable

Bitmap Drawable 参考资料


抱歉,我不能这样做。我没有使用资源,位图(照片)来自云端。 - Igor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接