2015-05-29 46 views
0

我有一個繪製線條的應用程序。當我按下屏幕時,削減我的手指並停止觸摸,它畫出一條線。我想知道如何在我將屏幕上的手指削減時看到正在繪製的線條。在繪製時用預覽繪製直線

這是我的代碼繪製線條:

public class DrawingView extends View { 

    private Path drawPath; 
    private Paint drawPaint; 
    private Paint canvasPaint; 
    private int paintColor=Color.BLACK; 
    private Canvas drawCanvas; 
    private Bitmap canvasBitmap; 
    float tX; 
    float tY; 

    public DrawingView(Context context, AttributeSet attrs){ 
     super(context, attrs); 
     setupDrawing(); 
    } 

    private void setupDrawing(){ 
     drawPath=new Path(); 
     drawPaint=new Paint(); 
     drawPaint.setColor(paintColor); 
     drawPaint.setAntiAlias(true); 
     drawPaint.setStrokeWidth(1); 
     drawPaint.setStyle(Paint.Style.STROKE); 
     drawPaint.setStrokeJoin(Paint.Join.ROUND); 
     drawPaint.setStrokeCap(Paint.Cap.ROUND); 
     canvasPaint=new Paint(Paint.DITHER_FLAG); 
    } 

    protected void onSizeChanged(int w, int h, int oldw, int oldh){ 
     super.onSizeChanged(w, h, oldw, oldh); 
     canvasBitmap=Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
     drawCanvas=new Canvas(canvasBitmap); 
    } 


    protected void onDraw(Canvas canvas){ 
     canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint); 
     canvas.drawPath(drawPath, drawPaint); 
    } 

    private void touch_start(float x, float y){ 
     drawPath.reset(); 
     drawPath.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){ 
      mX=x; 
      mY=y; 
     } 
    } 

    private void touch_up(){ 
     drawPath.lineTo(mX, mY); 
     drawCanvas.drawPath(drawPath, drawPaint); 
     drawPath.reset(); 
    } 

    public boolean onTouchEvent(MotionEvent event){ 
     tX=event.getX(); 
     tY=event.getY(); 
     switch(event.getAction()){ 
      case MotionEvent.ACTION_DOWN: 
       touch_start(tX,tY); 
      break; 
      case MotionEvent.ACTION_MOVE: 
       touch_move(tX,tY); 
      break; 
      case MotionEvent.ACTION_UP: 
       touch_up(); 
      break; 
      default: 
       return false; 
     } 
     invalidate(); 
     return true; 
    } 
} 
+0

請充分類代碼... – ravi

+0

我剛纔編輯的代碼與全類代碼 – malaka

回答

2
public class DrawingView extends View { 

    private static final float TOUCH_TOLERANCE = 0; 
    private Path drawPath; 
    private Paint drawPaint; 
    private Paint canvasPaint; 
    private int paintColor=Color.BLACK; 
    private Canvas drawCanvas; 
    private Bitmap canvasBitmap; 
    float tX; 
    float tY; 
    private float mX; 
    private float mY; 

    private float a,b; 

    public DrawingView(Context context, AttributeSet attrs){ 
     super(context, attrs); 
     setupDrawing(); 
    } 

    private void setupDrawing(){ 
     drawPath=new Path(); 
     drawPaint=new Paint(); 
     drawPaint.setColor(paintColor); 
     drawPaint.setAntiAlias(true); 
     drawPaint.setStrokeWidth(1); 
     drawPaint.setStyle(Paint.Style.STROKE); 
     drawPaint.setStrokeJoin(Paint.Join.ROUND); 
     drawPaint.setStrokeCap(Paint.Cap.ROUND); 
     canvasPaint=new Paint(Paint.DITHER_FLAG); 
    } 

    protected void onSizeChanged(int w, int h, int oldw, int oldh){ 
     super.onSizeChanged(w, h, oldw, oldh); 
     canvasBitmap=Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
     drawCanvas=new Canvas(canvasBitmap); 
    } 


    protected void onDraw(Canvas canvas){ 
     canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint); 
     canvas.drawPath(drawPath, drawPaint); 
    } 

    private void touch_start(float x, float y){ 
     drawPath.reset(); 
     drawPath.moveTo(x, y); 
     mX=x; 
     mY=y; 
     a=x; 
     b=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){ 
      mX=x; 
      mY=y; 
     } 
     drawPath.lineTo(mX, mY); 
     //invalidate(); 
    } 

    private void touch_up(float tX2, float tY2){ 
     drawPath.lineTo(mX, mY); 
     //drawCanvas.drawPath(drawPath, drawPaint); 
     drawCanvas.drawLine(a, b, tX2, tY2, drawPaint); 
     drawPath.reset(); 
    } 

    public boolean onTouchEvent(MotionEvent event){ 
     tX=event.getX(); 
     tY=event.getY(); 
     switch(event.getAction()){ 
      case MotionEvent.ACTION_DOWN: 
       touch_start(tX,tY); 
      break; 
      case MotionEvent.ACTION_MOVE: 
       touch_move(tX,tY); 
      break; 
      case MotionEvent.ACTION_UP: 
       touch_up(tX,tY); 
      break; 
      default: 
       return false; 
     } 
     invalidate(); 
     return true; 
    } 
} 

請試試這個...

+0

它繪製了一條「自由手」線,而不是一條直線。我想我沒有詳細說明,對不起 – malaka

+0

不,它只是畫自由手線..你只是畫線時移動..一旦運行它 – ravi

+0

它抽出手,但我需要直。如果沒有預覽,它會繪製一條直線,我想要預覽該直線。 – malaka