刮刮卡

刮刮卡_第1张图片

package com.example.zz.view;

import com.example.zz.R;

import android.content.Context;
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.Bitmap.Config;
import android.graphics.Paint.Align;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
 * 一个类似刮刮卡的ui,其中宽高使用的的使用的是刮刮卡遮挡图片的宽高, 其实我们可以根据view自己设置的宽高来控制图片的显示 Canvas.drawBitmap(Bitmap bitmap,Rect src,Rect dst,Paint paint);
 * 1. 使用PorterDuffXfermode(Mode.DST_OUT) 和 path 来实现bitmap擦出效果
 * 2. 获取图片擦出的像素占总像素的统计
 * 3. 是text显示的内容居中显示, paint.decent() 和 paint.acent();
 *
 */
public class GuaGuaKa extends View {

	private Paint mPaint;
	private Path mPath;
	private Bitmap mBitmap;
	private Canvas c;
	private Paint mTxtPaint;
	private Bitmap bitmap;
	private String text = "$100,000,000";
	private Paint mOutterPaint;
	private float d;

	public GuaGuaKa(Context context, AttributeSet attrs) {
		super(context, attrs);
		mTxtPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
		mTxtPaint.setColor(Color.BLACK);
		mTxtPaint.setTextSize(60);
		mTxtPaint.setStyle(Style.STROKE);
		mTxtPaint.setTextScaleX(1.5F);
		mTxtPaint.setTextAlign(Align.CENTER);
		float ascent = mTxtPaint.ascent();
		float descent = mTxtPaint.descent();
		d = (descent + ascent) / 2;
		
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
		mPaint.setStrokeCap(Cap.ROUND);
		mPaint.setStyle(Style.STROKE);
		mPaint.setStrokeWidth(20);
		mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_OUT));
		
		mOutterPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
		mOutterPaint.setStyle(Style.FILL);
		mOutterPaint.setColor(Color.parseColor("#aaaaaa"));
		
		mPath = new Path();
		mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.s_title);
		bitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Config.ARGB_8888);
		c = new Canvas(bitmap);
		c.drawRoundRect(new RectF(0, 0, mBitmap.getWidth(), mBitmap.getHeight()), 30, 30, mOutterPaint);
		c.drawBitmap(mBitmap, 0, 0, null);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int width = mBitmap.getWidth();
		int height = mBitmap.getHeight();
		setMeasuredDimension(width,height);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		mOutterPaint.setColor(Color.GRAY);
		canvas.drawRoundRect(new RectF(0, 0, mBitmap.getWidth(), mBitmap.getHeight()), 30, 30, mOutterPaint);
		canvas.drawText(text, getMeasuredWidth() / 2, getMeasuredHeight() / 2 - d, mTxtPaint);
		if(!isComplete){
			doZhegai();
			canvas.drawBitmap(bitmap, 0, 0, null);
		}
	}
	
	private void doZhegai() {
		c.drawPath(mPath, mPaint);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();
		float x = event.getX();
		float y = event.getY();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			float downX = x;
			float downY = y;
			mPath.moveTo(downX, downY);
			return true;
			
		case MotionEvent.ACTION_MOVE:
			float moveX = x;
			float moveY = y;
			mPath.lineTo(moveX, moveY);
			break;
		
		case MotionEvent.ACTION_UP:
			new Thread(mRunnable).start();
			break;
		default:
			break;
		}
		invalidate();
		return super.onTouchEvent(event);
	}
	
	/**
	 * 统计擦除区域任务
	 */
	private boolean isComplete = false;
	private Runnable mRunnable = new Runnable(){
		private int[] mPixels;

		@Override
		public void run(){

			int w = getWidth();
			int h = getHeight();

			float wipeArea = 0;
			float totalArea = w * h;

			Bitmap b = bitmap;

			mPixels = new int[w * h];

			/**
			 * 拿到所有的像素信息
			 */
			b.getPixels(mPixels, 0, w, 0, 0, w, h);

			/**
			 * 遍历统计擦除的区域
			 */
			for (int i = 0; i < w; i++){
				for (int j = 0; j < h; j++){
					int index = i + j * w;
					if (mPixels[index] == 0){
						wipeArea++;
					}
				}
			}
			/**
			 * 根据所占百分比,进行一些操作
			 */
			if (wipeArea > 0 && totalArea > 0){
				int percent = (int) (wipeArea * 100 / totalArea);   
				Log.e("TAG", percent + "");
				if (percent > 70){
					isComplete = true;
					postInvalidate();
				}
			}
		}
	};
}


你可能感兴趣的:(刮刮卡)