How to make an ImageView with rounded corners?

前端 未结 30 2561
天涯浪人
天涯浪人 2020-11-21 05:39

In Android, an ImageView is a rectangle by default. How can I make it a rounded rectangle (clip off all 4 corners of my Bitmap to be rounded rectangles) in the ImageView?

30条回答
  •  失恋的感觉
    2020-11-21 06:20

    Answer for the question that is redirected here: "How to create a circular ImageView in Android?"

    public static Bitmap getRoundBitmap(Bitmap bitmap) {
    
        int min = Math.min(bitmap.getWidth(), bitmap.getHeight());
    
        Bitmap bitmapRounded = Bitmap.createBitmap(min, min, bitmap.getConfig());
    
        Canvas canvas = new Canvas(bitmapRounded);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
        canvas.drawRoundRect((new RectF(0.0f, 0.0f, min, min)), min/2, min/2, paint);
    
        return bitmapRounded;
    }
    

提交回复
热议问题