Android Scratch Card Custom View

Source: Internet
Author: User

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.porterduffxfermode;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;public class 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 context, AttributeSet Attrs) {Super (context, attrs);    } public Textclear (context context) {super (context); }/** * Reset 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    Mtextclear = 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 Porterduffxfermode (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 (Montextclearlistener! = 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, ay);            Invalidate ();        return true; } else if (event.getaction () = = motionevent.action_up) {if (!isthreadstart){Msweapthread.start ();                Isthreadstart = true; Start scraping 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); }//Scratch award Completion Callback Interface public interface Ontextclearlistener {/** * Monitor scratch card Layer initialization */public void PR        Estratchinitlistener (Textclear textclear);        /** * Monitor the action of scraping start/public void Onstratchstartlistener (Textclear textclear); /** * Monitor Scratch finish */public void Onstratchcompletelistenner (Textclear textclear);    } private Runnable mrunnable = new Runnable () {@Override public void run () {while (!mcom                plete) {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 < H; j + +) {int index = i                        + J * W;                        if (mpixels[index] = = 0) {wipearea++;                    }}} if (Wipearea > 0 && totalarea > 0) {                    int percent = (int) (Wipearea * 100/totalarea);                    LOGGLOBLE.E ("percent", percent + "");                        if (Percent >) {mcomplete = true; if (MONtextclearlistener = null) {Montextclearlistener.onstratchcompletelistenner (mtextclear);                    } postinvalidate (); }                }            }        }    };}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Scratch Card Custom View

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.