Android scratch card custom view, android scratch card view
Import android. content. context; import android. graphics. bitmap; import android. graphics. bitmap. config; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. path; import android. graphics. porterDuff. mode; import android. graphics. porterduxfermode; import android. util. attributeSet; import android. view. motionEvent; import android. view. view; public c Lass TextClear extends View {private boolean isMove = false; private Bitmap bitmap = null; private Bitmap frontBitmap = null; private Path path; private Canvas mCanvas; private Paint paint; private Thread mSweapThread; private boolean isThreadStart = false; private OnTextClearListener mOnTextClearListener; private TextClear mTextClear; private boolean mComplete = false; public TextClear (Context c Ontext, AttributeSet attrs) {super (context, attrs);} public TextClear (Context context) {super (context );} /*** reset the scratch Layer */public void resetting () {mCanvas = null; path = null; isThreadStart = false; mComplete = false; invalidate (); mTextClear. setVisibility (View. VISIBLE);} public void setOnTextClearListener (OnTextClearListener listener, TextClear textClear) {mOnTextClearListener = listener; m TextClear = textClear ;}@ Override protected void onDraw (Canvas canvas) {if (mCanvas = null) {EraseBitmp ();} canvas. drawBitmap (bitmap, 0, 0, null); mCanvas. drawPath (path, paint); super. onDraw (canvas);} public void EraseBitmp () {bitmap = Bitmap. createBitmap (getWidth (), getHeight (), Bitmap. config. ARGB_4444); frontBitmap = CreateBitmap (Color. GRAY, getWidth (), getHeight (); paint = new Paint (); Paint. setStyle (Paint. style. STROKE); paint. setXfermode (new porterduduxfermode (Mode. CLEAR); paint. setAntiAlias (true); paint. setDither (true); paint. setStrokeJoin (Paint. join. ROUND); paint. setStrokeCap (Paint. cap. ROUND); paint. setStrokeWidth (40); path = new Path (); mCanvas = new Canvas (bitmap); mCanvas. drawBitmap (frontBitmap, 0, 0, null); mSweapThread = new Thread (mRunnable); // layer initialization callback if (mOnTextClear Listener! = Null) {mOnTextClearListener. preStratchInitListener (mTextClear) ;}@ Override public boolean onTouchEvent (MotionEvent event) {float ax = event. getX (); float ay = event. getY (); if (event. getAction () = MotionEvent. ACTION_DOWN) {isMove = false; path. reset (); path. moveTo (ax, ay); invalidate (); return true;} else if (event. getAction () = MotionEvent. ACTION_MOVE) {isMove = true; path. lineTo (ax, Y); invalidate (); return true;} else if (event. getAction () = MotionEvent. ACTION_UP) {if (! IsThreadStart) {mSweapThread. start (); isThreadStart = true; // start to scrape the callback if (mOnTextClearListener! = Null) {mOnTextClearListener. onStratchStartListener (mTextClear) ;}} return super. onTouchEvent (event);} public Bitmap CreateBitmap (int color, int width, int height) {LogGloble. d ("color", color + ""); int [] rgb = new int [width * height]; for (int I = 0; I <rgb. length; I ++) {rgb [I] = color;} return Bitmap. createBitmap (rgb, width, height, Config. ARGB_8888);} // The callback interface public interface OnTex TClearListener {/*** listen for Scratch card layer initialization */public void preStratchInitListener (TextClear textClear);/*** listen for Scratch action start */public void onStratchStartListener (TextClear textClear ); /*** complete the listening. */public void onStratchCompleteListenner (TextClear textClear);} private Runnable mRunnable = new Runnable () {@ Override public void run () {while (! MComplete) {int w = bitmap. getWidth (); int h = bitmap. getHeight (); float wipeArea = 0; float totalArea = w * h; int [] mPixels = new int [w * h]; bitmap. getPixels (mPixels, 0, w, 0, 0, w, h); for (int I = 0; I <w; I ++) {for (int j = 0; j
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.