1 介绍
Transition框架提供了一种动画的方法,可以两个view之间切换时有动画效果( overridePendingTransition()方法不一样)。Transition可以让两个View之间的其中一个组件,如TextView之间有动画效果。
2 框架介绍及使用步骤
2.1 开始和结束Scene,代表开始和结束的两个视图
2.2 Transition表示两个Scene之间共享元素的过渡的动画,如fade等。
2.3 TransitionManager传入 结束Scene和Transition,负责执行过渡。
3 Scene的创建
Scene的创建有两种方式:一种是使用new Scene();另一种是Scene.getSceneForLayout()方法。
第一种方法:目前已经过期,但是也还可以使用。
创建代码示例如下:
//get the layout ID
RelativeLayout baseLayout = (RelativeLayout)findViewById(R.id.base);
//first scene
ViewGroup startViews = (ViewGroup)getLayoutInflater()
.inflate(R.layout.activity_transition_start2, baseLayout, false);
//second scene
ViewGroup endViews = (ViewGroup)getLayoutInflater()
.inflate(R.layout.activity_transition_end2, baseLayout, false);
//create the two scenes
scene1 = new Scene(baseLayout, startViews);
scene2 = new Scene(baseLayout, endViews);
第二种方式,直接使用布局文件创建:
mSceneRoot=(ViewGroup)findViewById(R.id.scene_root);
// Create the scenes
scene1 =Scene.getSceneForLayout(mSceneRoot, R.layout.activity_transition_start3,this);
scene2 =Scene.getSceneForLayout(mSceneRoot, R.layout.activity_transition_end3,this);
从上面示例代码可以看出,第二种方式创建简单,代码较少。
4 Transition
Transition创建也有两种方式,一种是直接在代码里面创建,另一种是在xml创建tansition描述。
第一种创建方式:
transition = new AutoTransition(); // 或者使用new Fade()
transition.setDuration(2000);
transition.setInterpolator(new AccelerateDecelerateInterpolator());
第二种创建方式:这种方式需要在res文件目录下创建transition目录,在目录下创建fade_transition.xml文件
fade_transition.xml文件内容如下:
<autoTransition
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:interpolator="@android:interpolator/cycle"/>
在Acitivity中创建Transition如下:
transition = TransitionInflater.from(this).inflateTransition(R.transition.fade_transition);
// 第一个参数是结束的Scene,第二个参数是第四步定义的Transition动画。
Android自带的Transition动画类型有如下:
至此,Transition框架简单使用就是以上的几个部分。但是在有的时候Transition,并不适用,比如在使用ListView的时候。
- Animations applied to a
SurfaceView
may not appear correctly.SurfaceView
instances are updated from a non-UI thread, so the updates may be out of sync with the animations of other views. - Some specific transition types may not produce the desired animation effect when applied to a
TextureView
. - Classes that extend
AdapterView
, such asListView
, manage their child views in ways that are incompatible with the transitions framework. If you try to animate a view based onAdapterView
, the device display may hang. - If you try to resize a
TextView
with an animation, the text will pop to a new location before the object has completely resized. To avoid this problem, do not animate the resizing of views that contain text
参考:http://developer.android.com/intl/zh-cn/training/transitions/overview.html