How Android implements rounded rectangle and circular ImageView

There are many ways to implement rounded rectangle and circle in Android, among which the most common methods are imageloader, setting option and custom view.

1. Imageloader loads images

ImageLoader. getInstance(). displayImage(imageURL,imageView,Options.getRoundOptions());

2. Custom view implementation there are also many methods for custom view to realize rounded rectangle and circle, among which the most common is to use xfermode and shader. This article is to use bitmapshader to realize fillet rendering.

Custom circleimageview • talking about bitmapshader bitmapshader is a subclass of shader, which can be accessed through paint Set setshader (shader shader). Here we only focus on bitmapshader. Construction method: mbitmapshader = new bitmapshader (bitmap, tilemode. Clamp, tilemode. Clamp); Parameter 1: bitmap parameter 2, parameter 3: tilemode; There are three values for tilemode: clamp stretch repeat mirror image

Repeat: repeat the bitmap image horizontally and vertically: flip and repeat horizontally and vertically; Stretch: repeat 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; Now you can see that bitmapshader is set to mpaint, and when you paint with this mpaint, the painted area will be colored according to the tilemode you set. For our round corners and circles, the mode we set is clamp, but do you have a question: will the width or height of the view be stretched if it is larger than the width or height of our bitmap? Well, we will set a matrix for bitmapshader to appropriately enlarge or shrink the picture, so that the condition of "the width or height of the view is greater than the width or height of our bitmap" will not be tenable.

• custom attributes

• get custom attributes

•onMeasure

• set initialization parameters

• set the transform matrix for the renderer

•onDraw

Moreover, we have added several interfaces to the custom view, which can be used to directly set the type, edge color, edge width and picture information.

Use the circleimageview layout file:

We add click events to three imageviews in Java

The effect diagram after operation is as follows:

Source code download: http://xiazai.jb51.net/201609/yuanma/Android-ImageView (jb51.net). rar

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