一、相关概念 1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象 2、Canvas画布,绘图的目的区域,用于绘图 3、Bitmap位图,用于图的处理 4、Matrix矩阵 二、Bitmap 1、从资源中获取Bitmap
1
Resources res = getResources();
2
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);
[] Bitmap2Bytes(Bitmap bm) {
2
ByteArrayOutputStream baos = new ByteArrayOutputStream();
3
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
4
return baos.toByteArray();
5
}
(b.length != 0) {
;
6
}
7
}
4、Bitmap缩放
Bitmap zoomBitmap(Bitmap bitmap, h = bitmap.getHeight();
scaleHeight = ((float) height / h);
7
matrix.postScale(scaleWidth, scaleHeight);
8
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
9
return newbmp;
10
}
5、将Drawable转化为Bitmap
Bitmap drawableToBitmap(Drawable drawable) {
w = drawable.getIntrinsicWidth();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
Canvas canvas = drawable.draw(canvas);
16
return bitmap;
17
}
6、获得圆角图片
Bitmap getRoundedCornerBitmap(Bitmap bitmap, h = bitmap.getHeight();
color = 0xff424242;
Rect rect = output;
18
}
7、获得带倒影的图片
Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
reflectionGap = 4;
h = bitmap.getHeight();
paint.setXfermode(canvas.drawRect(0, h, w, bitmapWithReflection.getHeight()
31
+ reflectionGap, paint);
32
33
return bitmapWithReflection;
34
}
三、Drawable 1、Bitmap转换成Drawable
BitmapDrawable bd= new BitmapDrawable(getResource(), bm);
3 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
Drawable zoomDrawable(Drawable drawable, height = drawable.getIntrinsicHeight();
Bitmap oldbmp = drawableToBitmap(drawable);
Matrix matrix = sx = ((matrix.postScale(sx, sy);
Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
BitmapDrawable(newbmp);
17
}
本文转自dyh7077063的博客