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

Charming anorthosite: the oldest imprint of the Earth and the Moon

Speaking of anorthoclase, I believe most people a...

The heart-wrenching experience of the TV version of "Never Give Up"

Screen: Sound Effects: operate: Plot: Experience:...

Find the perfect platform 8 APP testing solutions

Testing applications is very cumbersome. Where ca...

Guidelines for Advertising in the Tourism Industry

During the peak travel season at the end of the y...

What are the unknown rules and tricks in Baidu bidding?

Baidu bidding is a good thing. It can be said tha...

The essence of men's culture: Dragon Turtle Rejuvenation Technique

Are you suffering from unreasonable complaints, c...

To promote Internet products, are you still using traditional branding methods?

As we all know, the advertising industry has a hi...

What is keyword promotion? Share 4 promotion tips!

Keyword promotion means that when users search us...