Android上的同步翻译

net*_*ein 7 android android-animation

我正在尝试在Android上同时进行多次翻译.

我在布局上有两个或更多按钮(大小相同),当我按下一个按钮时,我希望其他按钮移出屏幕.

我已经完成了一个测试应用程序来尝试实现此行为.

在它上面我点击一个按钮设置一个监听器进行测试,例如:

button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
        Button toMove = (Button) findViewById(R.id.button_test2);
        Button toMove2 = (Button) findViewById(R.id.button_test3);

        AnimationSet set = new AnimationSet(true);

        TranslateAnimation anim = new TranslateAnimation(0, -toMove
          .getWidth(), 0, 0);
        anim.setFillAfter(true);
        anim.setDuration(1000);

        toMove.setAnimation(anim);
        toMove2.setAnimation(anim);

        set.addAnimation(anim);

        set.startNow();
    }
Run Code Online (Sandbox Code Playgroud)

风景:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button android:id="@+id/button_test" android:layout_width="200px"
        android:layout_height="50px" android:text="@string/hello" />

    <Button android:id="@+id/button_test2" android:layout_width="200px"
        android:layout_height="50px" android:text="@string/hello"/>

    <Button android:id="@+id/button_test3" android:layout_width="200px"
        android:layout_height="50px" android:text="@string/hello"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

问题是两个按钮开始动画,一个接着一个接一个.我读过它是由于getDelayForView()返回每个的不同延迟.有没有办法同时移动2个或更多按钮?

谷歌不是很有帮助: -

She*_*tib 11

问题:

它似乎setAnimation会实际上开始动画,可能是异步的.但是,可能会锁定为第二个视图设置动画.必须有一个调度程序,因为按不同顺序设置按钮的动画不会影响底部更快的事实.

解决方案是通过创建两个单独的动画来防止这种假设锁定.

码:

public void onClick(View view) {
    Button toMove = (Button) findViewById(R.id.button_test2);
    Button toMove2 = (Button) findViewById(R.id.button_test3);

    TranslateAnimation anim = new TranslateAnimation(0, -toMove
            .getWidth(), 0, 0);
    anim.setFillAfter(true);
    anim.setDuration(1000);

    TranslateAnimation anim2 = new TranslateAnimation(0, -toMove
            .getWidth(), 0, 0);
    anim2.setFillAfter(true);
    anim2.setDuration(1000);

    //THERE IS ONE MORE TRICK

    toMove.setAnimation(anim);
    toMove2.setAnimation(anim2);
}
Run Code Online (Sandbox Code Playgroud)

注意:

//THERE IS ONE MORE TRICK,您可以添加以下代码以确保它们一起移动.仍然必须有1毫秒左右的滞后.

long time =AnimationUtils.currentAnimationTimeMillis();

//This invalidate is needed in new Android versions at least in order for the view to be refreshed.
toMove.invalidate(); 
toMove2.invalidate();
anim.setStartTime(time);
anim2.setStartTime(time);
Run Code Online (Sandbox Code Playgroud)