本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术。本文将讲解如何将Drawable转为Bitmap?

问:Rob

我想要设置某个Drawable作为墙纸,但是所有的墙纸功能只支持Bitmap。因为我是pre 2.1系统,所以不能使用WallpaperManager。

而且,我从网上下载的图片在R.drawable中也不能使用,出现这种情况该怎么处理?

答:varun

(最佳答案)

这种方法可以将BitmapDrawable转换成Bitmap

1
2
Drawable
d = ImagesArrayList.get(0); 
Bitmap
bitmap = ((BitmapDrawable)d).getBitmap();

答:Praveen

通常情况下,我觉得下面代码比较有用:

1
2
Bitmap
icon = BitmapFactory.decodeResource(context.getResources(),
                                           R.drawable.icon_resource);

注:下面介绍一种可以下载图片的版本:

1
2
3
4
5
6
7
8
String
name = c.getString(str_url);
URL
url_value =
new

URL(name);
ImageView
profile = (ImageView)v.findViewById(R.id.vdo_icon);
if

(profile !=
null)
{
    Bitmap
mIcon1 =
        BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
    profile.setImageBitmap(mIcon1);
}

答:André

1
2
3
4
5
6
7
8
9
10
11
12
public
static Bitmap drawableToBitmap (Drawable drawable) {
    if

(drawable
instanceof

BitmapDrawable) {
        return

((BitmapDrawable)drawable).getBitmap();
    }
 
    Bitmap
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas
canvas =
new

Canvas(bitmap);
    drawable.setBounds(0,
0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
 
    return

bitmap;
}

答:kabuko

Drawable可以放在Canvas上,而Bitmap支持Canvas:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public
static Bitmap drawableToBitmap (Drawable drawable) {
    if

(drawable
instanceof

BitmapDrawable) {
        return

((BitmapDrawable)drawable).getBitmap();
    }
 
    int
width = drawable.getIntrinsicWidth();
    width
= width > 0 ? width : 1;
    int
height = drawable.getIntrinsicHeight();
    height
= height > 0 ? height : 1;
 
    Bitmap
bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas
canvas =
new

Canvas(bitmap);
    drawable.setBounds(0,
0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
 
    return

bitmap;
}

原文链接:http://stackoverflow.com/questions/3035692/how-to-convert-a-drawable-to-a-bitmap

文章选自StackOverFlow社区,鉴于其内容对于开发者有所帮助,现将文章翻译于此,供大家参考及学习。9Tech将每日持续更新,读者可点击StackOverflow(简称:SOF)精选问答汇总,查看全部译文内容。同时,我们也招募志同道合的技术朋友共同翻译,造福大家!报名请发邮件至zhangqi_wj@cyou-inc.com。

如何将Drawable转为Bitmap?的更多相关文章

  1. Android Drawable、Bitmap、byte[]之间的转换

    转自http://blog.csdn.net/june5253/article/details/7826597 1.Bitmap-->Drawable Bitmap drawable2Bitma ...

  2. Drawable和Bitmap区别

    Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565.RGB888.作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低.我们理解为一种存储对象比较好 ...

  3. 获取应用图标,Drawable 转bitmap

    获取应用图标: PackageManager p = context.getPackageManager(); Drawable draw=null; ApplicationInfo info; tr ...

  4. 简单谈谈Resource,Drawable和Bitmap之间的转换

    一直接触这些东西,还是归个类整理一下比较好. Resource -> Drawable Drawable draw1 = this.getResources().getDrawable(R.dr ...

  5. android 获取资源文件 r.drawable中的图片转换为drawable、bitmap

    1.R-Drawable Resources resources = mContext.getResources(); Drawable drawable = resources.getDrawabl ...

  6. Android -- Drawable与Bitmap测试

    Drawable                                                                                 以下这个是测试加载10 ...

  7. Drawable和Bitmap转换

    一.Bitmap转Drawable Bitmap mBitMap=getYourBitMap(); //getYourBitMap()是你获取BitMap的方法 BitmapDrawable mBit ...

  8. Drawable、Bitmap、byte[]之间的转换

    android在处理一写图片资源的时候,会进行一些类型的转换: 1 Drawable → Bitmap 的简单方法 ((BitmapDrawable)res.getDrawable(R.drawabl ...

  9. Drawable与Bitmap(转)

    Drawable                                                                                 以下这个是测试加载10 ...

随机推荐

  1. 【学习总结】GirlsInAI ML-diary day-1-初识Python-Anaconda-Jupyter

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day1 初识Python-Anaconda-Jupyter: 1-下载并安装Anaconda 官网下载,按指导安装 ana ...

  2. Prometheus & SoundCloud

    Prometheus 系统监控方案 一 - Vovolie - 博客园https://www.cnblogs.com/vovlie/p/Prometheus_CONCEPTS.html Prometh ...

  3. 给input标签添加默认提示文字

    <input name="username" placeholder="请输入用户名" /> placeholder = "提示文字&qu ...

  4. Velocity ${} 和$!{}、!${}区别

    前言 在使用Velocity时经常会看到三种使用变量的情况 情况一:${name} 情况二:$!{name} 情况三:!${name} 那么三者之间到底有什么区别呢?莫慌!!!哈哈 情况一:${nam ...

  5. Day 6-1计算机网络基础&TCP/IP

    按照功能不同,人们将互联网协议分为osi七层或tcp/ip五层或tcp/ip四层(我们只需要掌握tcp/ip五层协议即可) 每层运行常见物理设备: TCP/IP协议: Transmission Con ...

  6. mongodb的安装方法

    下载安装 mongodb官网下载地址:https://www.mongodb.org/downloads#produc...直接下载.msi文件并安装到指定目录即可.我的安装路径是D:\mongodb ...

  7. python之路--线程的其他方法

    一 . current_thread的用法 import threading import time from threading import Thread, current_thread def ...

  8. Java之ArrayList自定义排序,通过实现comparator比较器接口

    两种排序方式: 1.实体类实现Comparable接口,重写compareTo(T o)方法,在其中定义排序规则,那么就可以直接调用Collections.sort()来排序对象数组 2.在调用方法的 ...

  9. postfix 邮箱设置及常见错误

    postfix 邮箱设置及常见错误 1.如果装了sendmail的话,先卸载了. yum remove sendmail 2.安装 Postfix yum install postfix 3.更改默认 ...

  10. codeforces740B

    Alyona and flowers CodeForces - 740B Little Alyona is celebrating Happy Birthday! Her mother has an ...