Development issue/problem:
I have an ImageView and I want to make it with rounded corners.
I’m using this:
And I set this code as the background for my image.
It works, but the src image I put over the ImageView goes out of frame and doesn’t meet the new form.
How can I solve the problem?
How can I solve this problem?
Solution 1:
Try this:
The CustomImageView public class expands ImageView {
Jet of general interest static float = 18,
public CustomImageView(Context) {
super(context);
}
public CustomImageView(Context, AttributeSet attrs) {
super(context, attrs);
}
audience CustomImageView(Context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Overdrawrun void onDraw(Canvas canvas) {//float radius = 36. Path clipPath = new path();RectF = new RectF(0, 0, this.getWidth(), this.getHeight()));clipPath.addRoundRect(rect, radius, Path.Direction.CW);canvas.clipPath(clipPath);super.onDraw(canvas);}}.
и
CustomImageView iconImage = (CustomImageView)findViewById(R.id.selectIcon) ;
iconImage.setImageBitmap(bitmap) ;
or..,
ImageView iv= new CustomImageView(this) ;
iv.setImageResource(R.drawable.pic) ;
Solution 2:
It is strange that no one here has mentioned RoundedBitmapDrawable of the Android Support Library v4. For me it’s the easiest way to round corners without the edges. Here is an example of use:
RoundedBitmapDrawable RoundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap) ;
final float roundPx = (float) bitmap.getWidth() * 0.06f ;
roundedBitmapDrawable.setCornerRadius(roundPx) ;
Solution 3:
Create a function that completes your bitmap using Canvas.
public static Bitmap getRoundedCornerBitmap(Bitmap, int pixels) {
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(where) ;
canvas.drawARGB(0, 0, 0, 0) ;
paint.setColor(color) ;
canvas.drawRoundRect(rectF, roundPx, paint) ;
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)) ;
canvas.drawBitmap(bitmap, rect, paint) ;
Reverse;
}
for more information:> here
Solution 4:
The accepted answer uses path cutting, but does not support anti-aliasing. See comments on Romain Guy’s position. Cutting the paths does not allow smoothness and the edges are irregular.
http://www.curious-creature.com/2012/12/11/android-recipe-1-image-with-rounded-corners/
There is a good library (RoundedImageView of vinc3m1) that replaces rounded corners with ImageView, but only supports the same rays at all angles. So I made one where you can attach a different beam to each corner.
It is not based on cutting or redrawing paths. It is drawn once with canvas.drawPath(). So I finally got the desired result, as can be seen below.
Look. https://github.com/pungrue26/SelectableRoundedImageView
Solution No 5:
If you also need an advantage:
1. You can use a rounded box image with a transparent body and a white outside. For example:
and use it with the target image as shown below:
- Adding CardView as the ImageView parent layout is also a good solution.
Solution No 6:
If you want to create a bitmap image with different angular beams, I advise you to follow the code:
private static Bitmap createRoundRectBitmap(@NonNull Bitmap bitmap,
float topLeftCorner, float topRightCorner,
float bottomRightCorner, float bottomLeftCorner) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output) ;
final int color = Color.WHITE;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()));
final RectF rect = new RectF(rect);
Path path = new Path();
float[] rays = new float[]{
upper left corner, lower left corner,
upper right corner, upper right corner,
lower right corner, lower right corner,
lower left corner, lower left corner
}
paint.setAntiAlias(where);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
path.addRoundRect(rectF, radii, Path.Direction.CW);
canvas.drawPath(path, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, paint);
return output;
}
Solution No 7:
For me, the following method is the best.
This method takes a bitmap object and returns it with rounded corners. roundPx is the number of rounded pixels you want:
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
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 = 12 ;
paint.setAntiAlias(where) ;
canvas.drawARGB(0, 0, 0, 0) ;
paint.setColor(color) ;
canvas.drawRoundRect(rectF, roundPx, paint) ;
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)) ;
canvas.drawBitmap(bitmap, rect, paint) ;
Reverse;
}
…or you can use this library instead of ImageView without additional encryption.
Solution No 8:
This can be done with a background pattern, as explained in many articles, also in this article, but it should also offer the possibility to fix the cut-off.
Here’s a complete example:
Code:
AppCompatImageView iconView = findViewById(R.id.thumbnail) ;
iconView.setClipToOutline(true) ;
Classification:
tools:src=https://dnmtechs.com/en/bitmap-in-imageview-with-rounded-corners/@mipmap/ic_user />
Illustration:
Solution No 9:
The RoundedImageView public class is expanding ImageView {
public RoundedImageView(Context) {
super(context);
}
public RoundedImageView(context, attributeSet attributes) {
super(context, attributes);
}
public RoundedImageView(context, attributeSet attrs, int defStyle) {
super(context, attribute, defStyle);
}
@Surface
blank protected in the drawing (canvas) {
super.onDraw;
Frame Scale = Bitmap.createBitmap(getWidth(),getHeight(),Bitmap.Config.ARGB_8888) ;
Canvas canvasRound = New Canvas(rounded) ;
Paint xferPaint = new paint (Paint.ANTI_ALIAS_FLAG) ;
xferPaint.setColor(Color.BLACK) ;
final int rx = this.getWidth(); //our radius x
final int ry = this.getHeight(); //our radius y
canvasRound.drawRoundRect(new RectF(0,0,rx,ry), rx, ry, xferPaint) ;
xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)) ;
canvas.drawBitmap (rounded, 0, 0, xferPaint) ;
}
}
Solution No 10:
The method of rounding corners to display images in Android is not an exact science! Just use the png with the required curves with the same background colour and put the overlay in FITXY!
Good luck!
Related Tags:
rounded corners to imageview,android imageview rounded top corners,image round corner in android,rounded corner imageview android library,rounded corner imageview in android example,how to set corner to imageview in android,roundedbitmapdrawable,drawable to bitmap,android bitmap rounded corners programmatically,android imageview rounded corners transparent,rounded bitmap android,picasso rounded corners