Several methods of implementing fillet image in Android

There are many poses for fillet images in Android. How many have you unlocked?

Method 1: setxfermode method

This method is to create a new bitmap of the same size, and then use paint. Setxfermode (New porterduffxfermode (mode. Src_in)); First draw the rounded rectangle, then draw the original bitmap, and then get a rounded bitmap.

comment:

It was used more in the early stage, occupying double the memory of bitmap.

Method 2: use bitmapshader

In this way, bitmapshader is generated from bitmap and then drawn into canvas. Some key codes are as follows. For the complete code, please refer to roundimageview in quickaf

comment:

It takes up a lot of memory and the implementation is a little complicated.

Method 3: image loading Library

At present, there are many popular image loading libraries on GitHub. Based on the rounded image function, you can easily achieve the desired effect with a little configuration. In fact, at the bottom, it is nothing more than using the above two methods. For example, the roundedbitmapdisplayer of Android universal image loader was implemented using setxfermode in the early stage, and later bitmapshader.

Take the alternative fresco as an example. Although the bottom layer is implemented in C, the fillet processing is still implemented in the Java layer in the way of bitmapshader. However, for the fillet implementation of non bitmap, fresco is painted directly with paint. Attach fresco configuration.

comment:

Implemented by the framework, it is simple and stable.

Method 4: Mask

Setxfermode is still used in this method, but the difference from method 1 is that no change is made to the picture. Only four corners with the same color as the background are drawn outside the fillet to block it, resulting in the visual effect of the fillet picture. The key codes are as follows:

Please refer to roundmaskview in quickaf for detailed code

In this way, rounded objects are not limited to ImageView, but can also be any layout, such as the following example

With FrameLayout, the LinearLayout is rounded. In terms of visual effect, the upper left and right fillets of ImageView and the lower left and right fillets of textview.

comment:

It has certain limitations, but not limited to pictures. All layouts can realize rounded corners visually.

about

Quickaf is an app rapid development framework on Android platform.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>