`
whotodo
  • 浏览: 167007 次
文章分类
社区版块
存档分类
最新评论

android:Bitmap和Drawable相互转换方法

 
阅读更多

很多开发者表示,不知道Android的Drawable和Bitmap之间如何相关转换。

一、Bitmap转Drawable

Bitmap bm=xxx; //xxx根据你的情况获取
BitmapDrawable bd=BitmapDrawable(bm);

BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。

二、 Drawable转Bitmap

转成Bitmap对象后,可以将Drawable对象通过Android的SK库存成一个字节输出流,最终还可以保存成为jpg和png的文件。

//通过openRawResource获取一个InputStream对象
InputStream input = getResources().openRawResource(R.drawable.bitmap);
//通过InputStream创建BitmapDrawable对象

BitmapDrawable pic = new BitmapDrawable(input);
//通过BitmapDrawable对象获取Bitmap对象
Bitmap bitmap = pic.getBitmap();

最终bm就是我们需要的Bitmap对象了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics