Android and Android scratch cards
I haven't written a blog for a long time. Today, we have just a time. Next we will bring you a scratch project to be used in e-commerce. I believe you have played scratch cards since childhood. At that time, we may use small pieces of paper. Now we can share this scratch project with you, hoping to help you learn and work.
I. go first
Ii. Principle Analysis
1. Create a bitmap size you want, draw a canvas Based on the bitmap size, and then override the onDraw () method.
2. In the continuous expansion of the scratch area, the area of the expansion surface is calculated. When the area exceeds 60%, it is clear that the Layer
3. The following scratch card information can be done using a random number in java. It can be a multiple of 900 or something before winning the prize.
Iii. source code example
Custom Control Code:
Package com. zengtao. view; import android. annotation. suppressLint; import android. content. context; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paint. style; import android. graphics. path; import android. graphics. porterduxfermode; import android. graphics. rect; import android. OS. handler; import android. OS. mes Sage; import android. util. attributeSet; import android. util. log; import android. view. motionEvent; import android. view. view;/***** scratch card ** @ author zengtao April 17, 2015 11:13:46 */@ SuppressLint ("DrawAllocation") public class GuaGuaKa extends View {// ------- 1. private int lastX; private int lastY; private int height = 300; private int width = 120; private int movingX; private int movingY; private Bitmap mB Itmap; private Paint mPaint; private Canvas mCanvas; private Path mPath; // -------- 2. private String mText; private Paint mTextPaint; private int mTextSize; private Rect mRectBounds; // width and height of the obtained scratch information private int [] mPixels; private volatile boolean isComlemete = false; private Message msg; private setOnGuaGuaComplemeteListener mGuaGuaComplemeteListener; public void setOnGuaGuaComplemeteListener (setOnGuaGu AComplemeteListener mGuaGuaComplemeteListener) {this. listener = mGuaGuaComplemeteListener;} public GuaGuaKa (Context context) {super (context); init (context);} public GuaGuaKa (Context context, AttributeSet attrs) {super (context, attrs ); init (context) ;}@ SuppressLint ("DrawAllocation") @ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasu ReSpec, heightMeasureSpec); // 1.1 get height and width = MeasureSpec. getSize (widthMeasureSpec); height = MeasureSpec. getSize (heightMeasureSpec); // 1.2 get the width and height when initializing BitmapmBitmap = Bitmap. createBitmap (width, height, Bitmap. config. ARGB_8888); mCanvas = new Canvas (mBitmap); // 1.3 set the setPath () of the paint path. // 2.1 set the textPaint attribute setTextMessagePaint (); mCanvas. drawColor (Color. parseColor ("# C7C7C7") ;}@ SuppressLint ("ClickableVie WAccessibility ") @ Overridepublic boolean onTouchEvent (MotionEvent event) {movingX = (int) event. getX (); movingY = (int) event. getY (); switch (event. getAction () {case MotionEvent. ACTION_DOWN: touchDown (event); break; case MotionEvent. ACTION_MOVE: touchMove (event); break; case MotionEvent. ACTION_UP: touchUp (event); break; default: break;} invalidate (); return true ;}@ Overrideprotected void onDraw (Canvas canvas) {c Anvas. drawText (mText, getWidth ()/2-mRectBounds. width ()/2, getHeight ()/2 + mRectBounds. height ()/2, mTextPaint); if (! IsComlemete) {drawPath (); canvas. drawBitmap (mBitmap, 0, 0, null) ;}// draw pathprivate void drawPath () {mPaint. setXfermode (new porterduduxfermode (android. graphics. porterDuff. mode. DST_OUT); mCanvas. drawPath (mPath, mPaint);} // lift private void touchUp (MotionEvent event) {new Thread (mRunnable ). start ();} // move private void touchMove (MotionEvent event) {int dx = Math. abs (movingX-lastX); int dy = Math. abs (mo VingY-lastY); if (dx> 3 | dy> 3) {mPath. lineTo (movingX, movingY);} lastX = movingX; lastY = movingY;} // press private void touchDown (MotionEvent event) {lastX = movingX; lastY = movingY; mPath. moveTo (lastX, lastY);}/*** set the attributes of the paint path */private void setPath () {mPaint. setColor (Color. parseColor ("# c0c0c0"); mPaint. setAntiAlias (true); mPaint. setDither (true); mPaint. setStrokeJoin (Paint. join. ROUND); mPaint. s EtStrokeCap (Paint. cap. ROUND); mPaint. setStyle (Paint. style. STROKE); mPaint. setStrokeWidth (40);}/*** set the paint brush attributes */private void setTextMessagePaint () {mTextPaint. setColor (Color. DKGRAY); mTextPaint. setTextSize (mTextSize); mTextPaint. setStyle (Style. FILL); // obtain the width and height of the text drawn by the current paint brush. getTextBounds (mText, 0, mText. length (), mRectBounds);}/*** initialize ** @ param context */private void init (Context context ){// ---- 1. mask Layer mPaint = new Paint (); mPath = new Path (); // ---- 2. the content in the masking layer is mTextPaint = new Paint (); mRectBounds = new Rect (); mText = "Thank you for your patronage"; mTextSize = 40 ;} /*** enable thread computing on how many areas a user can erase */private Runnable mRunnable = new Runnable () {@ Overridepublic void run () {int w = getWidth (); int h = getHeight (); float wipeArea = 0; float totalArea = w * h; Bitmap bitmap = mBitmap; if (mPixels = null) {mPixels = new int [w * h];} // Obtains all the pixel information on bitmap. bitmap. getPixels (mPixels, 0, w, 0, 0, w, h); for (int I = 0; I <w; I ++) {for (int j = 0; j
Ii. MainActivity implementation
package com.zengtao.testdemo;import android.app.Activity;import android.os.Bundle;import android.widget.Button;import android.widget.Toast;import com.zengtao.view.GuaGuaKa;import com.zengtao.view.GuaGuaKa.setOnGuaGuaComplemeteListener;public class MainActivity extends Activity {private GuaGuaKa mGuaGuaKa;private Button reStart;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mGuaGuaKa = (GuaGuaKa) findViewById(R.id.scratch);reStart = (Button) findViewById(R.id.reStart);mGuaGuaKa.setOnGuaGuaComplemeteListener(new setOnGuaGuaComplemeteListener() {@Overridepublic void complemete(String result) {Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();}@Overridepublic void notEnough(String result) {Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();}});}}
4: The above will be able to complete the implementation of scratch cards. It is quite simple to look at, so you can also try it. Although mosquitoes are small, they are also meat. Come and take it.
Demo: http://download.csdn.net/detail/u011546655/8617235