开发者

Android TranslationAnimation not working correctly

开发者 https://www.devze.com 2023-03-05 06:11 出处:网络
I am trying to animate cards across the screen. My code looks like this: private void animate(ImageViewanimationSource, ImageViewanimationTarget, int animationDuration, int animationDelay) {

I am trying to animate cards across the screen.

My code looks like this:

private void animate(ImageView  animationSource, ImageView  animationTarget, int animationDuration, int animationDelay) {
    int[] animationSourcePosition = {0,0};
    int[] animationTargetPosition = {0,0};

    animationSource.getLocationInWindow(animationSourcePosition);
    animationTarget.getLocationInWindow(animationTargetPosition);

    TranslateAnimation cardDealingAnimation = new TranslateAnima开发者_如何学运维tion(
        Animation.ABSOLUTE,animationSourcePosition[0],
        Animation.ABSOLUTE,animationTargetPosition[0],
        Animation.ABSOLUTE,animationSourcePosition[1],
        Animation.ABSOLUTE,animationTargetPosition[1]);

    cardDealingAnimation.setDuration(animationDuration);
    cardDealingAnimation.setStartOffset(animationDelay);
    animationTarget.startAnimation(cardDealingAnimation);
}

What happens is that it just pops up in the new place without showing the places in-between. Funnily enough, on one of my targets it does sometimes show up.


There are two problems I see here.

One problem is that you're getting the position of your objects in the Window, where you should just use the position of the objects in their parent container. It's easier, faster, and more what you're looking for - where the objects live in their layout. If they do not live in the same container, then you may need to get the position in the window or the screen, and then just deal with delta values (the difference in their locations, not the absolute locations themselves).

You can get these simple container positions from the getLeft() and getTop() methods on View.

The second problem is that the TranslateAnimation parameters don't do what you think they do. The numeric values (the second, fourth, sixth, and eighth values) specify the starting and ending locations of the target object as offsets from its current location. That's why they have the word 'delta' in them in the method declaration. So you should not send in the absolute coordinates of the object positions their, but, rather, the deltas that they should have.

Let's suppose you have calculated an xDelta and yDelta as the difference in x and y between the source and target positions. If you're animating the source to the target position, then you would use something like 0, xDelta, 0, yDelta for these values.

Note that TranslateAnimation() does not physically move the object - it just draws it in a different place. So as soon as the animation is complete, the object will snap back to its original location. You may want to set the fillAfter property to maintain its end location on the screen when the animation completes.

0

精彩评论

暂无评论...
验证码 换一张
取 消