Android includes the transitions framework, which enables you to easily animate changes between two view hierarchies.
Note: For Android versions earlier than 4.4.2 (API level 19) but greater than or equal to Android 4.0 (API level 14), use the animateLayoutChanges
attribute to animate layouts. To learn more, see Property Animation andAnimating Layout Changes.
To help you animate a change between one view hierarchy and another, Android provides the transitions framework.
》The trasnations framework has the following features:
Group-level animations
Information about the animation is stored in a Transition
object. To run the animation, you apply the transition using a TransitionManager
instance. The framework can transition between two different scenes or transition to a different state for the current scene.
You can also define your own custom transitions to create an animation effect using the APIs in the animations framework. The transitions framework also enables you to combine different animation effects in a transition set that contains a group of individual built-in or custom transitions.
This section lists some known limitations of the transitions framework:
- 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.
Generate Scenes from Layouts
Scene mAScene; Scene mAnotherScene; // Create the scene root for the scenes in this app mSceneRoot = (ViewGroup) findViewById(R.id.scene_root); // Create the scenes mAScene = Scene.getSceneForLayout(mSceneRoot, R.layout.a_scene, this); mAnotherScene = Scene.getSceneForLayout(mSceneRoot, R.layout.another_scene, this);
Create a Scene in Your Code
Scene mScene; // Obtain the scene root element mSceneRoot = (ViewGroup) mSomeLayoutElement; // Obtain the view hierarchy to add as a child of // the scene root when this scene is entered mViewHierarchy = (ViewGroup) someOtherLayoutElement; // Create a scene mScene = new Scene(mSceneRoot, mViewHierarchy);
To provide custom scene actions, define your actions as Runnable
objects and pass them to theScene.setExitAction()
or Scene.setEnterAction()
methods. The framework calls the setExitAction()
method on the starting scene before running the transition animation and the setEnterAction()
method on the ending scene after running the transition animation.
Note: Do not use scene actions to pass data between views in the starting and ending scenes. For more information, see Defining Transition Lifecycle Callbacks.
> In the transitions framework, animations create a series of frames that depict a change between the view hierarchies in the starting and ending scenes.
The following code snippet shows how to inflate a Transition
instance inside your activity from a resource file:
Transition mFadeTransition = TransitionInflater.from(this). inflateTransition(R.transition.fade_transition);
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android" android:transitionOrdering="sequential"> <fade android:fadingMode="fade_out" /> <changeBounds /> <fade android:fadingMode="fade_in" /> </transitionSet>
private TextView mLabelText; private Fade mFade; private ViewGroup mRootView; ... // Load the layout this.setContentView(R.layout.activity_main); ... // Create a new TextView and set some View properties mLabelText = new TextView(); mLabelText.setText("Label").setId("1"); // Get the root view and create a transition mRootView = (ViewGroup) findViewById(R.id.mainLayout); mFade = new Fade(IN); // Start recording changes to the view hierarchy TransitionManager.beginDelayedTransition(mRootView, mFade); // Add the new TextView to the view hierarchy mRootView.addView(mLabelText); // When the system redraws the screen to show this update, // the framework will animate the addition as a fade in》 To create a custom transition, add a class to your project that extends the
Transition
class and override the methods shown in the following snippet:
public class CustomTransition extends Transition { @Override public void captureStartValues(TransitionValues values) {} @Override public void captureEndValues(TransitionValues values) {} @Override public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {} }