Animating Views Using Scenes and Transitions ? - Creating Custom Transitions
2015-02-27 17:14
591 查看
A custom transition enables you to create an animation that is not available from any of the built-in transition classes. For example, you can define a custom transition that turns the foreground color of text and
input fields to gray to indicate that the fields are disabled in the new screen. This type of change helps users see the fields you disabled.
A custom transition, like one of the built-in transition types, applies animations to child views of both the starting and ending scenes. Unlike built-in transition types, however, you have to provide the code that
captures property values and generates animations. You may also want to define a subset of target views for your animation.
This lesson teaches you to capture property values and generate animations to create custom transitions.
To create a custom transition, add a class to your project that extends the
and override the methods shown in the following snippet:
The following sections explain how to override these methods.
Transition animations use the property animation system described in Property
Animation. Property animations change a view property between a starting and ending value over a specified period of time, so the framework needs to have both the starting and ending value of the property to construct the animation.
However, a property animation usually needs only a small subset of all the view's property values. For example, a color animation needs color property values, while a movement animation needs position property values.
Since the property values needed for an animation are specific to a transition, the transitions framework does not provide every property value to a transition. Instead, the framework invokes callback methods that allow a transition to capture only the property
values it needs and store them in the framework.
To pass the starting view values to the framework, implement the
The framework calls this method for every view in the starting scene. The method argument is a
that contains a reference to the view and a
in which you can store the view values you want. In your implementation, retrieve these property values and pass them back to the framework by storing them in the map.
To ensure that the key for a property value does not conflict with other
use the following naming scheme:
The following snippet shows an implementation of the
The framework calls the
once for every target view in the ending scene. In all other respects,
the same as
The following code snippet shows an implementation of the
In this example, both the
invoke
same, but it has different values in the starting and ending scenes. The framework maintains separate maps for the starting and ending states of a view.
To animate the changes to a view between its state in the starting scene and its state in the ending scene, you provide an animator by overriding the
When the framework calls this method, it passes in the scene root view and the
that contain the starting and ending values you captured.
The number of times the framework calls the
depends on the changes that occur between the starting and ending scenes. For example, consider a fade out/fade in animation implemented as a custom transition. If the starting scene has five targets of which two are removed from the ending scene, and the
ending scene has the three targets from the starting scene plus a new target, then the framework calls
times: three of the calls animate the fading out and fading in of the targets that stay in both scene objects; two more calls animate the fading out of the targets removed from the ending scene; and one call animates the fading in of the new target in the
ending scene.
For target views that exist on both the starting and ending scenes, the framework provides a
for both the
scene, the framework provides a
for the corresponding argument and
To implement the
and return it to the framework. For an example implementation, see the
in the CustomTransitionsample. For more information about property animators, see Property
Animation.
Custom transitions work the same as built-in transitions. You can apply a custom transition using a transition manager, as described in Applying
a Transition.
NEXT CLASS: ADDING ANIMATIONS
input fields to gray to indicate that the fields are disabled in the new screen. This type of change helps users see the fields you disabled.
A custom transition, like one of the built-in transition types, applies animations to child views of both the starting and ending scenes. Unlike built-in transition types, however, you have to provide the code that
captures property values and generates animations. You may also want to define a subset of target views for your animation.
This lesson teaches you to capture property values and generate animations to create custom transitions.
Extend the Transition Class
To create a custom transition, add a class to your project that extends the Transitionclass
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) {} }
The following sections explain how to override these methods.
Capture View Property Values
Transition animations use the property animation system described in PropertyAnimation. Property animations change a view property between a starting and ending value over a specified period of time, so the framework needs to have both the starting and ending value of the property to construct the animation.
However, a property animation usually needs only a small subset of all the view's property values. For example, a color animation needs color property values, while a movement animation needs position property values.
Since the property values needed for an animation are specific to a transition, the transitions framework does not provide every property value to a transition. Instead, the framework invokes callback methods that allow a transition to capture only the property
values it needs and store them in the framework.
Capturing Starting Values
To pass the starting view values to the framework, implement the captureStartValues(transitionValues)method.
The framework calls this method for every view in the starting scene. The method argument is a
TransitionValuesobject
that contains a reference to the view and a
Mapinstance
in which you can store the view values you want. In your implementation, retrieve these property values and pass them back to the framework by storing them in the map.
To ensure that the key for a property value does not conflict with other
TransitionValueskeys,
use the following naming scheme:
package_name:transition_name:property_name
The following snippet shows an implementation of the
captureStartValues()method:
public class CustomTransition extends Transition { // Define a key for storing a property value in // TransitionValues.values with the syntax // package_name:transition_class:property_name to avoid collisions private static final String PROPNAME_BACKGROUND = "com.example.android.customtransition:CustomTransition:background"; @Override public void captureStartValues(TransitionValues transitionValues) { // Call the convenience method captureValues captureValues(transitionValues); } // For the view in transitionValues.view, get the values you // want and put them in transitionValues.values private void captureValues(TransitionValues transitionValues) { // Get a reference to the view View view = transitionValues.view; // Store its background property in the values map transitionValues.values.put(PROPNAME_BACKGROUND, view.getBackground()); } ... }
Capture Ending Values
The framework calls the captureEndValues(TransitionValues)method
once for every target view in the ending scene. In all other respects,
captureEndValues()works
the same as
captureStartValues().
The following code snippet shows an implementation of the
captureEndValues()method:
@Override public void captureEndValues(TransitionValues transitionValues) { captureValues(transitionValues); }
In this example, both the
captureStartValues()and
captureEndValues()methods
invoke
captureValues()to retrieve and store values. The view property that
captureValues()retrieves is the
same, but it has different values in the starting and ending scenes. The framework maintains separate maps for the starting and ending states of a view.
Create a Custom Animator
To animate the changes to a view between its state in the starting scene and its state in the ending scene, you provide an animator by overriding the createAnimator()method.
When the framework calls this method, it passes in the scene root view and the
TransitionValuesobjects
that contain the starting and ending values you captured.
The number of times the framework calls the
createAnimator()method
depends on the changes that occur between the starting and ending scenes. For example, consider a fade out/fade in animation implemented as a custom transition. If the starting scene has five targets of which two are removed from the ending scene, and the
ending scene has the three targets from the starting scene plus a new target, then the framework calls
createAnimator()six
times: three of the calls animate the fading out and fading in of the targets that stay in both scene objects; two more calls animate the fading out of the targets removed from the ending scene; and one call animates the fading in of the new target in the
ending scene.
For target views that exist on both the starting and ending scenes, the framework provides a
TransitionValuesobject
for both the
startValuesand
endValuesarguments. For target views that only exist in the starting or the ending
scene, the framework provides a
TransitionValuesobject
for the corresponding argument and
nullfor the other.
To implement the
createAnimator(ViewGroup, TransitionValues, TransitionValues)method when you create a custom transition, use the view property values you captured to create an
Animatorobject
and return it to the framework. For an example implementation, see the
ChangeColorclass
in the CustomTransitionsample. For more information about property animators, see Property
Animation.
Apply a Custom Transition
Custom transitions work the same as built-in transitions. You can apply a custom transition using a transition manager, as described in Applyinga Transition.
NEXT CLASS: ADDING ANIMATIONS
相关文章推荐
- Animating Views Using Scenes and Transitions - Creating a Scene
- 使用Scenes和Transitions执行动画 -- Animating Views Using Scenes and Transitions
- Animating Views Using Scenes and Transitions - The Transitions Framework
- Animating Views Using Scenes and Transitions - Applying a Transition
- Animating Views Using Scenes and Transitions
- android-Animating Views Using Scenes and Transitions
- Animating Views Using Scenes and Transitions ---
- Creating custom and compound Views in Android - Tutorial(翻译)
- Creating custom headers and footers in Application level events using global.asax
- Creating custom headers and footers in Application level events using global.asax
- Creating Custom Annotations and Using Them
- Creating and Using custom controlsin VC++
- Creating custom and compound views in Android - Tutorial
- custom control的使用方法(Creating and Using custom controls in VC++)
- 论文笔记:Structure Inference Net: Object Detection Using Scene-Level Context and Instance-Level Relationships
- Collection Views and Building Custom Layouts-备
- Creating and Using a COM Object by Using Visual Studio 2005
- Creating and Using a Dynamic Link Library (C++)
- How To Print Using Custom Page Sizes on Windows NT and Windows 2000(VB6)
- Implementing and Using Custom Drawable States