301
技術社區[雲棲]
安卓中遮罩圖片的處理
安卓開發中不可缺少的就是各種圖片的圓角,遮罩等等。
以前我都是用shape處理的,發現背景圖的圓角要設置成和界麵父元素背景一樣才能看不出現紕漏。
當遇到背景多變的情況,比如listview隔行背景顏色不同的情況就鬱悶了,又要加一堆代碼處理。
現在有一個方法可以統一處理,就是用畫布Canvas,代碼和方法如下:
方法步驟說明:
1、畫布Canvas
2、在畫布上把要顯示的圖畫上去
3、把遮罩圖片畫上去,蓋在要顯示的圖片上麵
4、對這個遮罩圖片設置一個很重要的屬性PorterDuff.Mode.DST_IN,意思就是,圖片重疊的部分顯示下麵的圖片。
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); mCanvas.drawBitmap(mask, 0, 0, paint);
實現代碼:
1、配置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、創建一個MaskImage類繼承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 required and must refer to a valid image.");
}
if (mException != null)
throw mException;
/**
* 主要代碼實現
*/
//獲取圖片的資源文件
Bitmap original = BitmapFactory.decodeResource(getResources(), mImageSource);
//獲取遮罩層圖片
Bitmap mask = BitmapFactory.decodeResource(getResources(), mMaskSource);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
//將遮罩層的圖片放到畫布中
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));//疊加重複的部分,顯示下麵的
mCanvas.drawBitmap(original, 0, 0, null);
mCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
setImageBitmap(result);
setScaleType(ScaleType.CENTER);
a.recycle();
}
}
3、在layout布局文件中使用
<LinearLayout xmlns:andro
xmlns:maskimage="https://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="以下為正常圖片" />
<!-- 無遮罩圖片 -->
<ImageView
android:
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="以下為遮罩圖片變成圓形的圖片" />
<!-- 有遮罩圖片 -->
<com.xzw.mask.widget.MaskImage
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
maskimage:image="@drawable/icon_t"
maskimage:mask="@drawable/circle" />
</LinearLayout>注意布局中間中增加的命名空間:xmlns:maskimage="https://schemas.android.com/apk/res/com.xzw.mask"
下麵附上使用到的圖片和遮罩圖片,原理想必大家看到下麵兩個圖片就都明白了。


最後更新:2017-04-03 05:39:56