Animation of Android (3)

Source: Internet
Author: User

View animations, only view can be used.

After android3.0, the property is animated.

Valueanimation can record the process of a property change, so his object is anything.

So the real purpose of valueanimation is to make a series of value-change functions based on setinterpolator for a certain value of object.

While Valueanimation has a Animatorupdatelistener callback to each 10ms interval, the feedback changes with time values.

The specific code is as follows:

 PackageCom.joyfulmath.animatatorsamples;ImportAndroid.animation.ValueAnimator;ImportAndroid.animation.ValueAnimator.AnimatorUpdateListener;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;Importandroid.graphics.Paint.Align;ImportAndroid.graphics.Paint.FontMetrics;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.animation.BounceInterpolator; Public classValueanimationviewextendsView {Private Static FinalString TAG = "Valueanimationview"; PrivateContext Mcontext =NULL; PrivatePaint Mpaint =NULL; Private floatMX = 0; floattopy = 0; PrivateValueanimator Mvalueanimator =NULL;  PublicValueanimationview (Context context) { This(Context,NULL); }     PublicValueanimationview (Context context, AttributeSet attrs) { This(context, Attrs, 0); }     PublicValueanimationview (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle); LOG.D (TAG,"Valueanimationview"); Mcontext=context; } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); LOG.D (TAG,"Ondraw:mx\t" +MX); Canvas.drawcolor (0xffffff00);        Canvas.save (); Canvas.translate (0, 0); Canvas.drawtext (TAG, MX, MX-topy, Mpaint);    Canvas.restore (); } @Overrideprotected voidOnattachedtowindow () {Super. Onattachedtowindow (); LOG.D (TAG,"Onattachedtowindow"); Mpaint=NewPaint (); Mpaint.settextsize (18);        Mpaint.settextalign (Align.left); Mpaint.setcolor (0xffff0000); Mpaint.setantialias (true); FontMetrics FontMetrics=Mpaint.getfontmetrics (); Topy=Fontmetrics.top; floatAscenty =fontmetrics.ascent; floatDescenty =fontmetrics.descent; floatBottomy =Fontmetrics.bottom; LOG.D (TAG,"Onattachedtowindow fontmetrics topy:" +topy+ "\ t ascenty:" + ascenty + "\ t descenty:" +Descenty+ "\ t bottomy:" +bottomy); } @Overrideprotected voidOndetachedfromwindow () {Super. Ondetachedfromwindow (); }         Public voidCreateanimator () {if(Mvalueanimator = =NULL) {Mvalueanimator= Valueanimator.offloat (0.0f, 50.0f); Mvalueanimator.setduration (1000); Mvalueanimator.setinterpolator (Newbounceinterpolator ()); Mvalueanimator.addupdatelistener (NewAnimatorupdatelistener () {@Override Public voidonanimationupdate (valueanimator animation) {MX=(Float) animation.getanimatedvalue ();                Invalidate ();        }            }); }    }         Public voidStartvalueamimator () {log.d (TAG,"Startvalueamimator");        Createanimator ();    Mvalueanimator.start (); }}

The code above is a custom view that implements the basics of animation.

The key is to get the properties of the Objcet animation every time OnDraw.

And the trigger condition is:

            Mvalueanimator.addupdatelistener (new  Animatorupdatelistener () {                                @Override                public  void  onanimationupdate (valueanimator animation) {                    = (Float) Animation.getanimatedvalue ();                    invalidate ();//Trigger OnDraw                }            );         }

So the key to Valueobjcet is to change the properties of the object during the animation to determine how the animation will be implemented by this change.

Animation of Android (3)

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.