Android Properties Animation (property Animation) Full Parse (bottom)

Source: Internet
Author: User

Reprint please indicate source: http://blog.csdn.net/lmj623565791/article/details/38092093

The previous Android attribute animation (property Animation) fully parsed (above) has basically shown the core usage of property animations:

Objectanimator realization Animation, Valueanimator realization Animation, animatorset use and so on ~

Of course, the property animation has a part of the knowledge point, but also can make a very good effect, will be in this blog to show you ~

1. How to use XML files to create property animations

As you all know, the View Animator, drawable Animator can create animations under the Anim folder, then use them in the program, and even set them as property values in theme. Of course, property animations can also be declared in a file:

First create the Animator folder under Res, and then build the Res/animator/scalex.xml

<?xml version= "1.0" encoding= "Utf-8"? ><objectanimator xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:duration=" "    android:propertyname=" ScaleX "    android:valuefrom=" 1.0 "    Android : valueto= "2.0"    android:valuetype= "Floattype" ></objectAnimator>
Code:

public void ScaleX (view view) {//Load animation Animator Anim = Animatorinflater.loadanimator (this, r.animator.scalex); Anim.settarget (MMV); Anim.start ();}
Using Animatorinflater to load the animated resource file, and then set the target, the ok~~ is not very simple, this is just a horizontal magnification one times ~

What if I want to scale both vertically and horizontally? You can define the properties file:

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android"    android:ordering= "Together" >    <objectanimator        android:duration= "$"        android: Propertyname= "ScaleX"        android:valuefrom= "1"        android:valueto= "0.5" >    </objectAnimator>    <objectanimator        android:duration= "android:propertyname="        ScaleY "        android:valuefrom=" 1 "        android:valueto= "0.5" >    </objectAnimator></set>

With the set label, one of the orderring properties is set to together, "there is another value: sequentially (indicating one after another)."

One of the effects in the previous blog was that zooming, reversing, and so on had a center point or axis, the default center scaling, and the middle symmetry line as the reversal line, so I decided to zoom in and out with the top left corner as the center point:

Code:

Load animation Animator Anim = Animatorinflater.loadanimator (this, r.animator.scale); mmv.setpivotx (0); Mmv.setpivoty (0);// The displayed call Invalidatemmv.invalidate (); Anim.settarget (mMv); Anim.start ();

Very simple, directly to the view set Pivotx and Pivoty, and then call invalidate, OK.

See below:

Well, by writing an XML declaration animation, using set nested set, combined with the Orderring property, you can also basically implement any animation ~ ~ Above also demonstrates the setting of pivot.

2. Layout animation (layouts animations)

The main use of layouttransition to animate the layout of the container, when the view hierarchy in the container changes when there is a transition animation effect.

The basic code is:

Layouttransition transition = new Layouttransition (); Transition.setanimator (layouttransition.change_appearing, Transition.getanimator (layouttransition.change_appearing)); Transition.setanimator (LayoutTransition.APPEARING, NULL); Transition.setanimator (layouttransition.disappearing,null); Transition.setanimator ( Layouttransition.change_disappearing,null); mgridlayout.setlayouttransition (transition);

There are four types of transitions:

Layouttransition.appearing When a view appears in ViewGroup, the animation for this view setting

Layouttransition.change_appearing When a view appears in ViewGroup, this view affects other view locations, and animations for other view settings

Layouttransition.disappearing when a view disappears in ViewGroup, the animation for this view setting

Layouttransition.change_disappearing when a view disappears in ViewGroup, this view affects other view locations and animations for other view settings

Layouttransition.change is not an animation of other view settings due to the appearance or disappearance of the view that affects other view locations.

Notice who the animation is set to, and this view is the other view.

Okay, here's a comprehensive example:

Layout file:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:id=" @+id/id_container "android:layout_width=" Match_parent "Android:layout_heig ht= "match_parent" android:orientation= "vertical" > <button android:layout_width= "Wrap_content" a        ndroid:layout_height= "Wrap_content" android:onclick= "addbtn" android:text= "Addbtns"/> <CheckBox        Android:id= "@+id/id_appear" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:checked= "true" android:text= "appearing"/> <checkbox android:id= "@+id/id_change_appear        "Android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:checked=" true " android:text= "change_appearing"/> <checkbox android:id= "@+id/id_disappear" Android:layout_widt H= "Wrap_content" android:layout_height= "Wrap_content "android:checked=" true "android:text=" disappearing "/> <checkbox android:id=" @+id/id_change_disappear "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "an Droid:checked= "true" android:text= "change_disappearing"/></linearlayout>

Code:

Package Com.example.zhy_property_animation;import Android.animation.layouttransition;import android.app.Activity; Import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.button;import Android.widget.checkbox;import Android.widget.compoundbutton;import Android.widget.compoundbutton.oncheckedchangelistener;import Android.widget.gridlayout;public class Layoutanimaactivity extends Activity implementsoncheckedchangelistener{ Private ViewGroup viewgroup;private GridLayout mgridlayout;private int mval;private layouttransition mtransition; Private CheckBox Mappear, Mchangeappear, Mdisappear, mchangedisappear; @Overridepublic void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.layout_animator); ViewGroup = ( ViewGroup) Findviewbyid (r.id.id_container) mappear = (CheckBox) Findviewbyid (r.id.id_appear); mchangeappear = ( CheckBox) Findviewbyid (r.id.id_change_appear); MdisapPear = (checkbox) Findviewbyid (r.id.id_disappear); mchangedisappear = (checkbox) Findviewbyid (R.id.id_change_ disappear); Mappear.setoncheckedchangelistener (this), Mchangeappear.setoncheckedchangelistener (this); Mdisappear.setoncheckedchangelistener (this); Mchangedisappear.setoncheckedchangelistener (this);// Create a gridlayoutmgridlayout = new GridLayout (this);//Set 5 buttons per column mgridlayout.setcolumncount (5);// Add to Layout Viewgroup.addview (mgridlayout);//Default animation all open mtransition = new layouttransition (); Mgridlayout.setlayouttransition (mtransition);}  /** * Add button * * @param view */public void addbtn (view view) {Final button button = New button (this); Button.settext ((++mval) + ""); Mgridlayout.addview (Button, math.min (1, Mgridlayout.getchildcount ())); Button.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {mgridlayout.removeview (button);}});} @Overridepublic void OnCheckedChanged (Compoundbutton buttonview, Boolean isChecked) {mtransition = new layouttransition (); Mtransition.setanimator (layouttrAnsition. Appearing, (mappear.ischecked () Mtransition.getanimator (layouttransition.appearing): null)); Mtransition.setanimator (Layouttransition.change_appearing, (mchangeappear.ischecked ()? MTransition.getAnimator ( layouttransition.change_appearing): null); Mtransition.setanimator (Layouttransition.disappearing, ( Mdisappear.ischecked ()? Mtransition.getanimator (layouttransition.disappearing): null); Mtransition.setanimator (LayoutTransition.CHANGE_ Disappearing, (mchangedisappear.ischecked ()? Mtransition.getanimator (layouttransition.change_disappearing): null) ); Mgridlayout.setlayouttransition (mtransition);}}


Animation is a bit long, patience to see, it must be noted, is the current view or other views set animation.

Of course. Animation support customization, but also support the setting of time, such as we modified, added animation is:

Mtransition.setanimator (layouttransition.appearing, (mappear.ischecked)? Objectanimator.offloat (This, "ScaleX", 0, 1): null);

The effect is:


The original fade in, become the width from the middle of the effect of amplification ~ ~ is not bad ~ ~

3. Anim method of view

In SDK11, the Animate method is added to the view to make the animation more convenient.

Layout file:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "> < ImageView android:id= "@+id/id_ball" android:layout_width= "wrap_content" android:layout_height= "Wrap_c        Ontent "android:src=" @drawable/bol_blue "/> <linearlayout android:layout_width=" Fill_parent " android:layout_height= "Wrap_content" android:layout_alignparentbottom= "true" android:orientation= "Horizonta            L "> <button android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:onclick= "Viewanim" android:text= "View Anim"/> <button android:layout            _width= "Wrap_content" android:layout_height= "wrap_content" android:onclick= "Propertyvaluesholder" android:text= "Propertyvaluesholder" /> </LinearLayout></RelativeLayout> 
Code:

Package Com.example.zhy_property_animation;import Android.animation.objectanimator;import Android.animation.propertyvaluesholder;import Android.app.activity;import Android.os.bundle;import Android.util.displaymetrics;import Android.util.log;import Android.view.view;import Android.widget.ImageView; public class Viewanimateactivity extends activity{protected static final String TAG = "viewanimateactivity";p rivate Image View mblueball;private float mscreenheight; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.view_animator);D isplaymetrics outMetrics = new Displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (outmetrics); mscreenheight = Outmetrics.heightpixels;mblueball = (ImageView) Findviewbyid (R.id.id_ball);} public void Viewanim (view view) {//Need api12mblueball.animate ()//.alpha (0)//.y (MSCREENHEIGHT/2). Setduration (1000)/ /Need API 12.withStartAction (new Runnable () {@Overridepublic void run () {LOG.E (TAG, "START ");} Need API). Withendaction (New Runnable () {@Overridepublic void run () {LOG.E (TAG, "END"), Runonuithread (New Runnable ( {@Overridepublic void Run () {mblueball.sety (0); Mblueball.setalpha (1.0f);}});}).                                                                                                                                                  Start ();} }

Simple use of mblueball.animate (). Alpha (0). Y (MSCREENHEIGHT/2). Setduration () Start () can be animated ~ ~ but need SDK11, then in SDK12, SDK16 added Withstartaction and withendaction to perform some actions before and after the animation. Of course, you can. Setlistener (listener) and other operations.

Using Objectanimator to achieve the above changes, we can use: Propertyvalueholder

Propertyvaluesholder PVHX = propertyvaluesholder.offloat ("Alpha", 1f,0f, 1f); Propertyvaluesholder pvhy = propertyvaluesholder.offloat ("y", 0,MSCREENHEIGHT/2, 0); O Bjectanimator.ofpropertyvaluesholder (Mblueball, PVHX, Pvhy). Setduration (+). Start ();

The effect is the same as above.

Operation Result:




OK, about the property animation basic all the usage to this end ~ ~ ~


SOURCE Click to download




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.