Android_刮刮樂刮獎

來源:互聯網
上載者:User

Android_刮刮樂刮獎
該功能實現很簡單,就是根據手指座標位置在畫板上畫1.


2.代碼實現

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);}@Overrideprotected 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;}@Overridepublic 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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.