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

How to correctly place ads on TikTok?

1 Behind the TikTok phenomenon As of mid-June 201...

The warmest winter in Sichuan is here!

This is the most unfamiliar scenery in Sichuan, a...

Why do phones without touch keys have chins?

The development of mobile phones has driven the de...

Counting: Ancient Chinese Mathematical Wisdom on Small Sticks

The numbers that seem simple to modern people are...