android-Animating Views Using Scenes and Transitions
2015-12-31 13:55
691 查看
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
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
To run the animation, you apply the transition using a
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
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
Classes that extend
manage their child views in ways that are incompatible with the transitions framework. If you try to animate a view based on
the device display may hang.
If you try to resize a
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.
To provide custom scene actions, define your actions as
The framework calls the
starting scene before running the transition animation and the
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
activity from a resource file:
》 To create a custom transition, add a class to your project that extends the
and override the methods shown in the following snippet:
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
animateLayoutChangesattribute 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
Transitionobject.
To run the animation, you apply the transition using a
TransitionManagerinstance.
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
SurfaceViewmay not appear correctly.
SurfaceViewinstances
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 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
TextViewwith 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 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
Runnableobjects and pass them to the
Scene.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
Transitioninstance 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
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) {} }
相关文章推荐
- Android Studio快捷键每日一练(2)
- android 4.0 到 4.4的不同(我碰到的)
- android Graphics(一):概述及基本几何图形绘制
- Android Studio代码主题设置
- has leaked window com.android.internal.policy.impl.PhoneWindow$ that was originally added here
- android 创建多线程的几种方法
- as 开发常用的快捷键
- Android Studio实现跨进程调用Service (AILD Service)
- Android 保存bitmap到相册
- Android开发笔记(三十六)展示类控件
- android.support.design.widget.TabLayout的使用
- Android中图片大小和屏幕密度的关系讲解
- 关于红杏的公益代理, Android Studio以及freso的编译
- android socket通信
- android studio系列之插件Android ButterKnife Zelezny
- android studio 工程导入部分注意事项
- 使用AndroidStudio出现的问题(1)
- android 如何判断当前进程在主页面
- Android:防止过快点击造成多次事件
- Android4.4 Framework分析——Zygote进程的启动过程