滑动层活动动画android

时间:2014-12-15 11:19:09

标签: android animation material-design

我想实现活动转换的滑动层动画,如材料设计指南中所述。

Material Transition

然而,我现在所能做的只是slide_in和stay动画的简单组合,它并没有给我一个堆叠层的效果。我怎样才能实现它?

我目前的实施:

在活动开始时:

activity.overridePendingTransition(R.anim.slide_in_right, R.anim.stay);

关于活动结束:

activity.overridePendingTransition(R.anim.stay, R.anim.slide_out_right);

slide_in_right.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0"
        android:duration="@android:integer/config_shortAnimTime" />
</set>

slide_out_right.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <translate
        android:fromXDelta="0"
        android:toXDelta="100%p"
        android:duration="@android:integer/config_shortAnimTime" />
</set>

stay.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_shortAnimTime"
    android:fromYDelta="0%p"
    android:toYDelta="0%p" />

2 个答案:

答案 0 :(得分:7)

我终于找到了解决这个问题的方法。它完美无缺。

本回答中使用的重要组成部分:

  • BackActivity =背景活动
  • FrontActivity =前面滑动的活动
  • BackgroundView = FrontActivity中的基本ViewGroup

解决方案是在关闭FrontActivity之前将FrontActivity的布局设置为动画。 仅当您使用布局中集成的工具栏作为操作栏时才可以使用此功能!

我会在这里复制我的代码。我的动画是一个从底部向前滑动的活动,然后再次滑动到前一个活动前面的底部。只需更改动画,您就可以轻松地在任何方向上获得此效果。


1)在BackActivity中滑动FrontActivity
从BackActivity启动FrontActivity时,只需调用overridePendingTransition

Intent intent = new Intent(activity, FrontActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_bottom, 0);

slide_in_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="100%p"
    android:toYDelta="0%p"
    android:duration="@android:integer/config_mediumAnimTime"
    android:interpolator="@android:anim/decelerate_interpolator">
</translate>

2)从FrontActivity返回BackActivity时,在关闭FrontActivity之前,将FrontActivity的布局设置为动画!

我是通过在我的onOptionsSelected()和我的onBackPressed()中在FrontActivity中调用以下方法来实现此目的

private void animateOut() {
    Animation slideAnim = AnimationUtils.loadAnimation(this,R.anim.slide_out_bottom);
    slideAnim.setFillAfter(true);;
    slideAnim.setAnimationListener(new AnimationListener() {
        public void onAnimationStart(Animation paramAnimation) { }
        public void onAnimationRepeat(Animation paramAnimation) { }
        public void onAnimationEnd(Animation paramAnimation) { 
            finish();
            // if you call NavUtils.navigateUpFromSameTask(activity); instead, 
            // the screen will flicker once after the animation. Since FrontActivity is 
            // in front of BackActivity, calling finish() should give the same result.
            overridePendingTransition(0, 0);
        }
    });
    BackgroundView.startAnimation(slideAnim);
}

slide_out_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0%p"
    android:toYDelta="100%p"
    android:duration="@android:integer/config_mediumAnimTime"
    android:interpolator="@android:anim/accelerate_interpolator">
</translate>

3)现在我们必须确保在设置动画时可以看到BackActivity在FrontActivity后面。
我们需要为此透明主题。

styles.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- your theme -->
    </style>

    <style name="Theme.Transparent" parent="AppTheme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

</resources>

4)将透明主题应用于清单中的FrontActivity:

的AndroidManifest.xml

<activity
      android:name=".FrontActivity"
      android:theme="@style/Theme.Transparent"
      android:parentActivityName=".BackActivity" />

5)由于您的活动现在是透明的,因此您需要为BackgroundView添加背景。 标准背景是:

android:background="@android:color/background_light"
android:background="@android:color/background_dark"

front_activity.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- This is BackgroundView and can be any ViewGroup -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@android:color/background_light" >

    <android.support.v7.widget.Toolbar
        android:layout_height="@dimen/height_toolbar"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <!-- rest of layout -->

</FrameLayout>

那就是它。动画现在应该正常工作。


修改

我找到了一个不会闪烁的解决方案。动画看起来完美无瑕。

关闭FrontActivity时,请拨打finish而不是NavUtils.navigateUpFromSameTask(activity)。我在答案中改变了这一点。

答案 1 :(得分:4)

您可以获取所描述的行为,仅覆盖活动之间的转换。 我还对背部活动进行了淡入淡出效果以改善其外观:

<强> slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0"
        android:duration="@android:integer/config_shortAnimTime"/>
</set>

<强> slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p"
        android:duration="@android:integer/config_shortAnimTime"/>
</set>

<强> fade_back.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:duration="@android:integer/config_shortAnimTime"
        android:pivotX="50.0%"
        android:pivotY="50.0%"
        android:fromXScale="1.0"
        android:toXScale="0.9"
        android:fromYScale="1.0"
        android:toYScale="0.9"/>
    <alpha android:duration="@android:integer/config_shortAnimTime"
        android:fromAlpha="1.0"
        android:toAlpha="0.7"/>
</set>

<强> fade_forward.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:duration="@android:integer/config_shortAnimTime"
        android:pivotX="50.0%"
        android:pivotY="50.0%"
        android:fromXScale="0.9"
        android:toXScale="1.0"
        android:fromYScale="0.9"
        android:toYScale="1.0"/>
    <alpha android:duration="@android:integer/config_shortAnimTime"
        android:fromAlpha="0.7"
        android:toAlpha="1.0"/>
</set>

<强> ParentActivity.java

在您的活动常见的父类中,您可以包含可重复使用的代码:

/* Activity transitions */

protected void slideInTransition() {
    overridePendingTransition(R.anim.slide_in_right, R.anim.fade_back);
}

protected void slideOutTransition() {
    overridePendingTransition(R.anim.fade_forward, R.anim.slide_out_right);
}

然后在活动开始时:

startActivity(intent);
slideInTransition();

对于后退过渡:

@Override
public void onBackPressed() {
    super.onBackPressed();
    slideOutTransition();
}