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:
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.
Using BitmapShader Let’s look at the examples first
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
The wedding industry is becoming more and more po...
Using Laravel to build high-performance enterpris...
In March last year, the Ministry of Human Resourc...
Recently, advertisers of e-commerce apps seem to ...
In recent years, shared bikes have become popular...
For 2B operators, attracting new customers means ...
The topic "Five reasons why you won't wa...
Since the first generation of iPhone changed the ...
Smoked bacon, air-dried sausages, a jar of pickle...
Beijing time, September 17th morning news, Apple ...
When it comes to shrimp, the first thing that com...
Only truly active users can generate value and pr...
In fact, most of the Xiaohongshu merchants are mo...
Operations are basically a process of constantly ...
According to the China Manned Space Engineering O...