Android scratch card custom view, android scratch card view

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.