Several ways to draw circular pictures in Android

Several ways to draw circular pictures in Android

There are often some requirements in development, such as displaying avatars, displaying some special requirements, and displaying pictures in rounded corners or circles or other shapes. But often the pictures we have or the pictures obtained from the server are square. At this time, we need to process the pictures ourselves and process them into the required shapes. Just as there is more than one way to write the word "卉" in fennel beans, after my research, there is more than one way to draw special pictures. I found three ways. Let me tell you one by one.

Use Xfermode to intersect two images

By searching for information, I found that in Android, you can set the Xfermode of the brush, that is, the intersection mode, to set the display mode after the two images intersect. The specific mode is shown in the figure below. The source code can be found in Android apidemo. (SRC is the image we want to draw on the target image, that is, the original image, and DST is the target image)

As can be seen from the above figure, if we need to draw a circular image, we can first draw a circle of the same size as the target on the canvas, then select SRC_IN for xfermode, and then draw our avatar or other pictures. We can also draw our picture first, then draw the circle, but select DST_IN for xfermode. Both can achieve the desired effect. The sample code is as follows:

  1. Paint p = new Paint();
  2. p.setAntiAlias( true ); //anti-aliasing  
  3. p.setColor(Color.BLACK);
  4. p.setStyle(Paint.Style.STROKE);
  5. Canvas canvas = new Canvas(bitmap); //bitmap is our original image, such as avatar  
  6. p.setXfermode( new PorterDuffXfermode(Mode.DST_IN)); //Because we drew the picture first, so DST_IN  
  7. int radius = bitmap.getWidth; //Assume the image is square  
  8. canvas.drawCircle(radius, radius, radius, p); //r=radius, circle center (r, r)  

The above is a simple example. You can actually create more effects based on the above 16 modes. In addition, as long as you give us an intersecting graph, our graph can be displayed in the same shape as the graph.

Achieve graphics of specified shapes by clipping the canvas area

Canvas in Android provides methods such as ClipPath, ClipRect, ClipRegion for clipping. Through different combinations of Path, Rect, and Region, Android can support clipping regions of almost any shape. Therefore, we can obtain a region of almost any shape, and then draw on this region to get the image we want. Let's take a look at the example.

  1. int radius = src.getWidth() / 2 ; //src is the image we want to draw, the same as the bitmap in the previous 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, radius, radius, Path.Direction.CW);
  9. c.clipPath(path); //Clipping area  
  10. c.drawBitmap(src, 0 , 0 , paint); //Draw the picture  

Using BitmapShader

Let’s look at the examples 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 renderer of the brush. In essence, this method is actually drawing a circle, but the rendering uses our picture, and then we 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 the first method. At the same time, setting Shader.TileMode.MIRROR can also achieve a mirror effect, which is also excellent.

The above are three methods of implementation. All three methods can draw a lot of shapes. Of course, if you encounter very very very very complex situations, I recommend using the first method. At this time, you can ask the artist to give you a last-shift shape diagram, saving yourself the trouble of drawing it with code. You can choose according to 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, please reply and tell me ^_^.

Original address: Several ways to draw circular pictures in Android

<<:  Programmer skills get: some thoughts and understanding on code naming

>>:  Unveiling WeChat Enterprise Accounts: The Mobile Application Portal for Enterprise Customers

Recommend

Can the dog that has completed the experiment be happy if it is adopted?

Recently, a celebrity couple adopted a beagle and...

User operation: How to conduct user behavior analysis?

Many students are most afraid of doing open-ended...

How to write excellent promotional copy?

For many operations and promotion personnel, crea...

Uncle Love's bedroom skills, practical strategies

Uncle Qing's bedroom skills, practical strate...

Copywriting Basics Series | How to write logical copywriting?

Do you need logical thinking when writing copy ? ...

User fission growth tips!

There is an old saying: Three incompetent general...

LG is being criticized for withdrawing from China. What is the problem?

Since the beginning of this year, LG Electronics,...

It disappeared for 40 years and was named "extinct", but it recently reappeared!

In the past two weeks, we have collected these fr...

How did the iPhone 6 break the record by pre-selling 4 million units in one day?

Apple said in a statement last Friday that the iP...