需求
刮開浮層顯示獎品
沒有中獎:顯示遺憾沒有中獎
中獎:顯示獎品名字
刮開后與后臺進行數(shù)據(jù)交互,告知后臺是否中獎
原理
畫浮層,然后利用Paint可以與現(xiàn)有cancve進行交互,來實現(xiàn)擦除浮層的效果
paint.setXfermode(Xfermode object);
代碼
package com.treevc.lixiaoxuedemo;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Xfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by xxli on 10/9/16.
*/
public class ScratchView extends View {
private Paint mashPaint;
private Paint eraserPaint;
private Canvas mMashCanvas;
private Bitmap mBitmap;
private float mStartX;
private float mStartY;
private Path mPath;
public ScratchView(Context context) {
super(context);
init(null);
}
public ScratchView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ScratchView);
init(array);
}
public ScratchView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ScratchView);
init(array);
}
public ScratchView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
this(context, attrs, defStyleAttr);
}
private void init(TypedArray array) {
if(array != null){
}
mashPaint = new Paint();
mashPaint.setAntiAlias(true);
mashPaint.setColor(Color.GRAY);
mashPaint.setStyle(Paint.Style.FILL);
eraserPaint = new Paint();
eraserPaint.setAntiAlias(true);
eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
eraserPaint.setColor(Color.RED);
eraserPaint.setStrokeCap(Paint.Cap.ROUND);//設置筆尖形狀,讓繪制的邊緣圓滑
eraserPaint.setStyle(Paint.Style.STROKE); //必須指定,不然畫path畫不了
eraserPaint.setStrokeWidth(20);
mPath = new Path();
createMashBitmap();
}
private void createMashBitmap() {
mBitmap = Bitmap.createBitmap(mashBitMap.getWidth(), mashBitMap.getHeight(), Bitmap.Config.ARGB_8888);
mMashCanvas = new Canvas(mBitmap);
Rect rect = new Rect(0,0,mashBitMap.getWidth(),mashBitMap.getHeight());
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
mMashCanvas.drawRect(rect, paint);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mBitmap, 0, 0, mashPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
mStartX = event.getX();
mStartY = event.getY();
startEraser();
break;
case MotionEvent.ACTION_MOVE:
eraser(event.getX(),event.getY());
invalidate();
break;
case MotionEvent.ACTION_UP:
endEraser();
break;
}
return true;
}
private void endEraser() {
mPath.reset();
}
private void startEraser() {
mPath.moveTo(mStartX,mStartX);
}
private void eraser(float x, float y) {
//TODO: 10/10/16
//滾動了
float dx = Math.abs(x - mStartX);
float dy = Math.abs(y - mStartY);
Log.i("ScratchView", "dx:" + dx + "dy" + dy);
if(dx > 10 || dy > 10){
mStartY = y;
mStartX = x;
mPath.lineTo(x,y);
mMashCanvas.drawPath(mPath,eraserPaint);
mPath.moveTo(mStartX,mStartY);
}
}
}
在進一步,圖層樣子不是單純的顏色,畫圖片就好
private void createMashBitmap() {
mashBitMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_member_vertificate);
mBitmap = Bitmap.createBitmap(mashBitMap.getWidth(), mashBitMap.getHeight(), Bitmap.Config.ARGB_8888);
mMashCanvas = new Canvas(mBitmap);
Rect rect = new Rect(0,0,mashBitMap.getWidth(),mashBitMap.getHeight());
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
mMashCanvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_member_vertificate),0,0,paint);
}