An essential part of Android development is the rounded corners of various images, masks, and so on.
I used to deal with shape. Discover that the rounded corners of the background map are set to the same background as the interface's parent element.
When the background is changeable, for example, the color of the ListView interlaced background is depressed. Add a bunch of code processing.
Now there is a way to do this uniformly, using canvas, code and methods such as the following:
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 attribute PorterDuff.Mode.DST_IN for this mask image. That means. The picture that overlaps the image shows the following picture.
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 repeated portions, 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= "normal picture below"/> <!--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=" below to turn the mask picture into a rounded picture "/> <!--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>
Note the namespaces that are added 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 pictures and mask pictures, the principle must see the following two pictures are clear.
Processing of wirelessly mask pictures