Android上的同步翻译

时间:2010-11-16 16:07:46

标签: 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();
    }

观点:

<?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>

事情是两个按钮开始动画,一个接着一个接一个。我已经读过它是由getDelayForView()引起的,它们会返回不同的延迟。有没有办法同时移动2个或更多按钮?

Google不是很有帮助: - \

1 个答案:

答案 0 :(得分: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);
}

注意:

//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);
相关问题