2013-07-25 23 views
0

工作我做了一個簡單的平局和擦除的Android應用程序,我已經試過如下,但是橡皮擦代碼是不工作...它作爲鉛筆工作.... strokewidth的「5」 ......請幫我it.My代碼asbelow: 在customView我的橡皮是不是在android系統

public void eraser() { 
     // TODO Auto-generated method stub 
     mPaint = new Paint(); 
     Toast.makeText(getContext(), "eraser", Toast.LENGTH_LONG).show(); 
     mPaint.setXfermode(null); 
     mPaint.setAlpha(0x00FFFFFF); 
     mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
     // invalidate(); 
    } 

在mainActivity

eraser.setOnClickListener(新OnClickListener(){

@Override 
public void onClick(View v) { 

mDrawView.eraser(); 

} 

});

+0

我編輯我的答案。請看看 –

+0

還有問題... !!! :( – jigar

+0

我不相信。我自己創建了代碼並在設備上進行了測試。您的代碼出現了問題,否則我的代碼正在繪製路徑並刪除繪圖..: –

回答

2

我已創建自定義視圖,它是從你的不同。但會運作良好。我已經實現了它。

public class SingleTouchView extends View { 

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

    public SingleTouchView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     /** 
     * you need to pass here something width and height if it is predefined, 
     * otherwise use explicit constructor to use this view. 
     * 
     * I have taken 400 * 400 here. You can get screen height and width and 
     * according to that you can pass it. 
     */ 
     mBitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888); 
     mCanvas = new Canvas(mBitmap); 
     mPath = new Path(); 
     mBitmapPaint = new Paint(Paint.DITHER_FLAG); 

     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     mPaint.setDither(true); 
     mPaint.setColor(0xFFFF0000); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(12); 
    } 

    /** 
    * Dynamic View adding with parameters 
    * 
    * @param c 
    *   : Context 
    * @param width 
    *   : Width of View 
    * @param height 
    *   : Height of View 
    */ 
    public SingleTouchView(Context c, int width, int height) { 
     super(c); 

     mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
     mCanvas = new Canvas(mBitmap); 
     mPath = new Path(); 
     mBitmapPaint = new Paint(Paint.DITHER_FLAG); 

     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     mPaint.setDither(true); 
     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); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     // canvas.drawColor(0xFFAAAAAA); 
     canvas.drawColor(0x00FFFFFF); 
     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; 
    } 

    public void setErase() { 
     mPaint.setXfermode(null); 
     mPaint.setAlpha(0xFF); 
     mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
    } 
} 

我在這裏設置了setErase()方法。你可以像你一樣使用它。您可以根據您的要求添加另一個代碼。

要明確初始化視圖,使用以下短的代碼。

SingleTouchView View = new SingleTouchView(this, 480, 800); 
relativeLayout..addView(View); 

編輯

XML佈局,使用這一觀點

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/btnErase" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Erase" /> 

    <com.example.SingleTouchView 
     android:id="@+id/paintView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

只需啓動一些活動,並設置此佈局。

+0

我已經按照您的說法對其進行了更改, ,,但還是沒有解決......! – jigar

+0

您可以發佈您的自定義視圖代碼是否有可能? –

+0

等待...我張貼我的整個XML代碼..!檢查 – jigar