An indispensable part of Android development is the rounded corners of various images, masks and so on.
I used to work with shape, and found that the fillet of the background map should be set to the same background as the parent element of the interface to see no flaws.
When the background is changeable, such as the ListView background color of the different situation is depressed, but also add a bunch of code processing.
Now there is a way to do it uniformly, that is, canvas, the code and methods are as follows:
Method Step Description:
1 canvas
2, on the canvas to display the picture to go up
3. Put the mask picture on top of the picture you want to display
4. Set a very important property PorterDuff.Mode.DST_IN for this mask image, meaning that the image overlaps the part of the picture shown below.
Paint paint = new Paint (paint.anti_alias_flag);p Aint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.DST_IN)); Mcanvas.drawbitmap (mask, 0, 0, paint);
Implementation code:
1, Configuration Attrs.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name= "Maskimage" > <!--Identifier for the image that represents the Imageview's content. -- <attr name= "image" format= "reference"/> <!--crop Imageview ' s content same as mask-- & Lt;attr name= "Mask" format= "reference"/> </declare-styleable></resources>
2. Create a Maskimage class inheritance ImageView
Package Com.xzw.mask.widget;import Android.content.context;import Android.content.res.typedarray;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;import Android.graphics.porterduffxfermode;import Android.util.attributeset;import Android.widget.imageview;public Class Maskimage extends Imageview{int mimagesource=0;int mmasksource=0; RuntimeException mexception;public Maskimage (context context, AttributeSet Attrs) {Super (context, attrs); TypedArray a = GetContext (). Obtainstyledattributes (Attrs, r.styleable.maskimage, 0, 0); Mimagesource = A.getresourceid ( R.styleable.maskimage_image, 0); Mmasksource = A.getresourceid (r.styleable.maskimage_mask, 0); if (MImageSource = = 0 | | Mmasksource = = 0) {mexception = new IllegalArgumentException (a.getpositiondescription () + ": The content attribute is requ Ired and must refer to a valid image. "); if (mexcePtion = null) throw mexception; /** * Main code implementation *///get picture of resource file bitmap original = Bitmapfactory.decoderesource (Getresources (), mimagesource);//Get Mask Overlay picture Bitmap mask = Bitmapfactory.decoderesource (Getresources (), Mmasksource); Bitmap result = Bitmap.createbitmap (Mask.getwidth (), Mask.getheight (), config.argb_8888); Place a picture of the matte layer on canvas Mcanvas = new canvas (result); Paint paint = new Paint (paint.anti_alias_flag);p Aint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.DST_IN));// Overlay repeating section showing the following Mcanvas.drawbitmap (original, 0, 0, NULL), Mcanvas.drawbitmap (mask, 0, 0, paint);p aint.setxfermode (null); Setimagebitmap (result); Setscaletype (Scaletype.center); A.recycle ();}}
3. Using in Layout file
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:maskimage= "http// Schemas.android.com/apk/res/com.xzw.mask "android:layout_width=" match_parent "android:layout_height=" Match_ Parent "android:orientation=" vertical "> <textview android:layout_width=" wrap_content "Android:la yout_height= "Wrap_content" android:text= "following normal picture"/> <!--matte picture-<imageview android:id= "@+id/imageview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" ANDROID:SR c= "@drawable/icon_t"/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_c Ontent "android:text=" The following is a picture of the matte image turned round "/> <!--has a mask picture--<com.xzw.mask.widget.maskimage Android:id= "@+id/imageview_id" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Maskimage:image= "@drawable/icon_t" Maskimage:mask= "@drawable/circle"/></linearlayout>
Notice the added namespace in the middle of the layout: xmlns:maskimage= "Http://schemas.android.com/apk/res/com.xzw.mask"
The following is attached to the use of the picture and mask pictures, the principle must see the following two pictures are understood.