这里有新鲜出炉的精品教程,程序狗速度看过来!
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
这篇文章主要介绍了 Android Drawable 和 Bitmap 的转换实例详解的相关资料, 需要的朋友可以参考下
Android Drawable 和 Bitmap 的转换实例详解
通常我们需要通过代码去设置图片,就需要设置图片 Bitmap 和 Drawable 的转换,下面整理了几种方式
一、Bitmap 转 Drawable
- Bitmap bm = xxx; //xxx根据你的情况获取
- BitmapDrawable bd = new BitmapDrawable(bm); //因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
二、 Drawable 转 Bitmap
- Drawable d=xxx; //xxx根据自己的情况获取drawable
- BitmapDrawable bd = (BitmapDrawable) d;
- Bitmap bm = bd.getBitmap();
- //最终bm就是我们需要的Bitmap对象了。
从资源中获取 Bitmap
- public static Bitmap getBitmapFromResources(Activity act, int resId) {
- Resources res = act.getResources();
- return BitmapFactory.decodeResource(res, resId);
- }
byte[] → Bitmap
- public static Bitmap convertBytes2Bimap(byte[] b) {
- if (b.length == 0) {
- return null;
- }
- return BitmapFactory.decodeByteArray(b, 0, b.length);
- }
// Bitmap → byte[]
- public static byte[] convertBitmap2Bytes(Bitmap bm) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
- return baos.toByteArray();
- }
只是很简单代码片段,还是很容易懂得
来源: http://www.phperz.com/article/17/0605/335205.html