views:

70

answers:

1

I created a custom view loading an image of a small ball. So that the onDraw method would look like this:

public void onDraw(Canvas canvas) {
    canvas.drawBitmap(...);
}

Later, I added an onTouch listener to be able to listen to touch events to enable the ball to be dragged around.

public boolean onTouchEvent(MotionEvent event) { 
        int eventaction = event.getAction(); 

        int X = (int)event.getX(); 
        int Y = (int)event.getY(); 

        switch (eventaction ) { 

        case MotionEvent.ACTION_MOVE:   // touch drag with the ball 
            // move the balls the same as the finger 
            ball.setX(X-25); 
            ball.setY(Y-25); 

            break; 
        } 
        // redraw the canvas 
        invalidate(); 
        return true; 
    } 

Now, I am trying to make the ball move ONLY along a curve and if its not moved beyond a fixed point, make it swing back to its original position. So there are two problems I am currently facing:

  1. Fixing the movement path of the ball
  2. Flinging it back by animating it. One problem I am observing is if I use ball.startAnimation, and if the ball was slightly out of focus, the ball appears sliced.

Ball Swing

Any suggestions please?

A: 

The curve appears to be part of a circle, suggesting it has a centre and a radius.

I'd suggest determining what these are, and then writing a function that, given the finger position, returns where along the curve the ball should be. This simply requires determining the angle of the finger compared to the centre of the circle, and then using this angle to position the ball correctly using basic trigonometry.

In your picture, for example, the ball is at 0° and the mark appears to be at about 20°.

You could then determine what angle the ball should fling back to should the finger past a certain point (again, described by an angle from the circle's centre)

Dave