atg963
atg963

Reputation: 197

Making an ImageView follow touchpoint on screen

I am working on an Android app where I would like the user to be able to drag their finger on the screen and have an object I have set up as an Image View (a plane) follow it. I have a somewhat working model of this right now where the image will go to a point that is touched on the screen, but this is not exactly what I am looking to do. Also each time the user touches a new point, my image goes back to its starting spot then go to the users touch point. This is the relevant code:

public boolean onTouch(View v, MotionEvent e){
    switch (e.getAction()){
        case MotionEvent.ACTION_DOWN:
        {

        }
        case MotionEvent.ACTION_UP:
        {
            float x = e.getX();
            float y = e.getY();
            movePlane(x, y);
        }
        case MotionEvent.ACTION_MOVE:
        {

        }
    }
    return true;
}

private boolean movePlane(float endX, float endY){
    ImageView plane = (ImageView) findViewById(planeID);
    float startX = plane.getX();
    float startY = plane.getY();
    TranslateAnimation animation = new TranslateAnimation(startX, endX, startY, endY);
    animation.setDuration(3000);
    animation.setFillAfter(true);
    animation.setFillEnabled(true);
    plane.startAnimation(animation);

    return true;
}

So I know I am probably going to be working inside of the ACTION_DOWN case but I am not sure exactly how I should be handling the event and ImageView accordingly. Any help is really appreciated.

Upvotes: 0

Views: 432

Answers (2)

Gaurav Pangam
Gaurav Pangam

Reputation: 342

Hi try this link here you can use the params.x and params.y as the x,y co-ordinates you are passing to the method movePlane. Hope it helps.

There is a lot to read on that link, so thought its better i post the important code here. Below is the code you can use

plane.setOnTouchListener(new View.OnTouchListener() {
  private int initialX;
  private int initialY;
  private float initialTouchX;
  private float initialTouchY;

@Override public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
  case MotionEvent.ACTION_DOWN:
    initialX = params.x;
    initialY = params.y;
    initialTouchX = event.getRawX();
    initialTouchY = event.getRawY();
    return true;
  case MotionEvent.ACTION_UP:
    return true;
  case MotionEvent.ACTION_MOVE:
    params.x = initialX + (int) (event.getRawX() - initialTouchX);
    params.y = initialY + (int) (event.getRawY() - initialTouchY);
    movePlane(params.x, params.y);
    return true;
}
return false;
}
});

Plane is ofcourse the ImageView which you want to be dragged. You can refer the link if you want i took reference from it.

Upvotes: 1

JoWeber
JoWeber

Reputation: 68

you have to set the x and y values after your animation with an AnimationListener -> onAnimationFinished, to avoid the "Also each time the user touches a new point, my image goes back to its starting spot then go to the users touch point" thing.

Upvotes: 1

Related Questions