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

6 suggestions for elevator advertising

Every day when we enter and leave residential are...

Fission level 14, 8,000 copies sold out, a screen-sweeping distribution case!

Regarding fission, let me explain to you here how...

Plants 250 million years ago were able to "transform" the earth

Recently, the plant paleoecology team led by Prof...

Are driverless subways reliable?

I read in the news that the first batch of fully ...

Will washing away blood lipids make your body healthy?

When the machine starts, the garbage in the blood...

Let's talk about how to achieve high performance with CQRS architecture

Introduction to CQRS Architecture You should all ...

The secrets of the death of smart hardware startups

This article is transferred from: Channel Help Sh...

UI Development Trends in 2020: Declarative UI to Rule Them All

In 2020, it seems that the main players in UI dev...