一、Drawable 转换成 Bitmap

方法一

通过 BitmapFactory 中的 decodeResource 方法,将资源文件中的 R.drawable.ic_drawable 转化成Bitmap

Resources res = getResources();Bitmap    bmp = BitmapFactory.decodeResource(res, R.drawable.ic_drawable);

方法二

将 Drable 对象先转化成 BitmapDrawable ,然后调用 getBitmap 方法 获取

Resource res      = gerResource();Drawable drawable = res.getDrawable(R.drawable.ic_drawable);//获取drawableBitmapDrawable bd = (BitmapDrawable) drawable;Bitmap bm         = bd.getBitmap();

方法三

根据已有的Drawable创建一个新的Bitmap

    public static Bitmap drawableToBitmap(Drawable drawable) {        int w = drawable.getIntrinsicWidth();        int h = drawable.getIntrinsicHeight();        System.out.println("Drawable转Bitmap");        Bitmap.Config config =                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888                        : Bitmap.Config.RGB_565;        Bitmap bitmap = Bitmap.createBitmap(w, h, config);        //注意,下面三行代码要用到,否则在View或者SurfaceView里的canvas.drawBitmap会看不到图        Canvas canvas = new Canvas(bitmap);        drawable.setBounds(0, 0, w, h);        drawable.draw(canvas);        return bitmap;    }

二、Bitmap 转换成 Drawable

使用 BitmapDrawable 对 Bitmap 进行强制转换

Drawable drawable = new BitmapDrawable(bmp);

三、Bitmap 转换成 byte[]

public static byte[] getBytes(Bitmap bitmap){        ByteArrayOutputStream baos = new ByteArrayOutputStream();        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);        return baos.toByteArray();       }

四、byte[] 转化成 Bitmap

    public static Bitmap Bytes2Bimap(byte[] b) {        if (b.length != 0) {            return BitmapFactory.decodeByteArray(b, 0, b.length);        } else {            return null;        }    }

更多相关文章

  1. android安卓手机分屏多窗口实现方法
  2. android 关于listview item设置高度的问题解决方法
  3. Day03
  4. android 上面js java数据互传
  5. Android的常用方法(转载)
  6. Android(安卓)编译系统 --- 版本信息
  7. @BindView问题 Attempt to invoke virtual method 'void android
  8. Android(安卓)Vibrate
  9. Android(安卓)Binder 机制初步学习 笔记(三)—— Binder 进程通讯

随机推荐

  1. Android 设置动画变化的速率
  2. [置顶] Android客户端性能优化(魅族资深工
  3. 关于Android StatusBar 通顶问题的讨论和
  4. 探索Android 滑动置顶标题的实现
  5. Android 高级编程读书笔记 视图View定制
  6. [置顶] ADT版本号超过17后,所遇到的问题,如
  7. android 仿ios数字密码解锁界面的实例
  8. 安卓源码框架学习-framework层
  9. Android学习 之 应用程序基础及四大组件
  10. Android多线程问题