0

請看看下面的代碼:左向右運動的動畫

left_to_right.xml(動畫)

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
    <translate android:fromXDelta="-100%" android:toXDelta="0%" 
      android:fromYDelta="0%" android:toYDelta="0%" 
      android:duration="700"/> 
</set> 

Java代碼

animation2 = AnimationUtils.loadAnimation(this, R.anim.left_to_right); 
    animation2.setAnimationListener(new AnimationEvent2()); 

    private class AnimationEvent2 implements AnimationListener 
     { 

      @Override 
      public void onAnimationEnd(Animation animation) { 
       // TODO Auto-generated method stub 
       displayIndex++; 
       words.setText(wordList.get(displayIndex)); 

      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onAnimationStart(Animation animation) { 
       // TODO Auto-generated method stub 

      } 

     } 

這部動畫是應用於LinearLayout。激活後,它會從右側消失,但這不是一個動作。我想要的是,當激活時,LinearLayout應該從右向左移動,然後從左角消失(仍然移動,直到它離視圖100%),並且當它完全消失,並且應該出現相同的LinearLayout左邊。

這就像一個HorizontalScroller它滾動,讓其他物體進入位置。但這裏的問題是,這裏同樣的LinearLayout正在離開並回來。

我該怎麼做?

回答

0

我只是用這幾樣東西,可能會有所幫助

AnimationSet set = new AnimationSet(true)  
    Animation animation = new AlphaAnimation(0.0f, 1.0f); 
    animation.setDuration(200); 
    set.addAnimation(animation); 

    animation = new TranslateAnimation(
      Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, 
      Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); 

    animation.setDuration(200); 
    set.addAnimation(animation); 

    LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f); 
    Your_Layout.setLayoutAnimation(controller); 
1

left_to_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
    <translate android:fromXDelta="-100%" android:toXDelta="0%" 
      android:fromYDelta="0%" android:toYDelta="0%" 
      android:duration="700"/> 
</set> 

right_to_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
    <translate 
    android:fromXDelta="0%" android:toXDelta="100%" 
    android:fromYDelta="0%" android:toYDelta="0%" 
    android:duration="700" /> 
</set> 

樣例類明白了吧....

package com.test.testproject; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.animation.Animation; 
import android.view.animation.Animation.AnimationListener; 
import android.view.animation.AnimationUtils; 
import android.widget.LinearLayout; 

public class MainActivity extends Activity { 

    LinearLayout linearLayout ; 
    Animation left_to_right_animation; 
    Animation right_to_left_animation ; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     linearLayout = new LinearLayout(this);//intialise your layout 
     left_to_right_animation = AnimationUtils.loadAnimation(this, R.anim.left_to_right); 
     right_to_left_animation = AnimationUtils.loadAnimation(this, R.anim.right_to_left); 
     linearLayout.startAnimation(left_to_right_animation); 
     left_to_right_animation.setAnimationListener(new AnimationListener() { 

      @Override 
      public void onAnimationStart(Animation animation) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
       // TODO Auto-generated method stub 
       linearLayout.startAnimation(right_to_left_animation); 
      } 
     }); 
     right_to_left_animation.setAnimationListener(new AnimationListener() { 

      @Override 
      public void onAnimationStart(Animation animation) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
       // TODO Auto-generated method stub 

      } 
     }); 



    } 
}