简体   繁体   English

Android翻译动画

[英]Android Translation Animation

I'm struggling with Android animation system. 我正在努力使用Android动画系统。 For testing\\learning purpose, I've made a simple layout as follows: 为了进行测试\\学习,我做了一个简单的布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.8"
    android:background="#ffcccccc">

    <Button
        android:id="@+id/animation_object" 
        android:layout_width="44dp"
        android:layout_height="44dp"
        android:background="#ffff0000"
        android:text="Click me!"
        android:onClick="onClickAnimatingObject">

    </Button>
</FrameLayout>

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.2">

    <Button 
        android:id="@+id/btn_animate"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Animate!"
        android:onClick="onClick"/>
</LinearLayout>

What I'm doing is translating the animation_object button about it's width when I click on btn_animate button, like this: 当我单击btn_animate按钮时,我正在做的是翻译animation_object按钮的宽度,如下所示:

public void onClick(View view) {                
    ObjectAnimator animator = ObjectAnimator.ofFloat(animatingObject, "x", animatingObject.getLeft(), animatingObject.getWidth()).setDuration(300);
    animator.setRepeatMode(ObjectAnimator.INFINITE);
    animator.addListener(new AnimatorListener() {

        @Override
        public void onAnimationStart(Animator animation) {
            Log.v("ACN", "Start! l = "+animatingObject.getLeft()+" || r = "+animatingObject.getRight()+" || t = "+animatingObject.getTop()+" || b = " + animatingObject.getBottom());
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animator animation) {                
            Log.v("ACN", "End! l = "+animatingObject.getLeft()+" || r = "+animatingObject.getRight()+" || t = "+animatingObject.getTop()+" || b = " + animatingObject.getBottom());
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            // TODO Auto-generated method stub

        }
    });

    animator.start();

}

When I click the first time I get the following logs: 当我第一次单击时,将获得以下日志:

04-29 17:33:03.249: V/ACN(22061): Start! l = 0 || r = 88 || t = 0 || b = 88
04-29 17:33:03.554: V/ACN(22061): End! l = 0 || r = 88 || t = 0 || b = 88

therefore, when I click btn_animate again the animation_object button "restarts" the animation from its starting position. 因此,当我再次单击btn_animate时, animation_object按钮将从其起始位置“重新启动”动画。

What I'd want is that the button is translated from its current position after each click of btn_animate , but I really don't know how to do that. 我想要的是在每次单击btn_animate之后从当前位置转换按钮,但是我真的不知道该怎么做。 Any suggestion? 有什么建议吗?

Thank you Luca 谢谢卢卡

I recommend using getX() instead of of getLeft() in your object animator, especially as you are updating the x property. 我建议在对象动画设计器中使用getX()而不是getLeft(),尤其是在更新x属性时。 Then update the other value each time with another variable like: offset+animatingObject.getWidth(). 然后每次使用另一个变量更新另一个值,例如:offset + animatingObject.getWidth()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM