Search code examples
pointtouch-eventontouchlistener

How to declare a set of points for an object to move to and from? [Android/Java]


What I am trying to do is create a handwriting application which allows a person to press on the object (circle) and move it up/down on a set path. If the user reaches the lowest point another circle object is created and another set of points is created to follow etc.

So far I have a onTouch event which moves my ImageView object (circle) to where ever the finger is on the touch screen.

https://gist.github.com/Temptex/9796403

How can i get my ImageView (Circle) object to go from Point A -> B smoothly using onTouch events?

Edit: A picture example of what I am asking: https://i.sstatic.net/9kSfe.jpg


Solution

  • created an algorithm for it.

    Created an array of x integers and y integers and used a for loop to check if the object was to high (out of bounds in the y coordinates) and check if y and x were out of bounds in the arrays.

    If it was out of bounds from the array of integers, set it to the current x/y array values. in the for loop.

    If this helps anyone here is the code:

    private final int yCoOrdinate[] = {310, 360, 410};
    private final int xCoOrdinate[] = {905, 890, 875};
    
    // Get finger position
    int x = (int)event.getRawX();
    int y = (int)event.getRawY();
    
    // View x/y co-ordinate on TextView
    coordinates.setText("X = " + x + " - Y = " + y);
    
    for(int i = 0; i < yCoOrdinate.length; i++) {
    
        // If object is to high set it to yCoOrdinate[0]                
        if(y <= yCoOrdinate[0]) {
            y = yCoOrdinate[0];
        }
    
        // Checks top left corner of A              
        if(y == yCoOrdinate[0] && x <= xCoOrdinate[0]) {
            x = xCoOrdinate[0];
            Log.d("Coordinate check", "X = " + x + "Y = " + y);
    
        // Checks if current x/y position if out of bounds and sets them to i
        } else if (y <= yCoOrdinate[i] && x <= xCoOrdinate[i]) {
            y = yCoOrdinate[i];
            x = xCoOrdinate[i];
            Log.d("Coordinate check", "X = " + x + "Y = " + y);
        }
    }
    

    I can now control my ImageView with in bounds.