Note that there are some explanatory texts on larger screens.

plurals
  1. POAntialiased rounded corners on Android ImageView
    text
    copied!<p>I am new to android dev, and I have been trying for a few hours now to add nice and smooth rounded corners to an ImageView, without success.</p> <p>First thing I tried is simply to round corners of my images directly, but this implies changing the bitmap, and since I need to keep the raw ones, and those are pretty big, this is not really memory friendly. This would also cause other difficulties since my ImageView is fluid.</p> <p>Second thing I tried to use is the clipPath method after subclassing my view. This works, but corners are aliased. I then tried adding a PaintFlagsDrawFilter to implement the aliasing, but this didn't worked. I'm using monodroid, and I was wondering this was supposed to work in Java.</p> <p>Here is my code (C#): </p> <pre><code>public class MyImageView : ImageView { private float[] roundedCorner; /** * Contains the rounded corners for the view. * You can define one, four or height values. * This behaves as the css border-radius property * * @see http://developer.android.com/reference/android/graphics/Path.html#addRoundRect(android.graphics.RectF, float[], android.graphics.Path.Direction) */ public float[] RoundedCorners{ get{ return roundedCorner; } set{ float[] finalValue = new float[8]; int i=0; if(value.Length == 1){ for(i=0; i&lt;8;i++){ finalValue[i] = value[0]; } }else if(value.Length == 4){ for(i=0; i&lt;4;i++){ finalValue[2*i] = value[i]; finalValue[2*i+1] = value[i]; } } roundedCorner = finalValue; } } public SquareImageView (Context context) : base (context) { Initialize (); } public SquareImageView (Context context, IAttributeSet attrs) : base (context, attrs) { Initialize (); } private void Initialize () { RoundedCorners = new float[]{0,0,0,0}; } public override void Draw (Android.Graphics.Canvas canvas) { Path path = new Path(); path.AddRoundRect(new RectF(0,0, Width,Height),RoundedCorners, Path.Direction.Cw); canvas.ClipPath(path); base.Draw (canvas); } /** * try to add antialiasing. */ protected override void DispatchDraw (Canvas canvas) { canvas.DrawFilter = new PaintFlagsDrawFilter((PaintFlags)1, PaintFlags.AntiAlias); base.DispatchDraw (canvas); } } </code></pre> <p>Thanks for your help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload