-1

I'm very new to Android Development (as in started yesterday)! I'm trying to make an app where I can draw a circle and move it around. I ran into a problem, as touch events with my current code cause it to crash. Here's the code:

/**
 * 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;

}

and

/**
 * 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);
}
LKK
  • 9
  • 1
    http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Mike M. Apr 21 '16 at 06:02
  • I can't find the stack trace because it's crashing when I run the app on my phone, so there isn't a text output for the errors. – LKK Apr 21 '16 at 06:20

1 Answers1

0

First of all use below link to create canvas for drawing: https://stackoverflow.com/a/16650524/3741769

Once done with drawing, get view of your drawing from canvas and make it moveable using MoveGestureDetector class. Here link for MoveGestureDetector class.

https://github.com/Almeros/android-gesture-detectors/blob/master/library/src/main/java/com/almeros/android/multitouch/MoveGestureDetector.java

Community
  • 1
  • 1
Ahsanwarsi
  • 1,013
  • 9
  • 18