2015-09-04 79 views
0

我試圖用畫布繪製線條。我能夠畫出一條線,但是一旦我從屏幕上取下手指,它就會消失。我需要線路在屏幕上可見。這是我的代碼`在畫布上繪製後線條消失

public class CanvasView extends View { 
private Canvas mcanvas; 
private Bitmap mBitmap; 
private Path mPath; 
private Paint mPaint; 
private float mX,mY; 
private static final float TOLERANCE = 5; 
Context context; 

private float firstX ; 
private float firstY ; 

public CanvasView(Context c, AttributeSet attrs) { 
    super(c, attrs); 

    context = c; 

    mPath = new Path(); 

    mPaint = new Paint(); 
    mPaint.setAntiAlias(true); 
    mPaint.setColor(Color.BLACK); 
    mPaint.setStyle(Paint.Style.STROKE); 
    mPaint.setStrokeJoin(Paint.Join.ROUND); 
    mPaint.setStrokeWidth(4f); 

    firstX = (float)68.85; 
    firstY = (float)60.25; 

} 

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

    mBitmap  = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888); 
    mcanvas  = new Canvas(mBitmap); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
//  canvas.drawPath(mPath, mPaint); 
//  canvas.drawPoint(mX,mY,mPaint);/*just a point tarvel 
    canvas.drawLine(firstX,firstY,mX,mY,mPaint); 
} 

private void downTouch(float x, float y){ 
    mPath.reset(); 
    mPath.moveTo(x,y); 
    mX = x; 
    mY = y; 
    firstX = x; 
    firstY = y; 
} 

private void moveTouch(float x, float y){ 
//  float dx = Math.abs(x - mX); 
//  float dy = Math.abs(y - mY); 
//  if (dx >= TOLERANCE || dy >= TOLERANCE) { 
//   mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
     mX = x; 
     mY = y; 
//  } 
} 

private void upTouch(float x, float y){ 
    mPath.lineTo(x,y); 
    mPath.moveTo(x,y); 
    firstX = x; 
    firstY = y; 
} 

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

    switch (event.getAction()){ 
     case MotionEvent.ACTION_DOWN: 
      downTouch(x,y); 
      invalidate(); 
      break; 

     case MotionEvent.ACTION_MOVE: 
      moveTouch(x, y); 
      invalidate(); 
      break; 

     case MotionEvent.ACTION_UP: 
      upTouch(x,y); 
      invalidate(); 
      break; 
    } 

    return true; 
} 

public void clearCanvas(){ 
    mPath.reset(); 
    invalidate(); 
} 
}` 

任何人都可以幫助我。任何幫助/建議表示讚賞。 在此先感謝。

+0

你需要多條線才能在你的畫布上顯示? –

+0

@SalmanKhakwani是的,我需要根據用戶需要多次繪製線條。 – elsa

回答

1

你可以請嘗試下面的一個嗎?

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.graphics.RectF; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 

public class CanvasView extends View { 

    private static final float STROKE_WIDTH = 5f; 
    private static final float HALF_STROKE_WIDTH = STROKE_WIDTH/2; 
    private Paint paint = new Paint(); 
    private Path path = new Path(); 
    private float lastTouchX; 
    private float lastTouchY; 
    private final RectF dirtyRect = new RectF(); 

    public CanvasView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     paint.setAntiAlias(true); 
     paint.setColor(Color.BLACK); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeJoin(Paint.Join.ROUND); 
     paint.setStrokeWidth(STROKE_WIDTH); 
    } 

    public void clear() { 
     path.reset(); 
     invalidate(); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     canvas.drawPath(path, paint); 
    } 

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

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      path.moveTo(eventX, eventY); 
      lastTouchX = eventX; 
      lastTouchY = eventY; 
      return true; 

     case MotionEvent.ACTION_MOVE: 
     case MotionEvent.ACTION_UP: 
      resetDirtyRect(eventX, eventY); 
      int historySize = event.getHistorySize(); 
      for (int i = 0; i < historySize; i++) { 
       float historicalX = event.getHistoricalX(i); 
       float historicalY = event.getHistoricalY(i); 
       expandDirtyRect(historicalX, historicalY); 
       path.lineTo(historicalX, historicalY); 
      } 

      path.lineTo(eventX, eventY); 
      break; 

     default: 
      return false; 
     } 

     invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH), 
       (int) (dirtyRect.top - HALF_STROKE_WIDTH), 
       (int) (dirtyRect.right + HALF_STROKE_WIDTH), 
       (int) (dirtyRect.bottom + HALF_STROKE_WIDTH)); 

     lastTouchX = eventX; 
     lastTouchY = eventY; 

     return true; 
    } 

    private void expandDirtyRect(float historicalX, float historicalY) { 
     if (historicalX < dirtyRect.left) { 
      dirtyRect.left = historicalX; 
     } else if (historicalX > dirtyRect.right) { 
      dirtyRect.right = historicalX; 
     } 
     if (historicalY < dirtyRect.top) { 
      dirtyRect.top = historicalY; 
     } else if (historicalY > dirtyRect.bottom) { 
      dirtyRect.bottom = historicalY; 
     } 
    } 

    private void resetDirtyRect(float eventX, float eventY) { 
     dirtyRect.left = Math.min(lastTouchX, eventX); 
     dirtyRect.right = Math.max(lastTouchX, eventX); 
     dirtyRect.top = Math.min(lastTouchY, eventY); 
     dirtyRect.bottom = Math.max(lastTouchY, eventY); 
    } 
} 

希望這會對你有幫助。