Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw a circle picture in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to draw a circle picture in Android", so the editor summarizes the following contents, detailed contents, clear steps, and certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to draw a circle picture in Android" article.

Use the intersecting method of Xfermode two graphs

Through the search for information, it is found that the Xfermode of the brush can be set in android, that is, the intersection mode, thus setting the display mode after the two pictures intersect. The specific mode is shown in the figure below, and the source code can go to android apidemo. (SRC is the original picture that we want to draw on the target map, and DST is the target graph)

As you can see from the picture above, if we need to draw a circle, we can first draw a circle the same size as the target on the canvas, then xfermode selects SRC_IN, and then talk about our avatar or other pictures. You can also draw our picture first and then draw a circle, but xfermode should choose DST_IN. Both can achieve the effect we need. The sample code is as follows:

Paint p = new Paint (); p.setAntiAlias (true); / / Anti-aliasing p.setColor (Color.BLACK); p.setStyle (Paint.Style.STROKE); Canvas canvas = new Canvas (bitmap); / / bitmap is our original picture, such as the avatar p.setXfermode (new PorterDuffXfermode (Mode.DST_IN)); / / because we drew the picture first, DST_IN int radius = bitmap.getWidth; / / assume that the picture is square canvas.drawCircle (radius, p) / / r=radius, the center of the circle (r, r)

These are simple examples, and you can actually do more based on the 16 modes above. In addition, as long as you give an intersecting picture, what the shape of that picture is, our picture can be shown.

Realize the graphics of the specified shape by cropping the canvas area

Canvas in Android provides ClipPath, ClipRect, ClipRegion and other methods for clipping. Through the different combinations of Path, Rect and Region, Android can support clipping regions of almost any shape. So, we can get an area of almost any shape, and then draw a picture on this area, and we can get the picture we want, and look directly at the example.

Int radius = src.getWidth () / 2; / / src is the graph we want to draw, just like bitmap in the previous example. Bitmap dest = Bitmap.createBitmap (src.getWidth (), src.getHeight (), Bitmap.Config.ARGB_8888); Canvas c = new Canvas (dest); Paint paint = new Paint (); paint.setColor (Color.BLACK); paint.setAntiAlias (true); Path path = new Path (); path.addCircle (radius, Path.Direction.CW); c.clipPath (path); / / crop region c.drawBitmap (src, 0, 0, paint); / / put the picture on

Use BitmapShader

Let's just look at the example first.

Int radius = src.getWidth () / 2; BitmapShader bitmapShader = new BitmapShader (src, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); Bitmap dest = Bitmap.createBitmap (src.getWidth (), src.getHeight (), Bitmap.Config.ARGB_8888); Canvas c = new Canvas (dest); Paint paint = new Paint (); paint.setAntiAlias (true); paint.setShader (bitmapShader); c.drawCircle (radius,radius, radius, paint)

Shader is the renderer of the brush, in essence, this method is to draw a circle, but the rendering uses our image, and then you can get the specified shape. But I think this is not suitable for drawing very complex graphics, but in terms of memory consumption, it should be much smaller than *. At the same time, setting Shader.TileMode.MIRROR can also achieve mirror effect, which is also excellent.

The above is about the content of this article on "how to draw a circular picture in Android". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report