Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

Android Animation Problem

  • 29-12-2014 5:28pm
    #1
    Closed Accounts Posts: 19,777 ✭✭✭✭


    I have a bit of a conundrum applying animations to an ImageView that I'm attempting to treat as a sprite for games purposes:

    Below is a simple, cut down, example of what I'm doing; animating an ImageView from {x1, y1} to {x2, y2} (presume Gingerbread or greater in use):
    ImageView sprite = (ImageView) findViewById(id);
    TranslateAnimation ani = new TranslateAnimation(0f, x2 - x1, 0f, y2 - y1);
    ani.setDuration(500);
    ani.setFillAfter(true);
    ani.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}
    			
            @Override
            public void onAnimationEnd(Animation animation) {
                sprite.setX(x2);
        	    sprite.setY(y2);
            }
        });
    sprite.startAnimation(ani);
    
    The animation works well, but then when it gets to the point of setting the position of the ImageView to the new coordinates at the end, it jumps - seemingly extending the tradition so that it's traveled double the distance. Wen moved again, it reappears where it should have stopped, then does the same thing. If the onAnimationEnd code is omitted, the animation ends smoothly, but the ImageView then returns to its original position {x1, y1}.

    My feeling is this is somehow related to how animations are treated that I've not picked up from in the documentation and that there's some sort of silly mistake there, but for the life of me I can't spot it and a set of fresh eyes would be welcome.


Comments

  • Registered Users Posts: 2 daveyburke


    Mixing up absolute pixels (used by TranslateAnimation) and density-independent pixels? Try scaling by getResources().getDisplayMetrics().densityDpi.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    daveyburke wrote: »
    Mixing up absolute pixels (used by TranslateAnimation) and density-independent pixels? Try scaling by getResources().getDisplayMetrics().densityDpi.
    Nope, using absolute pixels in both cases.


  • Registered Users Posts: 747 ✭✭✭smackyB


    I have a bit of a conundrum applying animations to an ImageView that I'm attempting to treat as a sprite for games purposes:

    Below is a simple, cut down, example of what I'm doing; animating an ImageView from {x1, y1} to {x2, y2} (presume Gingerbread or greater in use):
    ImageView sprite = (ImageView) findViewById(id);
    TranslateAnimation ani = new TranslateAnimation(0f, x2 - x1, 0f, y2 - y1);
    ani.setDuration(500);
    ani.setFillAfter(true);
    ani.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}
    			
            @Override
            public void onAnimationEnd(Animation animation) {
                sprite.setX(x2);
        	    sprite.setY(y2);
            }
        });
    sprite.startAnimation(ani);
    
    The animation works well, but then when it gets to the point of setting the position of the ImageView to the new coordinates at the end, it jumps - seemingly extending the tradition so that it's traveled double the distance. Wen moved again, it reappears where it should have stopped, then does the same thing. If the onAnimationEnd code is omitted, the animation ends smoothly, but the ImageView then returns to its original position {x1, y1}.

    My feeling is this is somehow related to how animations are treated that I've not picked up from in the documentation and that there's some sort of silly mistake there, but for the life of me I can't spot it and a set of fresh eyes would be welcome.

    From a quick glance of the code, I think you're misinterpreting what the args for TranslateAnimation do:

    You're asking it to do an X animation from 0 to x2-x1 and a Y animation from 0 to y2-y1 and then asking it move to x2,y2 when finished.

    Say we took x1 and y1 to be 50 and x2 and y2 to be 100 respectively:

    It would animate from 0,0 to 50,50 and then when finished it would move the sprite to 100,100 which sounds like the jump that you're seeing.

    Hope that helps! :)


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    smackyB wrote: »
    From a quick glance of the code, I think you're misinterpreting what the args for TranslateAnimation do:
    Well, I think you're misinterpreting what the args for TranslateAnimation do. Using the four param constructor, they represent the change in coordinates to apply at the start and end of the animation, for X and Y respectively. So the first value is the deltaX from it's starting point, then deltaX at it's end point, then the same for the Y coordinates. Using your example, I'm asking it to do an X animation from x1+0 to x1+(x2-x1), and similarly for Y.

    Using the more detailed constructor, I attempted different approaches, including Animation.ABSOLUTE for the coordinates, which would follow the logic you suggested, but the problem persisted.

    As I said in my first post, the problem only appears to arise in onAnimationEnd or whenever I set the new position of the image once the animation is over. If I omit that the animation works fine, but the image reverts to it's initial position when the animation is done.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Found a solution.

    First of all, it should be noted that I was using setX and setY, which were introduced in Honeycomb. Prior to this, such positioning would have been carried out using LayoutParams:
    layoutParams.leftMargin = x2;
    layoutParams.topMargin = y2;
    sprite.setLayoutParams(layoutParams);
    
    This approach works from API level 1 onwards and should be used rather than setX/setY. It also should be applied after the animation is finished, when the onAnimationEnd event is called.

    Other than that, all that is required is ending the animation before positioning the image, essentially just:
    sprite.clearAnimation();
    
    And the whole thing works smoothly from there on.

    Thanks for the replies, to those who did so.


  • Advertisement
Advertisement