Android customized view to realize circular, rounded and elliptical pictures (bitmapshader graphics rendering)

1、 Foreword

Android implements rounded rectangle, circle or ellipse graphics. Generally, it is mainly realized by a custom view and xfermode. In fact, there are many methods to realize fillet images. The common one is to use xfermode and shader. This paper directly inherits ImageView and uses bitmapshader method to draw circles, fillets and ellipses. After you see my method in this paper, other similar shapes can be drawn.

2、 Renderings:

3、 Introduction to bitmapshader

Bitmapshader is a subclass of shader, which can be accessed through paint Setshader (shader shader)

We only focus on bitmapshader here. Construction method:

Parameter 1: bitmap

Parameter 2, parameter 3: tilemode;

There are three values for tilemode:

Clamp stretch

Repeat repeat

Mirror mirror

If you set up a screensaver for the computer screen, if the picture is too small, you can choose to repeat, stretch and mirror;

Repeat: repeat the bitmap horizontally and vertically

Mirror image: continuously flip and repeat horizontally and vertically;

Stretch: this mode should be somewhat different from the computer screensaver. This stretch is the last pixel of the picture; The last horizontal row pixel in the horizontal direction repeats continuously, and the column pixel in the vertical term repeats continuously;

public BitmapShader(Bitmap bitmap,Shader.TileMode tileX,Shader.TileMode tileY)

Call this method to generate a shader with a bitmap drawn.

Bitmap bitmap used within the renderer

tileX The tiling mode for x to draw the bitmap in. Tile mode in X direction on bitmap

tileY The tiling mode for y to draw the bitmap in. Tile mode in Y direction on bitmap

Tilemode: (there are three types in total)

Clamp: if the renderer is outside the original boundary, the edge coloring within the range is copied.

Repeat: repeat renderer images horizontally and vertically, tiled.

Mirror: horizontal and vertical repeat renderer images. This is different from repeat repeat. It is tiled in a mirror way.

4、 Implementation of custom circle, fillet and ellipse picture view

1. Measure the size of the view

2. Set bitmapshader and brush paint

3. Finally, the rounded corners, circles and ellipses are drawn. They must be in OnDraw. The basic principle is to use the brush rendered by the above mbitmapshader to draw

5、 View layout implementation

This is very simple. There are three customized Views:

6、 Using and testing custom views

After the custom view directly drawn above has been written, this view is used below. The use method is the same as that of ordinary ImageView. It can be used as an ordinary control.

7、 Summary

The above is the whole content of this article. I hope the content of this article can help you develop Android.

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
分享
二维码
< <上一篇
下一篇>>