Android 刮刮卡自訂view

來源:互聯網
上載者: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);    }    /**     * 重設刮獎圖層     */    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);        //圖層初始化回調        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;                //開始刮回調                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);    }    //刮獎完成回調介面    public interface OnTextClearListener {        /**         * 監聽刮刮卡圖層初始化         */        public void preStratchInitListener(TextClear textClear);        /**         * 監聽刮的動作開始         */        public void onStratchStartListener(TextClear textClear);        /**         * 監聽刮刮樂完成         */        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 < 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 > 60) {                        mComplete = true;                        if (mOnTextClearListener != null) {                            mOnTextClearListener.onStratchCompleteListenner(mTextClear);                        }                        postInvalidate();                    }                }            }        }    };}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android 刮刮卡自訂view

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.