Drawable -作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变、图形等。
1、Drawable → Bitmap 的简单方法
Drawable d = XXX;
BitmapDrawable
Bitmap
可简化为:((BitmapDrawable)res.getDrawable(R.drawable.youricon)).getBitmap();
Java代码
public static Bitmap drawableToBitmap(Drawable drawable){
bitmap= Bitmap
!= PixelFormat.OPAQUE ?Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
canvas= new Canvas(bitmap);
drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
}
3.Bitmap→Drawable
BitmapDrawable bitmapDrawable= (BitmapDrawable)bitmap;
Drawable drawable = (Drawable)bitmapDrawable;
Bitmap bitmap = newBitmap (...);
Drawable drawable = new BitmapDrawable(bitmap);
3、从资源中获取Bitmap
Java代码
Bitmap bmp=BitmapFactory.decodeResource(getResources(),R.drawable.pic);
4、Bitmap → Byte[]
Java代码
private byte[] Bitmap2Bytes(Bitmapbm){
baos =new ByteArrayOutputStream();
100, baos);
}
5、 byte[] → Bitmap
Java代码
private Bitmap Bytes2Bimap(byte[]b){
0,b.length);