Android ImageView 上的抗锯齿圆角

9

我是android开发的新手,已经尝试了几个小时来给ImageView添加漂亮、平滑的圆角,但一直没有成功。

首先我尝试直接对图片进行圆角处理,但这意味着需要改变位图,而由于我需要保留原始图片,而且那些图片相当大,这样并不太友好。这也会导致其他困难,因为我的ImageView是流体的。

其次,我尝试使用剪切路径方法来子类化我的视图。这可以工作,但是圆角是锯齿状的。然后我尝试添加PaintFlagsDrawFilter来实现抗锯齿,但这没有起作用。我正在使用monodroid,并且想知道这是否应该在Java中工作。

以下是我的代码(C#):

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<8;i++){
                    finalValue[i] = value[0];
                }
            }else if(value.Length == 4){
                for(i=0; i<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);
    }

}

感谢您的帮助!
2个回答

2
我创建了一个基于Romain Guy的示例代码(链接)RoundedImageView,它将这个逻辑封装到一个ImageView中,您只需直接使用即可。它支持边框和抗锯齿功能。
与其他圆角示例相比,它更加高效,因为它不会创建位图的另一个副本,也不使用绘制两次到画布的clipPath。

这看起来不错,似乎是我在寻找的东西。我现在没有安卓设备测试它,所以我不确定是否应该将此问题标记为已回答。 - Alexandre de Champeaux

1
请使用以下代码。
public Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) 
    {
        Bitmap output = null;

        if(bitmap != null)
        {
            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);
        }

        return output;
    }

并像这样调用该方法

   imageView.setImageBitmap(getRoundedCornerBitmap(bitmap, 10)); 

谢谢你的回答。但是正如我所说,我已经尝试过将我的图像的角落变圆,但由于我需要一个流体大小的位图,这意味着保留对原始位图的引用,而我想避免这种情况。基本上,我想要将视图本身的角落变圆,就好像我正在使用这个解决方案:链接 - Alexandre de Champeaux
1
好的 @Alex,请看这个链接 - Vivek Kumar Srivastava

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接