根据材料设计从右到左和从左到右重叠片段的动画

时间:2014-12-21 13:00:25

标签: android animation fragment overlap material

我正在尝试实现像Google材料设计文档中推荐的那样重叠一个片段的解决方案,但不幸的是我还没有找到api v9的方法(android 2.3) 有人可以帮我吗?我会很感激动画的xml示例,但代码也可以。

Link with animation

Animation Meaningful transitions page in Google docs

2 个答案:

答案 0 :(得分:0)

我还没有在实际的api v9设备上测试它,但所有的东西都可用,因为较低的API,所以请尝试一下:

activity1转到activity2时使用:

overridePendingTransition(R.anim.slide_in_right, R.anim.shade);

回去时使用:

overridePendingTransition(R.anim.unshade, R.anim.slide_out_right);

其中:

slide_in_right.xml

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

shade.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator">
    <alpha android:duration="1000" android:fromAlpha="1.0" android:toAlpha="0.7" />
</set>

unshade.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator">
    <alpha android:duration="1000" android:fromAlpha="0.7" android:toAlpha="1" />
</set>

slide_out_right.xml

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

答案 1 :(得分:0)

在代码中实现BaseActivity,扩展Activity并在其onCreate()内实现此

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //opening transition animations
        overridePendingTransition(R.anim.activity_open_translate,R.anim.activity_close_scale);
    }

创建两个动画文件。activity_open_translate的代码段是

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

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate android:fromXDelta="100%"
               android:toXDelta="0%"
               android:duration="@android:integer/config_mediumAnimTime" />

</set>

activty_close_scale的摘要是

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

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale android:fromXScale="100%p"
           android:toXScale="80%p"
           android:fromYScale="100%p"
           android:toYScale="80%p"
           android:pivotX="50%p"
           android:pivotY="50%p"
           android:duration="@android:integer/config_mediumAnimTime" />

    <alpha android:fromAlpha="1"
           android:toAlpha="0.5"
           android:duration="@android:integer/config_mediumAnimTime"/>

</set>

现在您要做的就是将此BaseActivity类扩展到您想要的Activity