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

Can I apply for a mortgage payment deferral due to the epidemic? How to apply?

Recently, epidemics have occurred in many places i...

App Store Market Optimization Tips and Strategies!

As the leader of the third-party application mark...

Neolithic "Meteor Hammer" Discovered!

Recently, Chen Zuhua, a citizen of Nanchang City,...

Sharp under Foxconn’s thinking: the father of LCD or the king of low prices?

Since Foxconn took over Sharp in August last year...

Mobile Assistant

Source code introduction Similar to 360 mobile as...

IIC: Industrial Internet of Things Artificial Intelligence Framework White Paper

Industrial AI (AI), the application of artificial...

Google releases x86 64-bit Android L emulator

The video, created by Terence Tuhinanshu, shows a...