Examples of two ways Android draws circular pictures
Two ways for Android to draw circular pictures
Look at the effect first
Here is the complete sample code
Use bitmapshader (shader)
When we draw the view, we have an art class in primary school. We use a watercolor pen to draw on the book and use a shader to draw circular pictures. The simplest way to understand is to set bitmap as a color to paint. Paint already has color. You want it to be square, round and flat. It depends on your mood. Canvas calls that method
The general idea of implementation is as follows:
1. Create a class to inherit ImageView and override ondraw()
2. Get the bitmap image
3. Calculate the scale of the picture and use the matrix to scale
4. Create bitmapshader shader and set the scaling matrix
5. Paint setting shader painting
The specific implementation notes are also clearly marked
Use xfermode to set the picture intersection mode
To put it simply, two pictures are drawn on a canvas. How are the two pictures displayed? For example, only the upper picture, only the lower picture, the intersection of the two pictures, etc
Realization idea
1. Create an empty bitmap and create a canvas based on this bitmap
2. Set canvas transparency to draw a desired shape
3. Set graph intersection mode
4. Get picture resources and draw them to canvas
Implementation code
This particularly classic picture
Reference documents
Inheriting ImageView to complete the implementation of circular and rounded image controls (using shaders)
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.