Android 动画视图从右到左

Android animation view right to left

我制作了一个小应用程序,我是 android animations/transition 中的新手。

我想要什么: 如果你按下一个按钮,背景(视图)应该滑出,另一个应该进来,但我不想开始另一个 activity,我只想改变现有的背景。

这正是我想要的:https://www.polymer-project.org/0.5/components/core-animated-pages/demos/simple.html (一定要在左上角的方框里换个方框才能看到)

我在其他帖子中读到了一些关于它的内容,但通常有启动另一个的解决方案 activity..

如果你明白我的意思,最好给我一个提示或 link 教程之类的东西。

编辑 - 解决方案

我让它为我工作,我构建了一个完全符合我想做的代码,我给出了所有答案 1 并标记了最后一个,因为它接近我所做的。 感谢大家。

这个 link 非常有用: https://github.com/codepath/android_guides/wiki/Animations(迄今为止我找到的最好的动画教程)

Slide right to left Android Animations(从右到左,从上到下的 xmls,...)

左转示例:

代码:

public void transitionleft(final View viewcome, final View viewgo){
        animcome = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_right_in);
        animgo = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_left_out);
        animgo.setDuration(1000);
        animcome.setDuration(1000);

        animcome.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                viewcome.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

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

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                viewgo.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        viewcome.startAnimation(animcome);
        viewgo.startAnimation(animgo);
    }

XML 过渡 res/anim/slide_right_in

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

res/anim/slide_left_out

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

工作原理: 我有 2 个视图,它们都具有屏幕尺寸 (match_parent)。一个可见,另一个不可见。 然后你将可见的函数作为 viewgo (因为这应该消失)和不可见的作为 viewcome (因为这应该是新来的人) 在动画开始时,viewcome 将设置为可见,因为它 "slide in"。 动画完成后,viewgo 将不可见,因为它 "slide out"。 非常简单,效果很好。

要改进动画,您可以使用 AnimatorSet 来同步两个动画 (set.playtogether()),但如果没有它,它对我来说也很好。

也许你可以试试 valueAnimator:

//animate from your current color to red ValueAnimator 
anim = ValueAnimator.ofInt(view.getBackgroundColor(), 
           Color.parseColor("#FF0000")); 
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
    @Override public void onAnimationUpdate(ValueAnimator animation) {       
         view.setBackgroundColor(animation.getAnimatedValue()); 
    } 
}); 
anim.start();
 public class MainActivity extends Activity implements OnGestureListener {    
    private LinearLayout main;    
    private TextView viewA;

    private GestureDetector gestureScanner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //this will set default color to black
        View view = findViewById(R.id.textView);
        view.setBackgroundColor(Color.BLACK);
    }

    @Override
    public boolean onTouchEvent(MotionEvent me) {
        return gestureScanner.onTouchEvent(me);

    }

    @Override
    public boolean onDown(MotionEvent e) {
        viewA.setText("-" + "DOWN" + "-");
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        viewA.setText("-" + "FLING" + "-");

        //changes color on swipe
        view.setBackgroundColor(Color.GREEN);
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        viewA.setText("-" + "LONG PRESS" + "-");
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        viewA.setText("-" + "SCROLL" + "-");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        viewA.setText("-" + "SHOW PRESS" + "-");
    }    

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        viewA.setText("-" + "SINGLE TAP UP" + "-");
        return true;
    }
}

这里在onCreate中,默认颜色设置为黑色。现在,每当检测到 'event' 时,如触摸、滑动(滑动)、滚动等,您的应用程序应该改变颜色。 在这里,我在 Fling 上更改了颜色,您可以修改它以获得随机颜色,甚至在其他事件侦听器中也可以这样做。

This link 应该会为您指明正确的方向。

希望这对您有所帮助。

编辑:

转场效果:

animatedBackgroundView.setBackgroundResource(R.anim.background_touch);
animatedBackgroundView.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    TransitionDrawable transition = (TransitionDrawable) view.getBackground();
    transition.startTransition(500);
    transition.reverseTransition(500);
}
});

您的后台资源文件:

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#22ffffff" />
<item android:drawable="#00ffffff" />
</transition>

这是一个完整的解决方案:

在 xml 文件中,在同一位置设置两个视图并将第二个视图的可见性设置为 invisible :

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/currentView"
        android:text="currentView"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="invisible"
        android:id="@+id/nextView"
        android:text="nextView"/>

    </RelativeLayout>

然后在你的 activity 中:

//First in you onCreate translate the second Layout to the right
comingView.setTranslationX(testbutton.getWidth());

// Here is the method that will do the job
// We slide both views (current one and waiting one) at the same time
// and in the same direction

private void slideExit(View currentView,final View comingView) {
    currentView.animate().
            translationXBy(-currentView.getWidth()).
            setDuration(1000).
            setInterpolator(new LinearInterpolator()).
            setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationStart(Animator animation) {
                    super.onAnimationStart(animation);
                    slideFromRightToLeft(comingView);
                    comingView.setVisibility(View.VISIBLE);
                }
            });
}

private void slideFromRightToLeft(View v) {
    v.animate().
            translationX(0).
            setDuration(1000).
            setInterpolator(new LinearInterpolator());
}

//call the method whenever you want
slideExit(currentView, comingView);