2010-11-16 46 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