public class RoundedCornerImageView extends ImageView
{
public RoundedCornerImageView(Context context)
{
super(context);
}
public RoundedCornerImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas)
{
// Round some corners betch!
Drawable maiDrawable = getDrawable();
float mCornerRadius = 10 * getContext().getResources()
.getDisplayMetrics().density;
if (maiDrawable instanceof BitmapDrawable && mCornerRadius > 0)
{
Paint paint = ((BitmapDrawable) maiDrawable).getPaint();
final int color = 0xff000000;
final RectF rectF = new RectF(0, 0, getWidth(), getHeight());
// Create an off-screen bitmap to the PorterDuff alpha blending to work right
int saveCount = canvas.saveLayer(rectF,
null,
Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG
| Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
| Canvas.FULL_COLOR_LAYER_SAVE_FLAG
| Canvas.CLIP_TO_LAYER_SAVE_FLAG);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, paint);
Xfermode oldMode = paint.getXfermode();
// This is the paint already associated with the BitmapDrawable that super draws
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
this.setScaleType(ImageView.ScaleType.FIT_XY);
super.onDraw(canvas);
paint.setXfermode(oldMode);
canvas.restoreToCount(saveCount);
}
else
{
super.onDraw(canvas);
}
}
}
{
public RoundedCornerImageView(Context context)
{
super(context);
}
public RoundedCornerImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas)
{
// Round some corners betch!
Drawable maiDrawable = getDrawable();
float mCornerRadius = 10 * getContext().getResources()
.getDisplayMetrics().density;
if (maiDrawable instanceof BitmapDrawable && mCornerRadius > 0)
{
Paint paint = ((BitmapDrawable) maiDrawable).getPaint();
final int color = 0xff000000;
final RectF rectF = new RectF(0, 0, getWidth(), getHeight());
// Create an off-screen bitmap to the PorterDuff alpha blending to work right
int saveCount = canvas.saveLayer(rectF,
null,
Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG
| Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
| Canvas.FULL_COLOR_LAYER_SAVE_FLAG
| Canvas.CLIP_TO_LAYER_SAVE_FLAG);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, paint);
Xfermode oldMode = paint.getXfermode();
// This is the paint already associated with the BitmapDrawable that super draws
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
this.setScaleType(ImageView.ScaleType.FIT_XY);
super.onDraw(canvas);
paint.setXfermode(oldMode);
canvas.restoreToCount(saveCount);
}
else
{
super.onDraw(canvas);
}
}
}