android-Animating Views Using Scenes and Transitions

来源:互联网 发布:网络教育学位证能买吗 编辑:程序博客网 时间:2024/04/29 10:17

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
Applies one or more animation effects to all of the views in a view hierarchy.
Transition-based animation
Runs animations based on the changes between starting and ending view property values.
Built-in animations
Includes predefined animations for common effects such as fade out or movement.

Resource file support
Loads view hierarchies and built-in animations from layout resource files.
Lifecycle callbacks
Defines callbacks that provide finer control over the animation and hierarchy change process.> The transitions framework provides abstractions for scenes, transitions, and transition managers. 

 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 aTextureView.
  • Classes that extend AdapterView, such as ListView, manage their child views in ways that are incompatible with the transitions framework. If you try to animate a view based on AdapterView, 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.
> The transitions framework can run animations between a starting and an ending scene. The starting scene is often determined automatically from the current state of the user interface. For the ending scene, the framework enables you to create a scene from a layout resource file or from a group of views in your code.

Generate Scenes from Layouts 

Scene mAScene;Scene mAnotherScene;// Create the scene root for the scenes in this appmSceneRoot = (ViewGroup) findViewById(R.id.scene_root);// Create the scenesmAScene = 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 elementmSceneRoot = (ViewGroup) mSomeLayoutElement;// Obtain the view hierarchy to add as a child of// the scene root when this scene is enteredmViewHierarchy = (ViewGroup) someOtherLayoutElement;// Create a scenemScene = 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 layoutthis.setContentView(R.layout.activity_main);...// Create a new TextView and set some View propertiesmLabelText = new TextView();mLabelText.setText("Label").setId("1");// Get the root view and create a transitionmRootView = (ViewGroup) findViewById(R.id.mainLayout);mFade = new Fade(IN);// Start recording changes to the view hierarchyTransitionManager.beginDelayedTransition(mRootView, mFade);// Add the new TextView to the view hierarchymRootView.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) {}}
0 0