public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx)
{
if(bitmap == null)
return null;
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(Paint.ANTI_ALIAS_FLAG);
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
// final Rect rectd = new Rect(0, 0, bitmap.getWidth()*1.5, bitmap.getHeight()1.5);
final RectF rectF = new RectF(rect);
// 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;
}
剪切图片
ImageView imgView = (ImageView) this.findViewById(R.id.img_logo);
Drawable d = getApplicationContext().getResources().getDrawable(R.drawable.empty_img);
BitmapDrawable drawable = (BitmapDrawable) d;
Bitmap bitmap = drawable.getBitmap();
if (bitmap != null) {
bitmap = BitmapHelper.getRoundedCornerBitmap(bitmap, bitmap.getWidth() / 2);
if (bitmap != null) {
imgView.setImageBitmap(bitmap);
}
}
图片使用