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

How are video ads delivered? Operation process!

As netizens' reading habits become increasing...

IDC: Global AI market size is expected to reach US$88.57 billion in 2021

Artificial intelligence (AI) technology has broad...

Apple developers have no choice: iOS 9 is expected to support iPhone 4S

[[135011]] According to Apple's previous soft...

On the winter solstice, who can resist a bowl of hot dumplings?

The Winter Solstice is not only an important sola...

How long can African kids continue to use signboards to market?

Recently, a video of African children holding sig...

Product operation: Let users share the deep logic of fission!

A good product will make users want to share, spo...

WeChat is working hard to update three major system update content highlights

The latest WeChat has started to make progress an...

Enterprise new media operation methods and skills!

What is the core reason why companies fail to do ...

These 11 bad habits are disease "catalysts"! How many of them are you among?

The emergence of diseases is sometimes closely re...

Tingyun CTO: AWS and Tingyun join hands to create a domestic cloud + APM model

[[124682]] Recently, the AWS Technology Summit wa...