从左到右的动画动画

时间:2013-12-09 08:56:46

标签: android android-layout animation user-interface android-animation

请查看以下代码:

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正在走开并回来。

我该怎么做?

2 个答案:

答案 0 :(得分: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

            }
        });



    }
}

答案 1 :(得分: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);