Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android_刮刮樂刮獎

Android_刮刮樂刮獎

編輯:關於Android編程

該功能實現很簡單,就是根據手指坐標位置在畫板上畫

1.效果圖

\ \

2.代碼實現

<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">public class ErinieShow extends RelativeLayout { Context context; RelativeLayout bg; MyView MyView; int level; public ErinieShow(Context context, int level) { super(context); this.context = context; this.level = level; bg = new RelativeLayout(context); MyView = new MyView(context); bg.addView(MyView); addView(bg); int[] resolution = PhoneUtil.getResolution(context); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( resolution[0], PhoneUtil.getFitHeight(context, 125)); bg.setId(100001); bg.setLayoutParams(params); MyView.setLayoutParams(params); switch (level) { case 0: bg.setBackgroundResource(R.drawable.rewardlevel0); break; case 1: bg.setBackgroundResource(R.drawable.rewardlevel1); break; case 2: bg.setBackgroundResource(R.drawable.rewardlevel2); break; default: bg.setBackgroundResource(R.drawable.rewardlevel3); break; } MyView.init(Color.parseColor("#d3d3d3"), 30, 10); } }

public class MyView extends TextView {

	private float TOUCH_TOLERANCE; // 填充距離,使線條更自然,柔和,值越小,越柔和。
	private Bitmap mBitmap;
	private Canvas mCanvas;
	private Paint mPaint;
	private Path mPath;
	private float mX, mY;
	private boolean isDraw = false;
	int time=0;
	
	public MyView(Context context) {
		super(context);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (isDraw) {
			mCanvas.drawPath(mPath, mPaint);
			canvas.drawBitmap(mBitmap, 0, 0, null);
		}
	}

	/**
	 * 開啟檫除功能
	 * 
	 * @param bgColor
	 *            覆蓋的背景顏色
	 * @param paintStrokeWidth
	 *            觸點(橡皮)寬度
	 * @param touchTolerance
	 *            填充距離,值越小,越柔和。
	 */
	public void init(final int bgColor, final int paintStrokeWidth,
			float touchTolerance) {
		TOUCH_TOLERANCE = touchTolerance;
		// 設置畫筆
		mPaint = new Paint();
		// mPaint.setAlpha(0);
		// 畫筆劃過的痕跡就變成透明色了
		mPaint.setColor(Color.BLACK); // 此處不能為透明色
		mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
		// 或者
		// mPaint.setAlpha(0);
		// mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

		mPaint.setAntiAlias(true);
		mPaint.setDither(true);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND); // 前圓角
		mPaint.setStrokeCap(Paint.Cap.ROUND); // 後圓角
		mPaint.setStrokeWidth(paintStrokeWidth); // 筆寬

		// 痕跡
		mPath = new Path();
		// 覆蓋
		LayoutParams layoutParams = getLayoutParams();
		int height = layoutParams.height;
		int width;
		if (getLayoutParams().width == LayoutParams.MATCH_PARENT) {
			width = 700;
		} else {
			width = layoutParams.width;
		}

		mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		mCanvas = new Canvas(mBitmap);

		mCanvas.drawColor(bgColor);
		isDraw = true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (!isDraw) {
			return true;
		}
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN: // 觸點按下
			// touchDown(event.getRawX(),event.getRawY());
			touchDown(event.getX(), event.getY());
			invalidate();
			break;
		case MotionEvent.ACTION_MOVE: // 觸點移動
			touchMove(event.getX(), event.getY());
			invalidate();
			break;
		case MotionEvent.ACTION_UP: // 觸點彈起
			touchUp(event.getX(), event.getY());
			invalidate();
			break;
		default:
			break;
		}
		return true;
	}

	private void touchDown(float x, float y) {
		mPath.reset();
		mPath.moveTo(x, y);
		mX = x;
		mY = y;
	}

	private void touchMove(float x, float y) {
		float dx = Math.abs(x - mX);
		float dy = Math.abs(y - mY);
		if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
			mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
			mX = x;
			mY = y;
		}

	}

	private void touchUp(float x, float y) {
		mPath.lineTo(x, y);
		mCanvas.drawPath(mPath, mPaint);
		mPath.reset();
	}

}

源碼下載http://download.csdn.net/detail/strawberry2013/7682865


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved