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

New ways to play with WeChat Mini Programs in the wedding industry

The wedding industry is becoming more and more po...

Building high-performance enterprise product architecture with Laravel

Using Laravel to build high-performance enterpris...

The effectiveness and traffic conversion of TikTok advertising in APP!

Recently, advertisers of e-commerce apps seem to ...

6 routines and 3 dimensions for B2B operators to attract new customers!

For 2B operators, attracting new customers means ...

Apple launches Android migration app, leaving no room for Android

Beijing time, September 17th morning news, Apple ...

How does product operation awaken dormant and lost users?

Only truly active users can generate value and pr...

What is the promotion method of Xiaohongshu? How to promote Xiaohongshu?

In fact, most of the Xiaohongshu merchants are mo...

7 aspects that a complete operation plan should include

Operations are basically a process of constantly ...