安卓中遮罩图片的处理
来源:互联网 发布:数据库系统实现 公开课 编辑:程序博客网 时间:2024/05/28 11:48
安卓开发中不可缺少的就是各种图片的圆角,遮罩等等。
以前我都是用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"
下面附上使用到的图片和遮罩图片,原理想必大家看到下面两个图片就都明白了。
1 0
- 安卓中遮罩图片的处理
- 处理图片的CFC
- Java的图片处理
- 图片透明度的处理
- 图片处理的类
- Java图片的处理
- 常用的图片处理
- 图片处理的方法
- ios图片的处理
- 图片的颜色处理
- Android_Bitmap_图片的处理
- SDWebImage图片的处理
- Bitmap图片的处理
- 图片的处理
- 处理图片的总结
- 图片的剪裁处理
- 图片的处理
- Android_图片的处理
- Codeforces Round #261 (Div. 2) C. Pashmak and Buses
- IOS 屏幕旋转
- HBase迁移数据方案(两个集群不能通信)
- java基础内容(1)
- GridView导出Excel的超好例子
- 安卓中遮罩图片的处理
- NTP
- 模拟量I/O与数字量I/O有什么区别?
- 【quick-cocos2d-x】开发工具sublime text及其强力插件QuickXDev
- poj题目分类
- Longest Substring Without Repeating Characters
- 整理了一个多数据库通用操作类
- Sublime设置特殊缩进
- Git Push 避免用户名和密码方法