Scenario Reproduction:
Write Android program also for some time, today suddenly asked how to implement a circular picture, many app images are round. But the user uploads the image is not round, so the problem comes, need our code to implement a circular picture. But the brain flew to think of third-party image caching tools Imageloader. But this can only set rounded corners, it is difficult to set the implementation rules of the circular picture. And then want to only paint and canvas to draw, but can only draw a circle, and finally how to make the circle filler is a problem. He was not able to answer his question at last. This time is shy, used to have only rounded corners of the picture, did not get this round picture ah. So I checked the information at night.
Realize:
/** * draw a circular picture based on the original and the length of the change * * @param source * @param m In * @return */private Bitmap Createcircleimage (Bitmap source, int min) { Final Paint paint = new paint (); Paint.setantialias (TRUE); Bitmap target = bitmap.createbitmap (min, Min, config.argb_8888); /** * produce a canvas of the same size * * canvas canvas = new canvas (target); /** * First Draw round */canvas.drawcircle (MIN/2, MIN/2, MIN/2, paint); /** * Use src_in */Paint.setxfermode (new Porterduffxfermode (PorterDuff.Mode.SRC_IN)) ; /** * Draw Picture */canvas.drawbitmap (source, 0, 0, paint); return target; }
In fact, mainly by: Paint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.SRC_IN)); This line of code, for what, I explained to you, src_in this model, Two draw the effect overlay after the intersection to show the image, that is, first paint in the canvas to draw a circle, and then with the image to the circle overlay. The intersection gets a circle and fills the circle with the picture. How to say, we first draw is a circle, the second draw is a bitmap, so the intersection of the circle, showing the bitmap, the realization of the circular picture effect.
Android realizes round picture