安卓开发中不可缺少的就是各种图片的圆角,遮罩等等。
以前我都是用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: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="以下为正常图片" />
<!-- 无遮罩图片 -->
<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="以下为遮罩图片变成圆形的图片" />
<!-- 有遮罩图片 -->
<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>
注意布局中间中增加的命名空间:xmlns:maskimage="http://schemas.android.com/apk/res/com.xzw.mask"
下面附上使用到的图片和遮罩图片,原理想必大家看到下面两个图片就都明白了。