2012-05-15 8 views
23

私は2つの線形レイアウトを持っています。これらのレイアウトは、両方のレイアウトで同時に2つの異なるアニメーションを実行したいのです。アンドロイドで同時に2つのアニメーションを開始するにはどうすればいいですか?

今や、それは逐次的に働いています。すなわち、あるものが別のものを開始した後である。

ここは私のコードです。

Animation inFromRight = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT, +0.0f, 
        Animation.RELATIVE_TO_PARENT, 0.0f, 
        Animation.RELATIVE_TO_PARENT, 0.0f, 
        Animation.RELATIVE_TO_PARENT, 0.0f); 
      inFromRight.setDuration(500); 
      inFromRight.setInterpolator(new AccelerateInterpolator()); 

    Animation outtoLeft = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT, 0.0f, 
        Animation.RELATIVE_TO_PARENT, -1.0f, 
        Animation.RELATIVE_TO_PARENT, 0.0f, 
        Animation.RELATIVE_TO_PARENT, 0.0f); 
      outtoLeft.setDuration(500); 
      outtoLeft.setInterpolator(new AccelerateInterpolator()); 

    @Override 
     public void onClick(View v) { 
      switch (v.getId()) { 
      case R.id.menu: 
          mainLayout.startAnimation(outtoLeft); 
       sideBar.startAnimation(inFromRight);     
       break; 
      } 
     } 

outtoLeft.setAnimationListener(new AnimationListener() { 
      @Override 
      public void onAnimationStart(Animation animation) { 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
       mainLayout 
         .setLayoutParams(new LayoutParams(
           LayoutParams.FILL_PARENT, 
           LayoutParams.FILL_PARENT, 40)); 

      } 
     }); 

ありがとうございます!

+0

あなたのコード全体ですか?両方のアニメーションを書き込む方法は、同時に開始する必要があります。 –

+0

いいえ、アニメーションリスナーもあります。 1つのアニメーションを完成させた後、他のUI操作を実行していますが、それは影響しますか?私はコードを更新しました。 – Noby

答えて

12

私はあなたがドキュメントからAnimationSet

http://developer.android.com/reference/android/view/animation/AnimationSet.html

を使用する必要があると思う:

Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform.

AnimationSetが実行/建設されることになっているかを示してのこのリンクの並べ替え: http://groups.google.com/group/android-developers/browse_thread/thread/9df24ac25f01cee9/6e0eac7802a541e3

+7

アニメーションセットはアニメーションの組み合わせとして動作するものであり、別々のアニメーションでは別々のアニメーションではないと思います。 – Noby

+0

あなたが正しいです、私は質問を誤解し、同じオブジェクトに対してアニメーションを実行していると思いました。 – Gophermofur

1

この問題を解決する方法は、AnimatorSetAnimatorのオブジェクト。あなたは後方互換性を心配している場合は、yourAnimation.xmlにすべてのアニメーションあなたを定義し、次に、あなたの活動に

ImageView reusableImageView = (ImageView)findViewById(R.id.imageView1); 
reusableImageView.setImageResource(R.drawable.flag); 
reusableImageView.setVisibility(View.VISIBLE); 
Animation an = AnimationUtils.loadAnimation(this, R.anim.yourAnimation); 
reusableImageView.startAnimation(an); 

を1.0

2

をAndroidにすべての方法をAPIを戻すためにNineOldAndroidsライブラリを使用することができますwant

<set 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shareInterpolator="false"> 
<scale 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:fromXScale="1.0" 
    android:fromYScale="1.0" 
    android:toXScale="2.0" 
    android:toYScale="2.0" 
    android:duration="2500" /> 
<scale 
    android:startOffset="2500" 
    android:duration="2500" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:fromXScale="1.0" 
    android:fromYScale="1.0" 
    android:toXScale="0.5" 
    android:toYScale="0.5" /> 
<rotate 
    android:fromDegrees="0" 
    android:toDegrees="360" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="5000" /> 
</set> 
1

最初のアニメーションのonAnimationStartイベントで2番目のアニメーションを開始して解決しました。左のものを置き換える私の例の右のレイアウトでは、それらの両方が同時に左に動いています。私はViewクラスのアニメーションプロパティを使用していますhttp://developer.android.com/reference/android/view/View.html#animate()はAPI v.12から利用可能です

leftLayout.animate() 
    .translationX(-leftLayout.getWidth()) // minus width 
    .setDuration(300) 
    .setListener(new AnimatorListenerAdapter() { 
     @Override 
     public void onAnimationStart(Animator animation) { 
      rightLayout.animate() 
        .translationX(-leftLayout.getWidth()) // minus width 
        .setDuration(300) 
        .setListener(new AnimatorListenerAdapter() { 
         @Override 
         public void onAnimationEnd(Animator animation) { 
          leftLayout.setVisibility(View.GONE); 
          leftLayout.setTranslationX(0f); // discarding changes 
          rightLayout.setTranslationX(0f); 
         } 
        }); 
     } 
}); 
関連する問題