简单谈谈Resource,Drawable和Bitmap之间的转换,drawablebitmap
投稿于 被查看 44920 次 评论:271
简单谈谈Resource,Drawable和Bitmap之间的转换,drawablebitmap
一直接触这些东西,还是归个类整理一下比较好。
Resource -> Drawable
Drawable draw1 = this.getResources().getDrawable(R.drawable.icon);
Drawable -> Bitmap
1.
static Bitmap drawableToBitmap(Drawable drawable) // drawable 转换成bitmap { int width = drawable.getIntrinsicWidth();// 取drawable的长宽 int height = drawable.getIntrinsicHeight(); Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ?Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565;// 取drawable的颜色格式 Bitmap bitmap = Bitmap.createBitmap(width, height, config);// 建立对应bitmap Canvas canvas = new Canvas(bitmap);// 建立对应bitmap的画布 drawable.setBounds(0, 0, width, height); drawable.draw(canvas);// 把drawable内容画到画布中 return bitmap; }
2.
Bitmap bitmap = ((android.graphics.drawable.BitmapDrawable) pm.getApplicationIcon(appInfo)).getBitmap();
Bitmap转Drawable:
Bitmap bitmap = new Bitmap (...);
Drawable drawable = new BitmapDrawable(bitmap );
这个应该行
Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon);
Bitmap newb = Bitmap.createBitmap( 300, 300, Config.ARGB_8888 );
Canvas canvasTemp = new Canvas( newb );
canvasTemp.drawBitmap(bmp, 50, 50, p);
用户评论