Android-Super simple to achieve picture rounded corners

Android-Super simple to achieve picture rounded corners

I've been troubled by image rounding recently. Many methods on the Internet can round image corners normally. But when I use it, the rounded corners are a bit sharp, not smooth at all. I use Glide to get the image. Glide also has BitmapTransformation to expand and implement rounded corners, but the effect I show is not very good. In the end, I concluded that it is because Glide compresses the pixels of the image and reduces the resolution, resulting in poor rounded corners. I couldn't find a solution on the Internet, so I tried to find a solution myself. Without further ado, let's see the implementation.

[[164613]]

This method is only suitable for pictures with small resolution. If the resolution is too high, the angle will not be round.

1. Customize ImageView and rewrite the ondraw method
2. The code is as follows:

  1. /**
  2. * Cut off the fillet
  3. */ public class RoundCornersImageView extends ImageView { private float radius int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } /**
  4. *
  5. * @param rx x direction radians
  6. * @param ry y direction radians
  7. */ public void setRadius(float rx, float ry) { this.radiusX = rx ; this.radiusY = ry ; } private void init() { radiusX = 58 ; radiusY = 58 ; } @Override protected void onDraw(Canvas canvas) { Path path = new Path(); Rect rect = new Rect(0, 0, getWidth(), getHeight()); RectF rectF = new RectF(rect); path.addRoundRect(rectF, radiusX, radiusY, Path.Direction.CCW); canvas.clipPath(path, Region.Op.REPLACE);//Op.REPLACE will display everything within this range, and the part beyond will be overwritten super.onDraw(canvas); } }

3. Ok, that's it. The idea is to take the graphics within the rounded rectangle display range

<<:  Android design pattern singleton mode

>>:  iOS: Let's talk about Designated Initializer

Recommend

Why don't Chinese planes flying to the US cross the Pacific Ocean?

The first human flight across the Pacific Ocean w...

Carbon neutrality is cute, everyone should know it sooner

Hello everyone, I am the cute little carbon dioxi...

Why can't an adult's heart regenerate? Scientists: Actively refuse

The liver has the greatest regenerative capacity ...

How can one operate a product like Xiaokaxiu on his own?

Recently, a friend asked me this question: "...

Are the hard days of the color TV industry coming to an end?

On November 22, along with the release of the 201...