Several ways to draw a circular picture in Android

Source: Internet
Author: User

There are often some requirements in development, such as displaying avatars, showing some special needs, and showing pictures as rounded or rounded or some other shape. But often the pictures we have on our hands or the images we get from the server are square. At this point we need to do our own processing, the image to be processed into the desired shape. As the anise Bean's "anise" is more than one, after my research, the method of drawing a special picture is not a kind of, I found three kinds, and listen to my one by one Tao.

Using Xfermode two diagram intersection method

Find the data found in Android can set the brush Xfermode is the intersection mode, so that the two map after the intersection of the display mode, the specific mode, the source can go to Android Apidemo. (SRC is the image that we want to draw on the target graph, DST is the target graph)


As you can see, if we need to draw a circular graph, we can draw a circle on the canvas with the same size as the target, and then Xfermode choose Src_in, and then we can go on our avatar or other pictures. Also can draw our diagram first, then draw the circle, but xfermode to choose Dst_in. Both of these 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 figure, such as the Avatar P.setxfermode (new Porterduffxfermode (mode.dst_in)); Because we drew the graph first, so dst_inint radius = bitmap.getwidth; Assume that the picture is a square canvas.drawcircle (radius, radius, radius, p); R=radius, center (r,r)  

The above is a simple example, according to the above 16 modes you can actually make more effects. In addition, as long as you give an intersection diagram, the shape of the graph, our diagram can show what.

To implement a graphic of a specified shape by cropping the canvas area

The Android Canvas provides Clippath, ClipRect, Clipregion and other methods to crop, with different combinations of path, Rect, region, and Android can support almost any shape of clipping area. So, we can almost get the area of any shape, and then draw on this area, we can get the picture we want, just look at the example.

int radius = src.getWidth() / 2; //src为我们要画上去的图,跟上一个示例中的bitmap一样。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, radius, radius, Path.Direction.CW);c.clipPath(path);   //裁剪区域c.drawBitmap(src, 0, 0, paint);  //把图画上去
Using Bitmapshader

Look at the example directly 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 ();p Aint.setantialias (True);p Aint.setshader (Bitmapshader); C.drawcircle (Radius,radius, Radius , paint);  

Shader is the brush renderer, essentially this method is actually a circle, but the rendering takes our picture, then we can get the specified shape. But I think that this is not suitable for drawing very complex graphics, but in memory consumption, should be much smaller than the first. At the same time, set Shader.TileMode.MIRROR, also can achieve mirror effect, is also excellent.

The above is the implementation of the three methods, three methods can draw a lot of shapes, of course, encountered very very very very complex situation, I was recommended to use the first, this time you can let the artist to a last class shape chart, save yourself to code to draw. People choose according to their own needs.

The Customshapeimageview on GitHub is done with the first method we've talked about. Roundedimageview and Circleimageview are done using Bitmapshader, and of course there may be some other controls, and maybe some other implementation methods, if you know, can reply to tell me ^_^.

Original address: Http://blog.isming.me/2014/09/19/draw-circle-image-in-android/, reproduced please indicate the source.

Several ways to draw a circular picture in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.