2016-04-21 72 views
-1

我對Android開發非常陌生(如昨天開始)!我正在嘗試製作一個應用程序,可以繪製一個圈並移動它。我遇到了一個問題,因爲使用當前代碼的觸摸事件導致它崩潰。下面的代碼:使用Android的畫布繪圖

/** 
* capture touch events and draw or erase circles accordingly 
*/ 
@Override 
public boolean onTouchEvent(MotionEvent event) { 
    int x = (int)event.getX(); 
    int y = (int)event.getY(); // get x and y coords 
    int xysquared = (int)(Math.pow((x-getWidth()/2),2) + Math.pow(y-getHeight()/2,2)); 
    int xyroot = (int)(Math.pow(xysquared, 0.5)); // calculate the euclidean distance from the coordinate to the circle center 
    int innerCircleRadius = Math.min(getHeight(),getWidth())/4; 
    int outerCircleRadius = Math.min(getHeight(),getWidth())/3; 
    switch(event.getAction()) { 

     case MotionEvent.ACTION_DOWN: 
      if(xyroot >= innerCircleRadius && xyroot <= outerCircleRadius) { 
       drawCircles(x,y); 
      } 
      break; 
     case MotionEvent.ACTION_MOVE: 
      if(xyroot >= innerCircleRadius && xyroot <= outerCircleRadius) { 
       drawCircles(x,y); 
      } 
      break; 
     case MotionEvent.ACTION_UP: 
      if(xyroot >= innerCircleRadius && xyroot<= outerCircleRadius) { 

      } 


    } 
    return true; 

} 

/** 
* draw the circle at given position w/ correct radius 
* @param x xcoord of circle center 
* @param y ycoord of circle center 
*/ 
public void drawCircles(int x,int y) { 
    int radius; 
    int getDiffSquared = (int)(Math.pow((getHeight()/2-getHeight()/3),2) + Math.pow((getWidth()/2-getWidth()/3), 2)); 
    radius = (int)Math.pow(getDiffSquared, 0.5)/2; 
    Path circle = new Path(); 
    Paint circlePaint = new Paint(); 
    circlePaint.setColor(getResources().getColor(R.color.black)); 
    circle.addCircle(x, y, radius, Direction.CW); 
    mCanvas.drawPath(circle,circlePaint); 
} 
+1

http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-錯誤 –

+0

發佈您的logcat – Ameer

+0

我無法找到堆棧跟蹤,因爲當我在手機上運行應用程序時它崩潰了,所以沒有爲錯誤輸出文本。 – LKK

回答