安卓资源收集安卓自定义VIEW程序员

Android:Activity过渡动画

2018-05-08  本文已影响308人  lonamessi

这里主要讲Android 5.x中,Google对动画效果进行的修改,主要是Activity的转场动画。

Android 在5.0(API 级别 21)中提供了三种Transition 类型
其中进入和退出的效果包括:

1.explode(分解)-----从屏幕中间进入或退出,移动视图
2.slide(滑动)---------从屏幕边缘进入或退出,移动视图
3.fade(淡出)---------通过改变屏幕上视图的不透明度达到添加或移除视图

共享元素包括:

1.changeBounds -------改变目标视图的布局边界
2.changeClipBounds ------裁剪目标视图边界
3.changeTransform --------改变目标视图的缩放比例和旋转角度
4.changeImageTransform -----改变目标图片的大小和缩放比例

想使用这个Activity的过渡动画,几个步骤非常简单。

1.比如从ActivityA跳转到ActivityB,只需要我们把ActivityA中原来的startActivity(intent)方法改为:

 startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

2.ActivityB中,设置的代码如下:

getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);

3.接下来我们要在ActivityB中写具体的进入的动画效果:

 getWindow().setEnterTransition(new Explode());
  getWindow().setEnterTransition(new Slide());
  getWindow().setEnterTransition(new Fade());

4.在ActivityB中设置离开ActivityB的动画效果

  getWindow().setExitTransition(new Explode());
  getWindow().setExitTransition(new Slide());
  getWindow().setExitrTransition(new Fade());

5.对于共享元素的动画效果,我们首先需要在ActivityA的布局文件中设置共享元素,给它增加相应的属性:

 android:transitionName="xxx"

6.同时在ActivityB的布局中,给要实现元素共享效果的元素也增加相同的属性

android:transitionName="xxx"
这里一定要注意命名要相同

7.如果是一个共享元素,ActivityA中只需要使用如下代码就可以:

startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this,view,"share").toBundle());

8.如果是多个共享元素,那么可以通过Pair.create()来创建多个共享元素:

startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this, Pair.create(view,"share"),Pair.create(fab,"fab")).toBundle());

示例代码:

ActivityA:

//设置不同动画效果
    public void explode(View view){
        intent = new Intent(this, Transitions.class);
        intent.putExtra("flag",0);
        startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    }
    //设置不同动画效果
    public void slide(View view){
        intent = new Intent(this, Transitions.class);
        intent.putExtra("flag",1);
        startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    }
    //设置不同动画效果
    public void fade(View view){
        intent = new Intent(this, Transitions.class);
        intent.putExtra("flag",2);
        startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    }
    //设置不同动画效果
    public void share(View view){
        View fab = findViewById(R.id.fab_button);
        intent = new Intent(this, Transitions.class);
        intent.putExtra("flag",3);
//        startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this,view,"share").toBundle());
        startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this, Pair.create(view,"share"),Pair.create(fab,"fab")).toBundle());

    }

ActivityA的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="lonamessi.palettetext.MainActivity">
    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:onClick="explode"
        android:text="explode"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:onClick="slide"
        android:text="slide" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"

        android:onClick="fade"
        android:text="fade"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:transitionName="share"
        android:onClick="share"
        android:text="share"/>
    <Button
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:id="@+id/fab_button"
        android:transitionName="fab"
        android:background="@mipmap/a"
        android:elevation="5dp"/>
</LinearLayout>

ActivityB的代码:

  getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
        int flag = getIntent().getExtras().getInt("flag");
        switch (flag){
            case 0:
                getWindow().setEnterTransition(new Explode());
                break;
            case 1:
                getWindow().setEnterTransition(new Slide());
                break;
            case 2:
                getWindow().setEnterTransition(new Fade());
                getWindow().setExitTransition(new Fade());
                break;
            case 3:
                break;

        }
        setContentView(R.layout.activity_transition_to);
    }

ActivityB的xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <View
        android:id="@+id/holder_view"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:transitionName="share"
        android:background="@color/colorPrimary"></View>
    <Button
        android:transitionName="fab"
        android:id="@+id/fab_button"
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:background="@mipmap/a"
        android:elevation="5dp"
        android:layout_below="@+id/holder_view"
        android:layout_marginTop="-26dp"
        android:layout_alignParentEnd="true"
        />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="10dp"
        android:layout_below="@+id/holder_view">
        <Button
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/button"
            android:layout_below="@+id/button4"
            android:layout_marginTop="10dp"/>
        <Button

            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginTop="10dp"
            android:id="@+id/button4"
            android:layout_alignParentStart="true"/>

    </RelativeLayout>

</RelativeLayout>
上一篇 下一篇

猜你喜欢

热点阅读