Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> Android中如何實現刮刮樂效果

Android中如何實現刮刮樂效果

編輯:Android開發教程

前幾個月剛接觸Android的時候做了一個小項目,其中也用到了類似刮刮樂的效果,現在把代碼貼出來

首先要做一個類似橡皮擦的東西吧,然後才能把紙上的筆跡擦除

/** 
 * FileName: SplashActivity.java 
 *  
 * @desc 橡皮擦功能,類似刮刮樂效果 
 * @author HTP 
 * @Date 20140311 
 * @version 1.00 
 */
public class Text_Rubbler extends TextView {  
      
    private float TOUCH_TOLERANCE; // 填充距離,使線條更自然,柔和,值越小,越柔和。  
      
    // private final int bgColor;  
    // 位圖  
    private Bitmap mBitmap;  
    // 畫布  
    private Canvas mCanvas;  
    // 畫筆  
    private Paint mPaint;  
    private Path mPath;  
    private float mX, mY;  
      
    private boolean isDraw = false;  
      
    public Text_Rubbler(Context context) {  
        /** 
         * @param context 上下文 
         */
        super(context);  
      
    }  
      
    public Text_Rubbler(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
        // bgColor =  
        // attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android",  
        // "textColor", 0xFFFFFF);  
        // System.out.println("Color:"+bgColor);  
    }  
      
    public Text_Rubbler(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        // bgColor =  
        // attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android",  
        // "textColor", 0xFFFFFF);  
        // System.out.println(bgColor);  
        // System.out.println(attrs.getAttributeValue("http://schemas.android.com/apk/res/android",  
        // "layout_width"));  
    }  
      
    @Override
    protected void onDraw(Canvas canvas) {  
        super.onDraw(canvas);  
        if (isDraw) {  
      
            mCanvas.drawPath(mPath, mPaint);  
            // mCanvas.drawPoint(mX, mY, mPaint);  
            canvas.drawBitmap(mBitmap, 0, 0, null);  
        }  
    }  
      
    /** 
     * 開啟檫除功能 
     *  
     * @param bgColor 
     *            覆蓋的背景顏色 
     * @param paintStrokeWidth 
     *            觸點(橡皮)寬度 
     * @param touchTolerance 
     *            填充距離,值越小,越柔和。 
     */
    public void beginRubbler(final int bgColor, final int paintStrokeWidth,  
            float touchTolerance) {  
        TOUCH_TOLERANCE = touchTolerance;  
        // 設置畫筆  
        mPaint = new Paint();  
        // mPaint.setAlpha(0);  
        // 畫筆劃過的痕跡就變成透明色了  
        mPaint.setColor(Color.BLACK); // 此處不能為透明色  
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));  
        // 或者  
        // mPaint.setAlpha(0);  
        // mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));  
      
        mPaint.setAntiAlias(true);  
        mPaint.setDither(true);  
        mPaint.setStyle(Paint.Style.STROKE);  
        mPaint.setStrokeJoin(Paint.Join.ROUND); // 前圓角  
        mPaint.setStrokeCap(Paint.Cap.ROUND); // 後圓角  
        mPaint.setStrokeWidth(paintStrokeWidth); // 筆寬  
      
        // 痕跡  
        mPath = new Path();  
        ;  
        // 覆蓋  
        // if (getLayoutParams().width == LayoutParams.FILL_PARENT) {  
        //  
        // }  
        mBitmap = Bitmap.createBitmap(getLayoutParams().width,  
                getLayoutParams().height, Config.ARGB_8888);  
        mCanvas = new Canvas(mBitmap);  
      
        mCanvas.drawColor(bgColor);  
        isDraw = true;  
    }  
      
    @Override
    public boolean onTouchEvent(MotionEvent event) {  
        if (!isDraw) {  
            return true;  
        }  
        switch (event.getAction()) {  
        case MotionEvent.ACTION_DOWN: // 觸點按下  
            // touchDown(event.getRawX(),event.getRawY());  
            touchDown(event.getX(), event.getY());  
            invalidate();  
            break;  
        case MotionEvent.ACTION_MOVE: // 觸點移動  
            touchMove(event.getX(), event.getY());  
            invalidate();  
            break;  
        case MotionEvent.ACTION_UP: // 觸點彈起  
            touchUp(event.getX(), event.getY());  
            invalidate();  
            break;  
        default:  
            break;  
        }  
        return true;  
    }  
      
    private void touchDown(float x, float y) {  
        mPath.reset();  
        mPath.moveTo(x, y);  
        mX = x;  
        mY = y;  
    }  
      
    private void touchMove(float x, float y) {  
        float dx = Math.abs(x - mX);  
        float dy = Math.abs(y - mY);  
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {  
            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);  
            mX = x;  
            mY = y;  
        }  
      
    }  
      
    private void touchUp(float x, float y) {  
        mPath.lineTo(x, y);  
        mCanvas.drawPath(mPath, mPaint);  
        mPath.reset();  
    }  
      
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved