The property animation system also provides the ability to add animations to view changes in the ViewGroup.
You can LayoutTransition
animate the view changes in the ViewGroup by using the display.
Note that the animation effect in this article is set to the container (ViewGroup), but the effect is reflected by the view that the container holds.
Four types of container conversion animations
When you add or remove a view from ViewGroup, or you call the view's setvisibility () method to control its display or disappearance, it is in a transition state. This event is likely to excite the animation.
A view that is currently added or removed can undergo an animated animation or a vanishing animation.
And it's not just the other view in the view,viewgroup that you want to control that can change, like going through an animation and moving to a new location.
so there are four kinds of related animation types :
Animation of the appearance of the 1.View itself;
2. Vanishing animation;
3. Because of the addition of other view and need to change the location of the animation;
4. Animations that need to change location because other view is removed.
(If the position of the current view does not need to change after adding or removing other view, no animation).
You can customize these animations by using the setanimator () method to set them into an LayoutTransition
object.
A Animator object and a constant are required when setting:
appearing -A flag indicating the animation that runs on items is appearing in the container.
change_appearing -A flag indicating the animation that runs in items that is changing due to A new item Appeari Ng in the container.
disappearing -A flag indicating the animation that runs in items that is disappearing from the container.
change_disappearing -A flag indicating the animation that runs in items that is changing due to an item DISAP Pearing from the container.
You can define the animations for these four types of events yourself, or you can use the default animations.
Finally setLayoutTransition(LayoutTransition)
, these animations LayoutTransition
are set to a viewgroup in the form of an object by means of the method.
For example, the following method generates a completely new Layouttransition object and sets it to the container (viewgroup type), so that four animations are all default animations.
Regenerate the Layouttransition object and set it to container private void Resettransition () { Mtransitioner = new Layouttransition ( ); Container.setlayouttransition (Mtransitioner); }
Generate four custom animations for this Mtransitioner object:
Generate Custom Animation private void Setupcustomanimations () {//Animation: change_appearing//changing while Adding Propertyvaluesholder pvhleft = Propertyvaluesholder.ofint ("left", 0, 1); Propertyvaluesholder pvhtop = Propertyvaluesholder.ofint ("Top", 0, 1); Propertyvaluesholder pvhright = Propertyvaluesholder.ofint ("Right", 0, 1); Propertyvaluesholder Pvhbottom = Propertyvaluesholder.ofint ("Bottom", 0, 1); Propertyvaluesholder Pvhscalex = propertyvaluesholder.offloat ("ScaleX", 1f, 0f, 1f); Propertyvaluesholder Pvhscaley = propertyvaluesholder.offloat ("ScaleY", 1f, 0f, 1f); Final Objectanimator Changein = Objectanimator.ofpropertyvaluesholder (this, Pvhleft, Pvhtop, Pvhright, PVH Bottom, Pvhscalex, Pvhscaley). Setduration (Mtransitioner.getduration (layouttransition.change _appearing)); Mtransitioner.setanimator (LayouttransitioN.change_appearing, Changein); Changein.addlistener (New Animatorlisteneradapter () {public void Onanimationend (Animator anim) { View view = (view) ((Objectanimator) anim). Gettarget (); View.setscalex (1f); View.setscaley (1f); } }); Animation: change_disappearing//changing while removing keyframe kf0 = Keyframe.offloat (0f, 0f); Keyframe Kf1 = Keyframe.offloat (. 9999f, 360f); Keyframe Kf2 = keyframe.offloat (1f, 0f); Propertyvaluesholder pvhrotation = propertyvaluesholder.ofkeyframe ("Rotation", kf0, Kf1, KF2); Final Objectanimator changeout = Objectanimator Ofpropertyvaluesholder (this, Pvhleft, Pvhtop, Pvhright, Pvhbottom, Pvhrotation). Setduration (Mtransitioner . Getduration (layouttransition.change_disappearing)); Mtransitioner.setanimator (layouttransition.change_disappearing, changeout); Changeout.addlistener (New Animatorlisteneradapter () {public void Onanimationend (Animator anim) { View view = (view) ((Objectanimator) anim). Gettarget (); View.setrotation (0f); } }); Animation: Appearing//Adding objectanimator animin = objectanimator.offloat (null, "RotationY", 90f, 0f). Setduration (Mtransitioner.getduration (layouttransition.appearing)); Mtransitioner.setanimator (layouttransition.appearing, animin); Animin.addlistener (New Animatorlisteneradapter () {public void Onanimationend (Animator anim) {V Iew view = (view) ((Objectanimator) anim). Gettarget (); View.setrotationy (0f); } }); Animation: Disappearing//removing objectanimator animout = objectanimator.offloat (null, "RotationX", 0f, 90f). setdUration (Mtransitioner.getduration (layouttransition.disappearing)); Mtransitioner.setanimator (layouttransition.disappearing, animout); Animout.addlistener (New Animatorlisteneradapter () {public void Onanimationend (Animator anim) { View view = (view) ((Objectanimator) anim). Gettarget (); View.setrotationx (0f); } }); }
Default layout transition animations
If you want to use the default animation, a very simple way is to set the android:animateLayoutchanges
property to True in the ViewGroup XML layout file.
This automatically animates the view that you want to remove or add, as well as the other view in the group, by default.
such as the Layoutanimationsbydefault in Apidemos:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:id= "@+id/addnewbutton" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:text=" Add button "/> <!-- < GridLayout android:columncount= "4" android:layout_width= "wrap_content" android:layout_height= " Wrap_content " android:id=" @+id/gridcontainer " android:animatelayoutchanges=" true " /> -- > <linearlayout android:id= "@+id/gridcontainer" android:layout_width= "Wrap_content " android:layout_height= "Wrap_content" android:animatelayoutchanges= "true" android:orientation= " Vertical "> </LinearLayout></LinearLayout>
I changed the layout to a linear layout, as long as the ViewGroup type is available.
By default, disappearing and change_appearing animations start immediately, and other animations have a default start delay.
This is because, for example: When a new view appears, the other view to immediately perform the change_appearing animation to make a position, and the new view in a certain delay after the execution of appearing appear;
Conversely, when a view disappears, it needs to disappearing the animation before it disappears, while the other view needs to wait until it disappears before executing change_disappearing.
Of course, these default behaviors can be setDuration(int, long)
changed through and setStartDelay(int, long)
other methods.
API Demos Code
The classes related to layout animation in Apidemos are: Layoutanimationsbydefault, Layoutanimations, Layoutanimationshideshow.
The complete project can be downloaded to GitHub. Https://github.com/mengdd/AnimationApiDemos
Resources
API Guides:property Animation
Http://developer.android.com/guide/topics/graphics/prop-animation.html
One of the animating Layout changes to Viewgroups
Layouttransition class Reference:
Http://developer.android.com/reference/android/animation/LayoutTransition.html
Project Address:
Https://github.com/mengdd/AnimationApiDemos
Android Animation Learning (v) Apidemos parsing: container layout animation layouttransition