简体   繁体   中英

How to give one radius corners to an imageview at runtime in android

I am interested in giving a radius to only one corner of an imageview. I am using the following code to give round corners to imageview, but it works on all four corners and I want only one corner to be rounded.

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);
final float roundPx = pixels;

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);

You may try the following:
1. Extend ImageView
2. Override onDraw method

a. Create a clip path object (Path class object clipPath)
b. Define your clipping region ( clipPath.addArc)
c. Set clip path to Canvas
d. Invoke super.Ondraw(Canvas)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM