Android之遮罩功能的实现
上午有个哥们问我android有没有获取圆形图片的方法,就是要把正方形的图片变成圆形的来显示。最近也有再搞flash,所以第一反应就是遮罩,android里面自己也没搞过。google搜索“android mask“挺多资料的,这里把例子与大家分享。先上效果图:[attach]250312[/attach]
好了现在开始代码部分,这里使用了自定义组件以及自定义属性的方式来进行编码[align=center]。[/align]
1、添加资源文件:attrs.xml[code]<?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>
[/code]2、创建自定义组件MaskImage.java[code]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;
import com.xzw.mask.R;
public class MaskImage extends ImageView{
int mImageSource=0;
int mMaskSource=0;
RuntimeException mException;
public MaskImage(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MaskImage, 0, 0);
mImageSource = typedArray.getResourceId(R.styleable.MaskImage_image, 0);
mMaskSource = typedArray.getResourceId(R.styleable.MaskImage_mask, 0);
if (mImageSource == 0 || mMaskSource == 0) {
mException = new IllegalArgumentException(typedArray.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);
typedArray.recycle();
}
}
[/code]3、在布局文件中添加[code]<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>[/code]这里要记得添加namespace也就是xml的命名空间[code]xmlns:maskimage="http://schemas.android.com/apk/res/com.xzw.mask"[/code]xmlns:maskimage的自我们自定义的命名空间方式。
命名规则是:[align=center]http://schemas.android.com/apk/res/包名[/align]
[align=center][align=left]这样代码就实现了遮罩效果了。说了这么多什么是遮罩还没说呢?就是有两张图片,一张图片放在另外一张图片的上面,放在上面的图片叫做遮罩层,下面的叫做被遮罩层,两张图片重叠的部分会显示出来,就形成了遮罩的效果。遮罩用在哪里了,就比如说头像,头像原来是正方形,突然想变成圆形的,这样搞就很简单的实现了。[/align][/align]
[align=center][align=left] 嘿嘿希望这个功能对大家有帮助哈。[/align][/align] 源码:[attach]250313[/attach]
[align=center][align=left] 参考:[align=center]http://stackoverflow.com/questions/12614542/maskingcrop-image-in-frame[/align][/align][/align]
[url=http://lipeng88213.iteye.com/blog/1189452]http://lipeng88213.iteye.com/blog/1189452[/url]