7

Android で複数の翻訳を同時に実行しようとしています。

レイアウト上に 2 つ以上のボタン (すべて同じサイズ) があり、1 つを押すと、他のボタンが画面の外に出ます。

この動作を実装するためにテストアプリを作成しました。

その上で、テストするボタンのクリックでリスナーを設定しました。

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>

問題は、2 つのボタンが少しずつ交互にアニメーションを開始することです。getDelayForView()それは、それぞれの異なる遅延を返すためであると読みました。2つ以上のボタンを同時に動かす方法はありますか?

Google はあまり役に立ちませんでした:-\

4

1 に答える 1

11

問題:

setAnimationアニメーションを実際に開始し、おそらく非同期で開始するようです。ただし、2 番目のビューのアニメーションの設定がロックされる場合があります。ボタンのアニメーションを異なる順序で設定しても、下の方が速いという事実には影響しないため、ディスパッチャが必要です。

解決策は、2 つの個別のアニメーションを作成することによって、この架空のロックを防ぐことです。

コード:

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);
于 2011-09-23T10:32:57.687 に答える