先将Drawable的图片格式转化成bitmap,然后在转化成圆角图片
// 将Drawable转化为Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas);
return bitmap;
}
// 获得圆角图片的方法
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
// 放大缩小图片
<span style="white-space:pre"> </span>public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
<span style="white-space:pre"> </span>int width = bitmap.getWidth();
<span style="white-space:pre"> </span>int height = bitmap.getHeight();
<span style="white-space:pre"> </span>Matrix matrix = new Matrix();
<span style="white-space:pre"> </span>float scaleWidht = ((float) w / width);
<span style="white-space:pre"> </span>float scaleHeight = ((float) h / height);
<span style="white-space:pre"> </span>matrix.postScale(scaleWidht, scaleHeight);
<span style="white-space:pre"> </span>Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
<span style="white-space:pre"> </span>return newbmp;
<span style="white-space:pre"> </span>}
然后就可以直接调用了
Bitmap bitmap = ImageUtil.drawableToBitmap(drawable);
// 缩放图片
Bitmap zoomBitmap = ImageUtil.zoomBitmap(bitmap, 480, 300);
// 获取圆角图片
Bitmap roundBitmap = ImageUtil.getRoundedCornerBitmap(zoomBitmap, 50.0f);
mImageView01.setImageBitmap(roundBitmap);