2016-04-21 21 views
-1

Ich bin sehr neu in Android Development (wie in gestern begonnen)! Ich versuche eine App zu erstellen, in der ich einen Kreis zeichnen und bewegen kann. Ich stieß auf ein Problem, da Touch-Ereignisse mit meinem aktuellen Code zum Absturz führen. Hier ist der Code:Leinwand Zeichnung mit 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; 

} 

und

/** 
* 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- Fehler –

+0

Veröffentlichen Sie Ihren Logcat – Ameer

+0

Ich kann die Stack-Trace nicht finden, da es abstürzt, wenn ich die App auf meinem Telefon ausführen, so gibt es keine Textausgabe für die Fehler. – LKK

Antwort

Verwandte Themen