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 

      } 
     }); 



    } 
} 
Смежные вопросы