自定义imageView使用遮罩实现
来源:互联网 发布:吉林大学网络课程中心 编辑:程序博客网 时间:2024/05/29 12:39
package com.example.mask;
import android.widget.ImageView;
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.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
public class MaskImage extends ImageView{
private static final String TAG = "MaskImage";
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 maskRes = BitmapFactory.decodeResource(getResources(), mMaskSource);
// Options opts=new Options();
// opts.inJustDecodeBounds=true;
// Bitmap maskRess = BitmapFactory.decodeResource(getResources(), mMaskSource,opts);
// float scare=1;
// int height=opts.outHeight;
// int width=opts.outWidth;
// float scareX=original.getWidth()/width;
// float scareY=original.getHeight()/height;
// scare=scareX>=scareY?scareX:scareY;
// opts.inSampleSize=(int) scare+1;
// opts.inJustDecodeBounds=false;
// Bitmap mask = BitmapFactory.decodeResource(getResources(), mMaskSource,opts);
int width=maskRes.getWidth();
int height=maskRes.getHeight();
int newWidth=original.getWidth();
int newHight=original.getHeight();
Matrix m=new Matrix();
m.postScale((float)newWidth/width, (float)newHight/height);
Bitmap mask = Bitmap.createBitmap(maskRes, 0, 0, width, height, m, false);
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.FIT_XY);
typedArray.recycle();
}
}
自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MaskImage">
<attr name="image" format="reference" />
<attr name="mask" format="reference" />
</declare-styleable>
</resources>
附常用工具转载
1、Drawable → Bitmap
public
static
Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas =
new
Canvas(bitmap);
// canvas.setBitmap(bitmap);
drawable.setBounds(
0
,
0
, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return
bitmap;
}
2、从资源中获取Bitmap
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
private
byte
[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos =
new
ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG,
100
, baos);
return
baos.toByteArray();
}
4、byte[] → Bitmap
private
Bitmap Bytes2Bimap(
byte
[] b){
if
(b.length!=
0
){
return
BitmapFactory.decodeByteArray(b,
0
, b.length);
}
else
{
return
null
;
}
}
static
boolean
saveBitmap2file(Bitmap bmp,String filename){
CompressFormat format= Bitmap.CompressFormat.JPEG;
int
quality =
100
;
OutputStream stream =
null
;
try
{
stream =
new
FileOutputStream(
"/sdcard/"
+ filename);
}
catch
(FileNotFoundException e) {
// TODO Auto-generated catch block
Generated by Foxit PDF Creator © Foxit Software
http:
//www.foxitsoftware.com For evaluation only.
e.printStackTrace();
}
return
bmp.compress(format, quality, stream);
}
6、将图片按自己的要求缩放
// 图片源
Bitmap bm = BitmapFactory.decodeStream(getResources()
.openRawResource(R.drawable.dog));
// 获得图片的宽高
int
width = bm.getWidth();
int
height = bm.getHeight();
// 设置想要的大小
int
newWidth =
320
;
int
newHeight =
480
;
// 计算缩放比例
float
scaleWidth = ((
float
) newWidth) / width;
float
scaleHeight = ((
float
) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix =
new
Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm,
0
,
0
, width, height, matrix,
true
);
// 放在画布上
canvas.drawBitmap(newbm,
0
,
0
, paint);
7:File图片转Bitmap
Bitmap bt = BitmapFactory.decodeFile(
"/sdcard/myImage/"
+
"head.jpg"
);
//图片地址
8://图片转Bitmap
public
Bitmap drawableToBitamp(
int
drawableResource) {<span style=
"white-space:pre"
> </span>
//可以取raw里面的资源
BitmapFactory.Options opt =
new
BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable =
true
;
opt.inInputShareable =
true
;
InputStream is =
this
.getResources().openRawResource(drawableResource);
BitmapFactory.decodeStream(is,
null
, opt);
return
BitmapFactory.decodeStream(is,
null
, opt);
- 自定义imageView使用遮罩实现
- 自定义ImageView实现
- 自定义ImageView实现圆形图片
- 自定义ImageView圆图实现
- 自定义ImageView实现圆形头像
- 自定义ImageView实现圆形图像
- 自定义ImageView实现圆形头像
- 自定义ImageView实现圆形图片
- 自定义ImageView实现圆角
- Android实现自定义圆形ImageView
- Android:通过自定义ImageView实现带边框的ImageView
- 自定义imageview,实现多点缩放回弹
- android 自定义imageview实现 图片放大缩小
- Android:自定义ImageView实现缩放,回弹效果
- android自定义ImageView实现圆形图片
- 自定义ImageView实现圆角矩形
- 自定义控件实现imageview的点击效果
- 自定义ImageView实现播放帧动画
- 怎么实现MindMapper中的主题更改
- Mybatis自定义主键增长的一种方式
- 模拟数据库连接池
- 使用CocoaPods后找不到头文件
- java equals重写
- 自定义imageView使用遮罩实现
- 更正:关于UFT12.51破解事宜
- git常用指令
- 微信JS分享实战代码
- Putty Command-Line Parameter
- 每天一点python -- python字典排序
- 科普dip,dp,sp,dpi,ppi,px,Density
- Ubuntu下编译Openpilot
- PowerShell 之 DNS Server (1)