Android动画 View 从右到左

标签 android animation view transition

我制作了一个小应用程序,并且是 Android 动画/过渡方面的新手。

我想要什么: 如果您按下一个按钮,背景( View )应该滑出,另一个应该进入,但我不想启动其他 Activity ,我只想更改现有的背景。

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

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

如果您明白我的意思,最好给我一个提示或教程链接或其他东西。

编辑 - 解决方案

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

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

Slide right to left Android Animations (xml 表示从右到左,从上到下,...)

向左过渡的示例:

代码:

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);
    }

res/anim/slide_right_in 中的 XML 转换

<?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 个 View ,它们都有屏幕尺寸(match_parent)。一个是可见的,另一个是不可见的。 然后你将可见的函数作为 viewgo (因为这应该消失),将不可见的作为 viewcome (因为这应该是新进来的) 在动画开始时,视域将设置为可见,因为它“滑入”。 动画完成后,viewgo 将不可见,因为它“滑出”。 这非常简单并且效果很好。

为了改进动画,您可以使用 AnimatorSet 来同步两个动画 (set.playtogether()),但即使没有它,它也对我有用。

最佳答案

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

在 jour xml 文件中在同一位置设置两个 View ,并将第二个 View 的可见性设置为不可见:

    <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);

关于Android动画 View 从右到左,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36809994/

相关文章:

xcode - 在标签栏 Controller 中的 View Controller 之间切换时停止运行代码

android - 运行时保留旧的应用程序数据 'flutter install'

android - 如何为AlertDialog设置自定义布局?

php - 为 PHP 查询创建 GridView

python - 动画后 matplotlib 退出

javascript - CSS 动画无法启动

android - 使用 match_parent 属性获取 View 宽度?

android - 为什么不同手机的编辑文字样式不一样

java - 在android中使用海绵城堡

wpf - 加载控件后立即启动模板动画