Android develops a method to draw rounded rectangles on canvas using custom view

This article describes the method of drawing rounded rectangle on canvas by using custom view in Android development. Share with you for your reference, as follows:

A few days ago, in a project of the company, the avatar image needs to be added with rounded corners, so the UI effect will be better. Therefore, a small demo is written to define the rounded corners, which is mainly rendered by bitmapshader (if you want to cut a picture into ellipses or circles and display it on the screen, you can also use bitmapshader)

The basic steps for bitmapshader class to render pictures are as follows:

1. Create an object of the bitmapshader class

There are three types of shader.titlemode: calmp, mirror and repeat

Calmp: use boundary color to fill the remaining space mirror: use mirror mode repeat: use repeat mode

2. Set the brush through paint's setshader (bitmapshafer)

3. Use the brush with setshader (bitmapshafer) to draw graphics

The following is a demo of drawing a fillet image

1. Custom roundercornerimageview.java class

2. Roundactivity.java class that displays the fillet image

In addition, some basic steps and methods that must be implemented for customizing the view under the notes

1. Inherit view

2. Override the construction method of custom view

3. If you need to measure the position of the view and rewrite the layout, you need to override the onmeasure(), onlayout(), and ondraw() methods

Onmeasure(): the size of the view itself can be measured. Onlayout(): the position of the view in the ViewGroup can be determined. Ondraw(): defines how to draw the view

More readers interested in Android related content can view the special topics of this site: Android control usage summary, Android development introduction and advanced tutorial, Android view skill summary, activity operation skill summary of Android programming, Android database operation skill summary and Android resource operation skill summary

I hope this article will help you in Android programming.

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