2012-01-17 37 views
10

我需要爲我在學校的最終項目創建一個「刮刮卡」應用程序,並且無法找到如何實施刮擦事件的方式(如何創建背景圖像並將灰色矩形放在其上,所以當我將刮擦那些矩形我會看到它們下面的圖片)如何在Android中創建刮刮卡?

該實現必須在Android中,因爲我不知道如何在Objective-C中開發。

我看到了Objective-C實現的參考,但它不好,因爲我不明白它。

我的代碼是:

public class FingerPaint extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     try { 
      MyView myView = new MyView(this); 
      myView.requestFocus(); 
      myView.PaintObjectInit(); 
      // setContentView(myView); 

      LinearLayout upper = (LinearLayout) findViewById(R.id.LinearLayout01); 
      upper.addView(myView); 
     } catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 

     // MyImageView myImageView = new MyImageView(this); 
     // setContentView(myImageView); 
    } 
} 



public class MyView extends View { 

    private Paint mPaint; 
    private Bitmap mBitmap; 
    private Canvas mCanvas; 
    private Path mPath; 
    private Paint mBitmapPaint; 

    public MyView(Context context) { 
     super(context); 
     this.mPaint = new Paint(); 
     mPath = new Path(); 
     mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
    } 

    protected void PaintObjectInit() { 
     mPaint.setAntiAlias(true); 
     mPaint.setDither(true); 
     //mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
     //mPaint.setColor(0xFFFF0000); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(12); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 

     try 
     { 


     //Bitmap bm1 = BitmapFactory.decodeResource(this.getResources(),R.drawable.scratch).copy(Bitmap.Config.ARGB_8888, true);; 
     //Bitmap bm2 = BitmapFactory.decodeResource(this.getResources(),R.drawable.main).copy(Bitmap.Config.ARGB_8888, true);; 

     //mBitmap = toTransparency(bm1, 0xFFAAAAAA, true, bm2); 

     mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
     mCanvas = new Canvas(mBitmap); 
     } 
     catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 

     canvas.drawColor(0xFFAAAAAA); 
     canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
     canvas.drawPath(mPath, mPaint); 
    } 

    private float mX, mY; 
    private static final float TOUCH_TOLERANCE = 4; 

    private void touch_start(float x, float y) { 
     // mPath.reset(); 
     mPath.moveTo(x, y); 
     mX = x; 
     mY = y; 
    } 

    private void touch_move(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 touch_up() { 
     mPath.lineTo(mX, mY); 
     // commit the path to our offscreen 
     mCanvas.drawPath(mPath, mPaint); 
     // kill this so we don't double draw 
     // mPath.reset(); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     float x = event.getX(); 
     float y = event.getY(); 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      touch_start(x, y); 
      invalidate(); 
      break; 
     case MotionEvent.ACTION_MOVE: 
      touch_move(x, y); 
      invalidate(); 
      break; 
     case MotionEvent.ACTION_UP: 
      touch_up(); 
      invalidate(); 
      break; 
     } 
     return true; 
    } 
} 

請幫助對此。

+0

檢查這個問題http://stackoverflow.com/questions/5848722/android-scratch-card-app – Sameer 2012-01-17 10:05:07

+0

它這麼想的幫助我,我不太瞭解它。 – Offer 2012-01-17 10:46:00

+0

所以刮刮卡不是那麼簡單。在android.hope中閱讀NFC會有所幫助。 – Sameer 2012-01-17 10:53:08

回答

1

我發現這個庫非常有用。

https://github.com/sharish/ScratchView

很容易集成

<com.cooltechworks.views.ScratchImageView 
    android:id="@+id/sample_image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white" 
    android:src="@drawable/img_sample2" 
/> 

enter image description hereenter image description here

1

你可以像這樣 「刮刮卡」 的應用程序。你需要遵循下面的代碼,這是工作代碼。你只需要理解和實現你自己的邏輯。

public class MainActJava extends AppCompatActivity { 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); 
     setContentView(new MyView(this, bitmap)); 
    } 

    public class MyView extends View implements View.OnTouchListener { 

     private static final float TOUCH_TOLERANCE = 4; 
     private Paint mPaint; 
     private Bitmap oBitmap; 
     private Bitmap holder; 
     private Canvas mCanvas; 
     private Path mPath; 
     private Paint mBitmapPaint; 
     private float mX, mY; 

     public MyView(Context context) { 
      super(context); 
     } 

     public MyView(Context context, Bitmap bitmap) { 
      super(context); 
      setOnTouchListener(this); 
      this.oBitmap = bitmap; 
      this.mPaint = new Paint(); 
      mPath = new Path(); 
      mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
      init(); 
     } 

     protected void init() { 
      mPaint.setAntiAlias(true); 
      mPaint.setDither(true); 
      mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
      mPaint.setStyle(Paint.Style.STROKE); 
      mPaint.setStrokeJoin(Paint.Join.ROUND); 
      mPaint.setStrokeCap(Paint.Cap.ROUND); 
      mPaint.setStrokeWidth(35); 
     } 

     @Override 
     protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
      super.onSizeChanged(w, h, oldw, oldh); 
      holder = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
      mCanvas = new Canvas(holder); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      onDrawing(canvas); 
     } 

     private void onDrawing(Canvas canvas) { 
      mCanvas.drawColor(0xFFAAAAAA); 
      mCanvas.drawPath(mPath, mPaint); 
      canvas.drawBitmap(oBitmap, getWidth()/2, getHeight()/2, mBitmapPaint); 
      canvas.drawBitmap(holder, 0, 0, mBitmapPaint); 
     } 

     private void touch_start(float x, float y) { 
      mPath.moveTo(x, y); 
      mX = x; 
      mY = y; 
     } 

     private void touch_move(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 touch_up() { 
      mPath.lineTo(mX, mY); 
      mCanvas.drawPath(mPath, mPaint); 
     } 


     @Override 
     public boolean onTouch(View view, MotionEvent event) { 
      float x = event.getX(); 
      float y = event.getY(); 

      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        touch_start(x, y); 
        invalidate(); 
        break; 
       case MotionEvent.ACTION_MOVE: 
        touch_move(x, y); 
        invalidate(); 
        break; 
       case MotionEvent.ACTION_UP: 
        touch_up(); 
        invalidate(); 
        break; 
      } 
      return true; 
     } 
    } 
} 

輸出:

enter image description here