工具类(bitmap与drawable)

//bitmap转换drawable
public Drawable bitmapToDrawable(Bitmap bitmap) {
  BitmapDrawable bd = new BitmapDrawable(getResources(),bitmap);
  return bd;
}
//drawable转换成 bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
  // 取 drawable 的长宽
  int w = drawable.getIntrinsicWidth();
  int h = drawable.getIntrinsicHeight();

  // 取 drawable 的颜色格式
  Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
      : Bitmap.Config.RGB_565;
  // 建立对应 bitmap
  Bitmap bitmap = Bitmap.createBitmap(w, h, config);
  // 建立对应 bitmap 的画布
  Canvas canvas = new Canvas(bitmap);
  drawable.setBounds(0, 0, w, h);
  // 把 drawable 内容画到画布中
  drawable.draw(canvas);
  return bitmap;
}

猜你喜欢

转载自blog.csdn.net/weixin_37160260/article/details/80110968