Android学习之5.X过渡动画的实现
来源:互联网 发布:网络进度计划波浪线 编辑:程序博客网 时间:2024/05/28 20:20
你是否对安卓现有的页面跳转动画觉得单一生硬呢?
Android 5.0中,Google对动画效果做了更深的诠释,为Activity的跳转设计更加丰富的动画效果。
看下效果图
由于不会Gif,这图是扣来的,但是我们就是来讲述这种效果是如何实现的
Android 5.X提供了三种Transition类型
进入:一个Activity的所有视图如何进入屏幕
退出:一个Activity的所有视图,如何退出屏幕
共享:一个共享元素过渡动画决定两个Activity之间的过渡,如何共享他们的视图
进入退出效果:
explode(分解)—-从屏幕中间进入或退出
slide(滑动)—-从屏幕边缘进入或退出,(包括状态栏)
fade(淡出)—-类似透明度渐变动画,通过改变透明度来进入或者退出视图
共享元素:
changeBounds —- 改变目标视图的布局边界
changeClipBounds —- 裁剪目标视图边界
changeTransform —–改变目标视图的缩放比例和旋转角度
changeImageTransform —- 改变目标图片的大小和缩放比例
实现方式:
Activity A:
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public void explode(View view){ intent=new Intent(this,Transitions.class); intent.putExtra("flag",0); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle()); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public void slide(View view){ intent=new Intent(this,Transitions.class); intent.putExtra("flag",1); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle()); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public void fade(View view){ intent=new Intent(this,Transitions.class); intent.putExtra("flag",2); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle()); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public void normol(View view){ intent=new Intent(this,Transitions.class); intent.putExtra("flag",3); startActivity(intent); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public void share(View view){ intent=new Intent(this,Transitions.class); intent.putExtra("flag",4); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this,view,"share").toBundle()); }
这里为了比较,设置了explode,slide,fade,normol,share,五种过渡方式,大家可以看下实现的动画效果
Activity B:
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); int flag = getIntent().getExtras().getInt("flag"); switch (flag) { case 0: Explode explode = new Explode(); explode.setDuration(2000); getWindow().setEnterTransition(explode); getWindow().setExitTransition(explode); break; case 1: Slide slide=new Slide(); slide.setDuration(2000); getWindow().setEnterTransition(slide); getWindow().setExitTransition(slide); break; case 2: Fade fade=new Fade(); fade.setDuration(2000); getWindow().setEnterTransition(fade); getWindow().setExitTransition(fade); break; case 3: break; case 4: break; } setContentView(R.layout.transitions);
也可以通过style实现
<style name="myTheme" parent="android:Theme.Material"> <!-- 允许使用transitions --> <item name="android:windowContentTransitions">true</item> <!-- 指定进入和退出transitions --> <item name="android:windowEnterTransition">@transition/explode</item> <item name="android:windowExitTransition">@transition/explode</item> <!-- 指定shared element transitions --> <item name="android:windowSharedElementEnterTransition"> @transition/change_image_transform</item> <item name="android:windowSharedElementExitTransition"> @transition/change_image_transform</item> </style>
这里explode,slide,fade动画都很好理解,也很好实现
共享元素需要讲一下
首先你需要在Activity A中设置共享元素,添加属性
android:transitionName="share"
当然,“share”可以自定义,这个没有局限。
在Activity B中也需要给要实现共享元素效果 的元素增加此属性
android:transitionName="share"
代码中作如下设置
intent=new Intent(this,Transitions.class); intent.putExtra("flag",4); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this,view,"share").toBundle());
实现多个共享元素的话
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this, Pair.create(view1, "agreedName1"), Pair.create(view2, "agreedName2"));
实现效果
注意点:
运行机器必须是api21以上才有效果,因为是基于5.0的嘛,所以用模拟器运行效果小伙伴要注意一下,不然会出现
Caused by: java.lang.NoSuchMethodError: android.app.ActivityOptions.makeSceneTransitionAnimation
好啦,就介绍到这啦,代码大家可以下载运行一下,看下效果。
代码下载
http://pan.baidu.com/s/1kUGCM27
- Android学习之5.X过渡动画的实现
- 群英传之Android 5.X过渡动画
- Android 5.x的Activity过渡动画.
- android 5.x—过渡动画Transition
- Android 5.0学习之Activity过渡动画
- Android 5.0学习之Activity过渡动画
- android过渡动画之makeSceneTransitionAnimation学习笔记
- Android之过渡动画
- Android之过渡动画
- Android 5.X Activity过渡动画,以及漂亮的共享元素效果
- android过渡动画之makeSceneTransitionAnimation
- iOS学习之——自定义过渡动画的实现和使用
- ym—— Android 5.0学习之Activity过渡动画
- Android 5.0学习之Activity共享元素过渡动画
- android5.x之Activity过渡动画
- Android过渡动画之共享元素实现以及遇到的一些问题
- iOS 过渡动画的实现
- android中Activity过渡动画学习
- 分享DCT,DST,Walsh,Hadamard,Haar和Slant图像处理程序
- Android学习中遇到的坑
- Maven常用仓库地址以及手动添加jar包到仓库
- Kafka源码分析之RecordAccumulator
- 多重背包代码
- Android学习之5.X过渡动画的实现
- LeetCode Valid Palindrom
- IF和SWITCH的原理
- 使用 JavaMail 实现邮件发送与收取
- 详解android:scaleType属性
- python中隐式的内存共享
- 安装1.7.1版本PCL开源库
- Struts2源码分析(一)绘制Tomcat启动时时序图并分析
- maven 常用仓库地址