Activity过渡动画

<span style="font-family: 微软雅黑; widows: auto; background-color: rgb(255, 255, 255);">1.Android5.X提供了三种Transition类型</span>
1)进入:Activity所有视图进入屏幕。
2)退出:Activity所有视图退出屏幕。
3)共享元素:Activity之间的过渡。

进入和退出效果包括:
explode(分解):从屏幕中间进或出,移动视图。
side(滑动):从屏幕边缘进或出,移动视图。
fade(淡出):通过改变屏幕上视图的不透明度达到添加或者移除视图。

共享元素包括:
changeBounds:改变目标后视图的布局边界。
changeClipBounds:裁剪目标视图边界。
changeTransform:改变目标视图的缩放比例和旋转角度。
changeImageTransform:改变目标图片的大小和缩放比例。
共享元素就是两个元素共同拥有的,当从ActivityA跳转到ActivityB时,其余元素都会不见,只有共享元素会通过动画跳转到ActivityB。
设置共享元素步骤:
1)首先在Activity1的布局文件中设置共享的元素,设置相应的属性: android:transitionName=“”;
<Button
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:onClick="share"
    android:text="share"
    android:transitionName="share"
    />
2) 同时在Activity2的布局文件中给要实现共享效果的元素也添加相同的属性: android:transitionName=“”;
<View
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:id="@+id/holder_view"
    android:transitionName="share"
    android:background="?android:colorPrimary"
    />
注意:两处的命名要相同。

共享一个元素的例子:
startActivity(intent,ActivityOptions.makeSceneTrannsitionAnimation(this, view(要共享的View布局), "share"(所取的名字)).toBundle());
如果有多个共享的例子,Pair.create()来创建多个共享元素:
startActivity(intent,ActivityOptions.makeSceneTrannsitionAnimation(this, Pair.create(view, "share"),
Pair.create(fab,"fab")).toBundle());
使用动画的步骤:
从ActivityA跳转到ActivityB,只需在ActivityA中 将基本的startActivity(intent)方法改为: startActivity(intent,ActivityOptions.makeSceneTrannsitionAnimation(this).toBundle ());
在ActivityB中,设置: 允许使用transitions. getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); 或者在样式文件中设置代码:
<item name="android:window.FEATURE_CONTENT_TRANSITIONS" >
然后再设置进入ActivityB的具体动画效果:
getWindow().setEnterTransition(new Explode());
getWindow().setEnterTransition(new Slide());
getWindow().setEnterTransition(new Fade());

或者设置离开ActivityB的动画效果:
getWindow().setExitTransition(new Explode() );
getWindow().setExitTransition(new Slide());
getWindow().setExitTransition(new Fade () );

例如:
在ActivityA中:
/从屏幕中间进或出
public void explode(View viwe){
    intent = new Intent(this, Transitions.class);
    intent.putExtra("flag", 0);
    startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    Log.e("flag----", "" + 0);
}

//从屏幕边缘进或出
public void slide(View view){
    intent = new Intent(this, Transitions.class);
    intent.putExtra("flag", 1);
    startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    Log.e("flag----",""+1);
}

//从屏幕淡出
public void fade(View view){
    intent = new Intent(this, Transitions.class);
    intent.putExtra("flag", 2);
    startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    Log.e("flag----", "" + 2);
}

//设置共享元素
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());
    Log.e("flag----", "" + 4);
}

在ActivityB中:
switch (flag){
    case 0:
        getWindow().setEnterTransition(new Explode());
        break;

    case 1:
        getWindow().setEnterTransition(new Slide());
        break;

    case 2:
        getWindow().setEnterTransition(new Fade());
        getWindow().setEnterTransition(new Fade());
        break;
    case 3:break;
}

效果:







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值