James111
James111

Reputation: 15903

TranslateAnimation not moving Image to the right coordinate

For some reason my TranslateAnimation is not moving the 2 ImageView's to the correct place. I honestly have no idea why the ImageView's are going off the screen!! Im defining minY, maxY, minX, maxX allows me to generate coordinates in between these constants

I've checked that the X and Y coordinates I'm generating are within the screens maxY and maxX. But the imageview's keep getting moved off the screen!

My screens maxY = 2500 and maxX = 1599. Im using a Samsung galaxy tab S

Generating random x + y code:

private static int[] generateRandomX(){
        int[] numbers = new int[2];
        int R;

        int Low = 0;
        int High = 800;

        for(int i=0;i<numbers.length;i++) {
            Random r = new Random();
            R = r.nextInt(High - Low) + Low;
            numbers[i] = R;
//            Log.d("X pos -->", "X = "+numbers[i]);

        }


        return numbers;
    }

    private static int[] generateRandomY(){
        int[] numbers = new int[2];
        int R;

        int Low = 1000;
        int High = 2000;

        for(int i=0;i<numbers.length;i++) {
            Random r = new Random();
            R = r.nextInt(High - Low) + Low;
            numbers[i] = R;
//            Log.d("Y pos -->", "Y = "+numbers[i]);
        }


        return numbers;
    }

Here is where I'm assigning the x and y to the TranslateAnimation

This bit of code is responsible for moving the ImageView from 0X, toX, 0Y, toY

 public static void swipedDiceInThisDirection(float n1, float n2, float n3, float n4){

        //Die1
        moveDie1 = new TranslateAnimation(0, n1, 0, n2);  //  new TranslateAnimation(xFrom,xTo, yFrom,yTo)
        moveDie1.setDuration(2000);  // animation duration

        //Die2
        moveDie2 = new TranslateAnimation(0, n3, 0, n4);
        moveDie2.setDuration(2000);

        moveDie1.setFillAfter(true);
        moveDie2.setFillAfter(true);
        moveDie1.setFillEnabled(true);
        moveDie2.setFillEnabled(true);

        animationListeners(); //Animation listeners
        determineDiceClicked();

    }

Upvotes: 0

Views: 333

Answers (1)

Whitney
Whitney

Reputation: 1237

Translate animation doesn't take in absolute values by default, but takes in the amount of change to apply (the delta). So if your view is at x=1000 and the random number goes to the high range of 800, it is going to move to 1800 which will be off screen for you. Thus in order to go too 800 you would need to consider where your view is currently now and how much change to move it to the new position:

moveDie1 = new TranslateAnimation(0, n1-moveDie1.getX(), 0, n2-moveDie1.getY());

Which turns into 800-1000, so a change of -200 moves the view to x=800 like you wanted.

Or just use a different translationanimation constructor that takes in animation type so you can use absolute values.

public TranslateAnimation (int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)

moveDie1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE, n1, Animation.ABSOLUTE, n2);

Upvotes: 1

Related Questions