//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; }
工具类(bitmap与drawable)
最新推荐文章于 2022-02-10 21:34:22 发布