在网上搜了一些其它的方法,在这里分享下
1.Drawable—>Bitmap
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.sample_0);
2.Bitmap---->Drawable
Drawable drawable =new BitmapDrawable(bmp);
其他方法
1、Drawable → Bitmap
public static Bitmap drawableToBitmap(Drawable drawable)
{
Config config;
if (drawable.getOpacity() != PixelFormat.OPAQUE)
{
config = Bitmap.Config.ARGB_8888;
}
else
{
config = Bitmap.Config.RGB_565;
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
rawable.getIntrinsicHeight(),config);
Canvas canvas = new Canvas(bitmap);
// canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
2、从资源中获取Bitmap
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
3、Bitmap → byte[]
private byte[] Bitmap2Bytes(Bitmap bm)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
4、 byte[] → Bitmap
private Bitmap Bytes2Bimap(byte[] b)
{
if (b.length != 0)
{
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else
{
return null;
}
}
A R G B
透明度 红色 绿色 蓝色
Bitmap.Config ARGB_4444 每个像素 占四位
Bitmap.Config ARGB_8888 32 每个像素 占八位
Bitmap.Config RGB_565 16 R占5位 R 占6位 B占5位 没有透明度(A)
其实很简单嘛就跟double float 一样 你越到精度也就越到
但是一点主意点 一半情况下我们都是用argb888 但是无可厚非 它也相对的很占内存
因为一个像素32位 8位一个字节 如果是800*480的图片的话自己算 估计有1M多了
所以各位使用的时候悠着点 因为作为应用开发人员你使用的内存是有限的
分享到:
相关推荐
详细的介绍了android_Drawable、Bitmap、byte[]之间的转换 在android开发中非常实用
Android Drawable、Bitmap、byte、灰度 转换
Drawable Bitmap InputStream byte[]相互转化工具类,单例模式
android Bitmap用法总结 Bitmap用法总结 1、Drawable → Bitmap public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable....
Android Drawable、Bitmap、byte、灰度 之间的转换
文档中描述了Drawable Bitmap 几种转换方法供初学者使用。方法都是经过实践验证的。
一个在VC中将Bitmap转换为Byte[]的小例子。
[Logo] (https://github.com/jainakhil88/android-commons/blob/master/sample/Android8/res/drawable-mdpi/ic_launcher.png) Android Commons Android Commons aims to provide quick, easy and ready to use set ...
将图片由bitmap类型转换为stream类型再转换为byte[]类型,然后再还原
// []{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f,R.drawable.g,R.drawable.h,R.drawable.i,R.drawable.j},true); // mAnimation = new Animation(mContext,new int // []{...
Drawable Bitmap之间的转化Drawable Bitmap之间的转化
将Bitmap转成byte[],进行保存,用到时再取出byte[],转化成Bitmap,进行显示
android 画图 bitmap drawable canvas paint
很多开发者表示,不知道Android的Drawable和Bitmap之间如何相关转换。下面Android123给大家两种比较简单高效的方法。 一、Bitmap转Drawable 代码如下: Bitmap bm=xxx; //xxx根据你的情况获取 BitmapDrawable bd...
Android Drawable和Bitmap的转换实例详解 通常我们需要通过代码去设置图片,就需要设置图片Bitmap和Drawable的转换,下面整理了几种方式 一、Bitmap转Drawable Bitmap bm=xxx; //xxx根据你的情况获取 ...
本文实例讲述了Android中Bitmap用法。分享给大家供大家参考,具体如下: 一般在android程序中把图片...import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import an
android bitmap通过byte数组以流的形式创建,自己加了进度条,看起来稍微好点
// 将Drawable,转成Bitmap bg_bitmap = ((BitmapDrawable) oldbg).getBitmap(); Bitmap blur_bitmap = com.example.administrator.text2.util.EasyBlur.with(MainActivity.this) .bitmap(bg_bitmap) //要模糊的...
软件开发网在此之前给大家介绍过图片加载框架Glide的基本用法介绍,大家可以先参考一下,本篇内容更加深入的分析了Glide获取图片Path、Bitmap用法,以及实现的代码分析。 1. 获取Bitmap: 1)在图片下载缓存好之后...
我就废话不多说了,大家还是直接看代码吧~ //Uri.parse(file://+result.getImage().getCompressPath())) String path=uri.... Bitmap bitmap= BitmapFactory.decodeFile(path); int height= bitmap.get