2010-11-16 48 views
7

我正在尝试在Android上同时做几个翻译。同时在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()返回不同的延迟。有什么办法可以同时移动两个或多个按钮吗?

谷歌是不是非常有帮助: - \

回答

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); 
+0

这行不通形成我吗? – 2013-12-30 12:33:22