Android realizes round picture

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.