Android 如何将方形图像转换为椭圆形

Android 如何将方形图像转换为椭圆形,android,android-layout,Android,Android Layout,在我的应用程序中,我从图库中获取图像,该图像的形状是方形的。我想将该图像设置为imageView,然后它应该是椭圆形。也就是说,在我的例子中,我需要裁剪出像人脸一样的图像。有人能告诉我怎么做吗?提前谢谢。使用下面的类而不是图像视图 RoundedCornerImageView imageView1; imageView1.setRadius(10); 这将使图像半径增加10像素,你可以给出你想要的wat值,并使其成为你想要的形状。试试看 祝您一切顺利: public class Round

在我的应用程序中,我从图库中获取图像,该图像的形状是方形的。我想将该图像设置为imageView,然后它应该是椭圆形。也就是说,在我的例子中,我需要裁剪出像人脸一样的图像。有人能告诉我怎么做吗?提前谢谢。

使用下面的类而不是图像视图

 RoundedCornerImageView imageView1;
 imageView1.setRadius(10);
这将使图像半径增加10像素,你可以给出你想要的wat值,并使其成为你想要的形状。试试看

祝您一切顺利:

public class RoundedCornerImageView extends ImageView {
    private int radius = 10;

    public RoundedCornerImageView(Context context) {
        super(context);
    }

    protected void onDraw(Canvas canvas) {
        Path clipPath = new Path();
        int w = this.getWidth();
        int h = this.getHeight();
        clipPath.addRoundRect(new RectF(0, 0, w, h), radius, radius, Path.Direction.CW);
        canvas.clipPath(clipPath);
        super.onDraw(canvas);
    }

    public RoundedCornerImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public RoundedCornerImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setRadius(int radius){
        this.radius = radius;
        this.invalidate();
    }
}
你可以用这个

public  Drawable getRoundedCornerImage(Drawable bitmapDrawable) {
        Bitmap bitmap = ((BitmapDrawable)bitmapDrawable).getBitmap();
        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 = 100;

        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);
        Drawable image = new BitmapDrawable(output);
        return image;

    }
希望这对你有帮助

你也可以用这个或

最后,在主要活动中,实例化类形状并将两个参数传递给类。要裁剪为椭圆形的图像和将设置最终图像的imageview

可能重复的
public class Shape {

    private Bitmap bmp;
    private ImageView img;
    public Shape(Bitmap bmp, ImageView img) {

        this.bmp=bmp;
        this.img=img;
        onDraw();
    }


    private void onDraw(){

        Canvas canvas=new Canvas();
        if (bmp.getWidth() == 0 || bmp.getHeight() == 0) {
            return;
        }

        int w = bmp.getWidth(), h = bmp.getHeight();

        Bitmap roundBitmap = getOvalCroppedBitmap(bmp, w);

        img.setImageBitmap(roundBitmap);

    }


    public static Bitmap getOvalCroppedBitmap(Bitmap bitmap, int radius) {
        Bitmap finalBitmap;
        if (bitmap.getWidth() != radius || bitmap.getHeight() != radius)
            finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius,
                    false);
        else
            finalBitmap = bitmap;
        Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
                finalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, finalBitmap.getWidth(),
                finalBitmap.getHeight());

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.parseColor("#BAB399"));
        RectF oval = new RectF(0, 0, 130, 150);
        canvas.drawOval(oval, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(finalBitmap, rect, oval, paint);

        return output;
    }