Xfermode to achieve rounded rectangles or rounded corners picture

Source: Internet
Author: User

Recently learned a lot about rounded rectangles or rounded pictures of the article, writing is very good, but every time a learning will do, but over time and do not remember how to write code, reflect on a bit, is that they are just looking at, and no real digestion, so not counting their own things, So today, the code of the Great God looked again, his summary, and then write again, it is understood, but also want to write a record, convenient next time to find, can also be shared.

Xfermode is an android brush paint can be set a brush property, in particular, you can combine the two pictures, according to the form of the set, you can combine a variety of forms, specific people look at the picture:

Here is the srcin mode, the general idea is to draw a circle or rounded rectangle, and then draw our picture, and then according to the above Srcin mode, the final draw a circular picture or rounded rectangle. In Android, the first pictures are taken as SRC, and then DST is drawn.

The specific code is as follows, the inside said very detailed.

Package Com.fanxl.roundview;import Android.annotation.suppresslint;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmap.config;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.paint;import Android.graphics.porterduff.mode;import Android.graphics.porterduffxfermode;import Android.graphics.rectf;import Android.util.attributeset;import  Android.view.view;public class Roundview extends View {private Bitmap src;private Bitmap out;private int width;private int  height;private int type;  private static final int type_circle = 0; private static final int type_round = 1; Public Roundview (Context context) {This (context, null);} Public Roundview (context context, AttributeSet Attrs) {This (context, attrs, 0);} Public Roundview (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); Initview () ;} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {Super.onmeaSure (Widthmeasurespec, heightmeasurespec);//The width-height int widthsize = measurespec.getsize (widthmeasurespec) of the calculated control; int Widthmode = Measurespec.getmode (widthmeasurespec); int heightsize = Measurespec.getsize (heightmeasurespec); int Heightmode = Measurespec.getmode (Heightmeasurespec), if (Widthmode = = measurespec.exactly) {width = widthsize;} else {int ImgWidth = src.getwidth () + getpaddingleft () + getpaddingright (); if (Widthmode = = measurespec.at_most) {width = Math.min (w Idthsize, imgwidth);} else {width = imgwidth;}} if (Heightmode = = measurespec.exactly) {height = heightsize;} else {int imgheight = Src.getheight () + getpaddingtop () + Get Paddingbottom (); if (Heightmode = = measurespec.at_most) {height = math.min (heightsize, imgheight);} else {height = Imgheig HT;}} Sets the final custom control's wide-height switch (type) {case Type_circle:int min = math.min (width, height) according to the type to be drawn; Setmeasureddimension (min, min) ; break;case type_round:setmeasureddimension (width, height); break;}} @SuppressLint ("Newapi") private void Initview () {//Disable hardware acceleration, there are some problems with hardware acceleration, which disables setlayertype (layer_type_software, null); src = Bitmapfactory.decoderesource (getresources (), R.DRAWABLE.DD);//set type, rounded picture or rounded rectangle type = type_circle;} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); Xmodeimage ();// Place the painted canvas on top of the custom artboard Canvas.drawbitmap (out, 0, 0, null);} private void Xmodeimage () {//Create a canvas based on the original picture out = Bitmap.createbitmap (width, height, config.argb_8888);//Create an artboard, Canvas based on canvases = new canvas (out);//Create a brush paint paint = new paint (paint.anti_alias_flag); switch (type) {case Type_ro UND://began to paint with a paintbrush on a canvas with an artboard, where a rounded rectangle was drawn canvas.drawroundrect (new RECTF (0, 0, width, height), max, Max, paint); Break;case type_ circle://Draw a circle, take the minimum value of the width high as the diameter of the circle int min = math.min (width, height);//Start Drawing round canvas.drawcircle (MIN/2, MIN/2, MIN/2, paint); Set Xfermode brush mode to Src_inpaint.setxfermode (new Porterduffxfermode (mode.src_in));//Then there is a picture drawn, Finally, the overlay of two images is realized Canvas.drawbitmap (src, 0, 0, paint);}}

Final effect:



Xfermode to achieve rounded rectangles or rounded corners 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.