效果如下图:
这里需要用到上一节中PorterDuffXfermode(把传递改头换面的服务员) xfermode
内容请参考http://note.youdao.com/share/?id=3f61125240ed4553df90532d42192ccb&type=note
刮刮需要用到的是Xor的效果。
刮刮卡的自定义view
public class GuaGuaView extends View{
private Bitmap mBitmap;
private Paint mPaintRect;
private Paint mPaintLine;
private int width;
private int height;
private Canvas mCanvasBitmap;
private Bitmap mBitMapBackground;
private float x;
private float y;
private Path mPhth;
private int mLastX;
private int mLastY;
public GuaGuaView(Context context) {
super(context);
}
public GuaGuaView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.GuaGuaView);
int paintwidth = a.getDimensionPixelOffset(R.styleable.GuaGuaView_guaguaview_paintwidth, 30);
BitmapDrawable dra = (BitmapDrawable) a.getDrawable(R.styleable.GuaGuaView_guaguaview_background);
if(dra!=null){
mBitMapBackground = dra.getBitmap();
}else {
mBitMapBackground = BitmapFactory.decodeResource(getResources(), R.mipmap.aa);
}
mPaintRect = new Paint();
mPaintRect.setColor(Color.GREEN);
mPaintLine = new Paint();
mPaintLine.setColor(Color.YELLOW);
mPaintLine.setStrokeWidth(paintwidth);
PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.XOR);
mPaintLine.setAntiAlias(true);
mPaintLine.setDither(true);
mPaintLine.setStyle(Paint.Style.STROKE);
mPaintLine.setStrokeJoin(Paint.Join.ROUND); // 圆角 连接点使用的样式
mPaintLine.setStrokeCap(Paint.Cap.ROUND); // 圆角 覆盖时使用的样式
mPaintLine.setXfermode(xfermode);
mPhth= new Path();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();
switch (action)
{
case MotionEvent.ACTION_DOWN:
mLastX = x;
mLastY = y;
mPhth.moveTo(mLastX, mLastY);
break;
case MotionEvent.ACTION_MOVE:
int dx = Math.abs(x - mLastX);
int dy = Math.abs(y - mLastY);
if (dx > 5 || dy > 5)
mPhth.lineTo(x, y);
mLastX = x;
mLastY = y;
break;
}
invalidate();
return true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = mBitMapBackground.getWidth();
height = mBitMapBackground.getHeight();
mBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
setMeasuredDimension(width, height); //设置view的宽和高
mCanvasBitmap = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mBitMapBackground, new Rect(0, 0, mBitMapBackground.getWidth(), mBitMapBackground.getHeight()),
new Rect(0, 0, mBitMapBackground.getWidth(), mBitMapBackground.getHeight()), null);
mCanvasBitmap.drawRect(0, 0, mBitMapBackground.getWidth(), mBitMapBackground.getHeight(), mPaintRect);
mCanvasBitmap.drawPath(mPhth,mPaintLine);
canvas.drawBitmap(mBitmap,0,0,null);
}
}
如何保存Bitmap
mButtonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mGuaGuaView.setDrawingCacheEnabled(true);
Bitmap bitmap = mGuaGuaView.getDrawingCache(true);
File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
if (!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});