您的位置:首页 > Web前端

PorterDuffXfermode实现刮刮卡效果

2017-03-21 17:25 267 查看
//自定义view继承view

public class XfermodeView extends View {

private Context context;
private Path path;
private Canvas mcanvas;
private Paint mpanit;
private Bitmap bgBitmap;
private Bitmap fgBitmap;

public XfermodeView(Context context) {
this(context, null);
}

public XfermodeView(Context context, AttributeSet attrs) {
super(context, attrs);
// this.context=context;
initView();
}

private void initView() {
mpanit = new Paint();
mpanit.setAlpha(0);//透明度设置为0显示檫除效果,
//portduff进行图层混合时也会计算透明通道的值,混合后会显示底层的图片
mpanit.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
//使用dis_in模式将路径绘制到到前面覆盖的图层上面
mpanit.setStyle(Paint.Style.STROKE);
mpanit.setStrokeWidth(50);
mpanit.setStrokeCap(Paint.Cap.ROUND);//让笔触连接处圆滑
mpanit.setStrokeJoin(Paint.Join.ROUND);//让笔触连接处圆滑
path = new Path();
bgBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.lock_screen_houma);//设置背景图片
fgBitmap = Bitmap.createBitmap(bgBitmap.getWidth(), bgBitmap.getHeight(), Bitmap.Config.ARGB_8888);
mcanvas = new Canvas(fgBitmap);//根据背景图片绘制灰色区域
mcanvas.drawColor(Color.GRAY);//设置画布颜色
}

@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.reset();
path.moveTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
path.lineTo(event.getX(), event.getY());//绘制线条
break;

}
mcanvas.drawPath(path, mpanit);
invalidate();
return true;
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bgBitmap,0,0,null);//默认画笔绘制背景
canvas.drawBitmap(fgBitmap,0,0,null);//绘制灰色区域

}


}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: