</pre><pre code_snippet_id="411661" snippet_file_name="blog_20140630_1_1574839" name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;">public class MaskImage extends ImageView {</span>
int screenWidth = 0;
int screenHeight = 0;
Bitmap original = null;
TypedArray a;
boolean sendMessage = false;
int maskSoure = 0;
int maskClickSource = 0;
private boolean clickImage = false; // 是否点击图片
int defaultImgRes = 0;
public MaskImage(Context context) {
super(context);
}
public MaskImage(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MaskImage(Context context, AttributeSet attrs) {
super(context, attrs);
a = context.getTheme().obtainStyledAttributes(
attrs,
AndroidUtil.getResourceDeclareStyleableIntArray(context,
"ImImageView"), 0, 0);
sendMessage = a.getBoolean(AndroidUtil.getStyleableResourceInt(context,
"ImImageView_sendMessage"), false);
defaultImgRes = a.getResourceId(AndroidUtil.getStyleableResourceInt(
context, "ImImageView_defaultImgRes"), 0);
}
public void sendOrFrom() {
if (sendMessage) {
maskSoure = R.drawable.bg_msg_pop_orange;
maskClickSource = R.drawable.jkt_chat_mytextbg_hig_active;
} else {
maskSoure = R.drawable.bg_msg_pop_gray;
maskClickSource = R.drawable.jkt_chat_textbg_hig_active;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
clickImage = true;
invalidate();
Log.d("debug", "on down");
}
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.d("debug", "on up");
clickImage = false;
invalidate();
}
if (event.getAction() == MotionEvent.ACTION_CANCEL) {
Log.d("debug", "on cancel");
clickImage = false;
invalidate();
}
return super.onTouchEvent(event);
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
// 判断图片是否为空
if (drawable != null && drawable instanceof BitmapDrawable) {
original = ((BitmapDrawable) drawable).getBitmap();
// 转换为Bitmap的是否为Null
if (original != null) {
if (original.getWidth() != this.getWidth()
|| original.getHeight() != this.getHeight()) {
original = zoomImg(original, this.getWidth(), this.getHeight());
}
int x = 0;
int y = 0;
int sc = canvas.saveLayer(x, y, x + this.getWidth(),
y + this.getHeight(), null, Canvas.MATRIX_SAVE_FLAG
| Canvas.CLIP_SAVE_FLAG
| Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
| Canvas.FULL_COLOR_LAYER_SAVE_FLAG
| Canvas.CLIP_TO_LAYER_SAVE_FLAG);
Rect rf = new Rect(0, 0, this.getWidth(), this.getHeight());
// 显示模型样式 发送遮罩 接受遮罩
sendOrFrom();
// .9图片改变大小
NinePatchDrawable npd = (NinePatchDrawable) getResources()
.getDrawable(maskSoure);
npd.setBounds(rf);
// 将遮罩图片绘制到画板
npd.draw(canvas);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// 图片交接处显示之前的样式
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(original, 0, 0, paint);
// 图片点击 添加一层图片 实现点击图片切换样式
if (clickImage) {
paint.setXfermode(new PorterDuffXfermode(
PorterDuff.Mode.XOR));
NinePatchDrawable click = (NinePatchDrawable) getResources()
.getDrawable(maskClickSource);
click.setBounds(rf);
click.draw(canvas);
}
canvas.restoreToCount(sc);
}
}
if (a != null) {
a.recycle();
}
}
// 缩放图片
public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
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);
return newbm;
}
}
Demo 源码地址:https://github.com/zhaocheng19940105/MaskImage