Several Methods to draw circular images in Android

Source: Internet
Author: User

Several Methods to draw circular images in Android

There are often some requirements during development, such as displaying the Avatar and displaying some special requirements, and displaying the image as rounded or circular or other shapes. However, images on our hands or images obtained from servers are usually square. At this time, we need to process the image as needed. For example, there is more than one method of "NLP" in the text of Xiangxiang bean. After my research, there is no way to draw special pictures. I have found three methods and I will hear them one by one.

Intersection of Two Images Using Xfermode

By searching for information, we found that in android, you can set the Xfermode of the paint brush to the intersection mode, and then set the display mode after the intersection of the two images. For details about the mode, see. The source code can go to android apidemo. (SRC is the source image to be drawn to the target image, that is, the source image. DST is the target image)

We can see that if we need to draw a circular graph, we can first draw a circle of the same size as the target on the canvas, and then select SRC_IN for xfermode, let's talk about our profile picture or other pictures. You can also draw a graph first and then draw a circle. However, you must select DST_IN for xfermode. Both of them can achieve the desired effect. The sample code is as follows:

 
 
  1. Paint p = new Paint ();
  2. P. setAntiAlias (true); // de-sawtooth
  3. P. setColor (Color. BLACK );
  4. P. setStyle (Paint. Style. STROKE );
  5. Canvas canvas = new Canvas (bitmap); // bitmap is our original image, such as the Avatar
  6. P. setXfermode (new porterduxfermode (Mode. DST_IN); // DST_IN because the figure is drawn first
  7. Int radius = bitmap. getWidth; // assume that the image is square.
  8. Canvas. drawCircle (radius, p); // r = radius, center (r, r)

The above is a simple example. Based on the above 16 modes, you can actually make more results. In addition, as long as you give an intersection chart, the shape of the chart can be displayed.

Specifies the shape of the image by cropping the canvas area

In Android, Canvas provides methods such as ClipPath, ClipRect, and ClipRegion for cropping. By using different combinations of Path, Rect, and Region, Android supports cropping areas of almost any shape. Therefore, we can get almost any shape of the area, and then draw a picture on this area, we can get the image we want, directly look at the example.

 
 
  1. Int radius = src. getWidth ()/2; // src indicates the image to be drawn, which is the same as bitmap in the example.
  2. Bitmap dest = Bitmap. createBitmap (src. getWidth (), src. getHeight (), Bitmap. Config. ARGB_8888 );
  3. Canvas c = new Canvas (dest );
  4. Paint paint = new Paint ();
  5. Paint. setColor (Color. BLACK );
  6. Paint. setAntiAlias (true );
  7. Path path = new Path ();
  8. Path. addCircle (radius, Path. Direction. CW );
  9. C. clipPath (path); // specifies the cropping area.
  10. C. drawBitmap (src, 0, 0, paint); // upload the picture

Use BitmapShader

Let's look at the example first.

 
 
  1. int radius = src.getWidth() / 2; 
  2. BitmapShader bitmapShader = new BitmapShader(src, Shader.TileMode.REPEAT, 
  3. Shader.TileMode.REPEAT); 
  4. Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888); 
  5. Canvas c = new Canvas(dest); 
  6. Paint paint = new Paint(); 
  7. paint.setAntiAlias(true); 
  8. paint.setShader(bitmapShader); 
  9. c.drawCircle(radius,radius, radius, paint); 

Shader is the paint brush Renderer. In essence, this method is actually a circle, but the rendering uses our image, and then we can get the specified shape. However, I think this is not suitable for drawing complex images, but the memory consumption should be much smaller than the first one. In addition, setting Shader. TileMode. MIRROR can also achieve the mirroring effect, which is also excellent.

The above three methods can be used to draw a lot of shapes. Of course, when there are very, very, and very complex situations, I suggest using the first method, at this time, the artist can give a last-shift shape chart, saving himself from coding. You can choose one based on your needs.

On github, CustomShapeImageView is drawn using the first method we mentioned. RoundedImageView and CircleImageView are completed using bitmapshader. Of course, there may be some other controls and some other implementation methods. If you know, you can reply and tell me ^_^.

Original article address: several methods to draw a circular image 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.