Processing of masked images in Android and masking images in Android
What is indispensable in Android development is the rounded corners and masks of various images.
Previously, I used shape processing. I found that the rounded corner of the background image must be set like the background of the interface parent element.
When the background is changing, for example, if the background color of the listview is different, it will be depressing, and a bunch of code will be added for processing.
Now there is a method that can be processed in a unified manner, that is, Canvas. The code and method are as follows:
Steps:
1. Canvas
2. display the picture on the canvas.
3. Draw a masked image and place it on the image to be displayed.
4. Set a very important attribute PorterDuff. Mode. DST_IN for this masked image, which means that the overlapping part of the image Displays the following image.
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));mCanvas.drawBitmap(mask, 0, 0, paint);
Implementation Code:
1. Configure 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 --> <attr name="mask" format="reference" /> </declare-styleable></resources>
2. Create a MaskImage class to inherit 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. porterduxfermode; import android. util. attributeSet; import android. widget. ima GeView; 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 =. getResourceId (R. styleable. maskImage_image, 0); mMaskSource =. getResourceId (R. styleable. maskImage_mask, 0 ); If (mImageSource = 0 | mMaskSource = 0) {mException = new IllegalArgumentException (. getPositionDescription () + ": The content attribute is required and must refer to a valid image. ");} if (mException! = Null) throw mException;/*** main code implementation * // obtain the image's resource file Bitmap original = BitmapFactory. decodeResource (getResources (), mImageSource); // obtain the Bitmap mask of the mask layer image = BitmapFactory. decodeResource (getResources (), mMaskSource); Bitmap result = Bitmap. createBitmap (mask. getWidth (), mask. getHeight (), Config. ARGB_8888); // place the image of the mask layer in the Canvas mCanvas = new Canvas (result); Paint paint = new Paint (Paint. ANTI_ALIAS_FLAG); paint. setXfermode (new porterduxfermode (PorterDuff. mode. DST_IN); // overlay the duplicate part to display the following mCanvas. drawBitmap (original, 0, 0, null); mCanvas. drawBitmap (mask, 0, 0, paint); paint. setXfermode (null); setImageBitmap (result); setScaleType (ScaleType. CENTER);. recycle ();}}
3. Use it in the 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: layout_height = "wrap_content" android: text = "The following are normal images"/> <! -- Unmasked image --> <ImageView android: id = "@ + id/imageview1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: src = "@ drawable/icon_t"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "The following is an image with a circular mask image"/> <! -- Masked image --> <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 namespace added in the middle of the layout: xmlns: maskimage = "http://schemas.android.com/apk/res/com.xzw.mask"
The following is an example of the image and mask used. The principle must be clear when you see the following two images.
In Android, How can I fix an image on another image and move it on a mobile phone?
Isn't it the function of the graphic editing tool to fix one image on the other? What does it mean to move a fixed-size image?
In Android, how does one display text under an image on the button?
<? Xmlversion = "1.0" encoding = "UTF-8"?>
<TabHostxmlns: android = "schemas.android.com/apk/res/android"
Android: id = "@ android: id/tabhost"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TabWidget
Android: id = "@ android: id/tabs"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
</TabWidget>
<FrameLayout
Android: id = "@ android: id/tabcontent"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView
Android: id = "@ + id/tabCon1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "aaaaa"/>
<TextView
Android: id = "@ + id/tabCon2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content & q ...... full text>