[转载] Android之遮罩功能的实现

本文介绍了一种在Android中实现圆形图片显示的方法,通过自定义组件和属性,利用遮罩技术将正方形图片转换为圆形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



转载地址:http://bbs.51cto.com/thread-1031415-1.html  谢谢作者:xuzw13

     上午有个哥们问我android有没有获取圆形图片的方法,就是要把正方形的图片变成圆形的来显示。最近也有再搞flash,所以第一反应就是遮罩,android里面自己也没搞过。google搜索“android mask“挺多资料的,这里把例子与大家分享。先上效果图:

未命名.jpg (21.63 KB)
2013-4-27 12:52


好了现在开始代码部分,这里使用了自定义组件以及自定义属性的方式来进行编码




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.java
复制内容到剪贴板
代码:
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();
        }
}
3、在布局文件中添加
复制内容到剪贴板
代码:
<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>
这里要记得添加namespace也就是xml的命名空间
复制内容到剪贴板
代码:
xmlns:maskimage="http://schemas.android.com/apk/res/com.xzw.mask"
xmlns:maskimage的自我们自定义的命名空间方式。
命名规则是:

http://schemas.android.com/apk/res/包名




这样代码就实现了遮罩效果了。说了这么多什么是遮罩还没说呢?就是有两张图片,一张图片放在另外一张图片的上面,放在上面的图片叫做遮罩层,下面的叫做被遮罩层,两张图片重叠的部分会显示出来,就形成了遮罩的效果。遮罩用在哪里了,就比如说头像,头像原来是正方形,突然想变成圆形的,这样搞就很简单的实现了。


    嘿嘿希望这个功能对大家有帮助哈。

    源码: mask.zip (1.06 MB)
mask.zip (1.06 MB)
下载次数: 2549
2013-4-27 12:54


   参考:

http://stackoverflow.com/questions/12614542/maskingcrop-image-in-frame


http://lipeng88213.iteye.com/blog/1189452
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值