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 deal with the image to the desired shape. Just as the anise Bean's "anise" is more than one. After my research. The method of drawing special pictures is not a kind of, I found three kinds. And listen to my one by one-way.
Using Xfermode two diagram intersection method
Find out how you can set the Xfermode-in-the-intersect mode of the brush in Android to set the display after the two-image intersection. Detailed mode see, source code can go to Android Apidemo. (SRC is the image that we want to draw on the target graph, DST is the target graph)
By being able to see, suppose 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. The same also can draw our diagram first, then draw a circle, just xfermode to choose Dst_in. Both can achieve the effect we need. The demo 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, for example, Avatar P.setxfermode (New Porterduffxfermode (mode.dst_in)); Since we drew the diagram first, dst_inint radius = bitmap.getwidth; If the picture is a square canvas.drawcircle (radius, radius, radius, p); R=radius, center (r,r)
The above is a simple demonstration sample, according to the above 16 modes you can actually make a lot of other effects. In addition, just want you to give an intersection diagram. What is the shape of that picture, and what is our picture?
To implement a graphic of a specified shape by cropping the canvas area
The Android Canvas offers Clippath, ClipRect, Clipregion and other methods to crop, with different combinations of path, Rect, region, and Android almost able to support freeform cropping.
So we were almost able to get the random shape of the area and then draw on this area to get the picture we wanted. See the Demo sample directly.
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 directly at the Demo sample
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, which in essence is actually a circle, but the rendering uses our images and then we can get the specified shape. But I think. This is not suitable for drawing very complex graphics, but in memory consumption, it should be much smaller than the first type. At the same time, setting the Shader.TileMode.MIRROR, but also to achieve the mirror effect, is also excellent.
The above is the implementation of three methods, three methods can draw a lot of shapes, of course, encountered very very complex situation, I would recommend the use of the first. This time allows the artist to give an end-class shape chart. Save yourself the 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 use Bitmapshader, and of course there are other controls, and maybe some other implementations, assuming you know. Able to 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