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);
}