Original article, reprint please contact the author

preface

Glide is the most widely used image loading framework and is officially recommended by Google. In terms of image processing, it offers a lot of nice features.

How to be flexible

Glide itself provides a way to load rounded images — but it’s a simple quadrangle. In actual project development, sometimes you just need to show the rounded corners at the top, or you just need to show the rounded corners on the left, etc. So, this method is applicable to use Glide as the project picture frame of the small cute, developers can set the four rounded corners separately, not only show and hide, the radius of each rounded corner is also independent existence.

Using document

Glide has a way to make Transformations of pictures before they are displayed. For more information on this topic, you can read this Glide – Custom conversion. This article’s RoundCorner is implemented by inheriting the BitmapTransformation class. It provides two constructors, one of which takes four arguments: leftTop: top left, rightTop: top right, leftBottom: bottom left, rightBottom: bottom right. For the outside, you can flexibly choose which fillets to display and the radius of the four fillets. Another function that provides only one argument is to set all four rounded corners at the same time. Of course, this is used when all four rounded corners are displayed at the same radius. In the meantime, all you need to do is pass the values in the constructor, and the dp processing is already done inside the class. So let’s do a simple demonstration.

  • Load a normal rounded image
Glide.with(this).load("http://p15.qhimg.com/bdm/720_444_0/t01b12dfd7f42342197.jpg")
        .apply(RequestOptions.bitmapTransform(RoundCorner(20f)))
        .into(img)
Copy the code

  • It’s just rounded at the top
Glide.with(this).load("http://p15.qhimg.com/bdm/720_444_0/t01b12dfd7f42342197.jpg")
        .apply(RequestOptions.bitmapTransform(RoundCorner(leftTop = 20f, rightTop = 20f)))
        .into(img)
Copy the code

  • Just the left rounded corner
Glide.with(this).load("http://p15.qhimg.com/bdm/720_444_0/t01b12dfd7f42342197.jpg")
        .apply(RequestOptions.bitmapTransform(RoundCorner(leftTop = 20f, leftBottom = 20f)))
        .into(img)
Copy the code

This is a simple demo, but if you want more flexibility in loading rounded images, you can use this method. The code is here, the portal — RoundCorner

conclusion

The above