Android Animation Basics

Source: Internet
Author: User
Tags gety

There are three main types of Android animations:

1> view animations, also known as tween (tween) animations can perform a series of simple transformations (position, size, rotation, transparency) within a view container. For example, if you have a TextView object, you can move, rotate, scale, and transparency to set its text, and of course, if it has a background image, the background image will change with the text.

tweened animations are defined by XML or Android code and are recommended for use with XML file definitions because they are more readable and reusable.

2> drawable animation is actually frame animation (frame animation), which allows you to achieve the same effect as a slideshow, the essence of this animation is actually drawable, so the animation of the XML definition of the file is generally placed in the res/drawable/directory.

3> Android 3.0 After the introduction of property animation, property animation can easily realize many of the things that the view animation does not do, see, the view animation is nothing but do that kind of things, other also can not make, and property animation, such as 3D rotate a picture. In fact, you can remember a little bit, the principle of property animation is to modify the control's property value implementation of the animation.

It is particularly important to note that motion tweens and frame animations do not change the layout. For example, when the button moves, in the final position is not the corresponding click event, you need to click on the button in the layout of the position to take effect.

Basic usage of Property animations reference: http://blog.csdn.net/guolin_blog/article/details/43536355

A circle is moved from the upper-left corner of the screen to the lower-right corner through property animation.

1> defines the point class:

 Public classPoint { Public floatx;  Public floaty;  PublicPoint (floatXfloaty) {         This. x =x;  This. y =y; }     Public floatGetX () {returnx; }     Public floatGetY () {returny; }}

2> defining the Core class implementing the Typeevaluator interface and overriding the Evaluate () method

 Public classPointevalutorImplementsTypeevaluator {@Override PublicObject Evaluate (floatFraction, Object Startvalue, Object Endvalue) {Point StartPoint=(point) startvalue; Point EndPoint=(point) endvalue; floatx = startpoint.getx () + fraction * (Endpoint.getx ()-startpoint.getx ()); floaty = startpoint.gety () + fraction * (endpoint.gety ()-endpoint.gety ()); Point Point=NewPoint (x, y); returnPoint ; }}

3> Custom View

 Public classAnimationviewextendsView {Private Static Final floatRADIUS =50f; PrivatePoint currentpoint; PrivatePaint Mpaint;  PublicAnimationview (Context context, AttributeSet attrs) {Super(context, attrs); Mpaint=NewPaint (Paint.anti_alias_flag);    Mpaint.setcolor (color.red); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); if(Currentpoint = =NULL) {Currentpoint=NewPoint (Radius,radius);            Drawcricle (canvas);        Startanimation (); }Else{drawcricle (canvas); }    }     Public voiddrawcricle (canvas canvas) {floatx =Currentpoint.getx (); floaty =currentpoint.gety ();    Canvas.drawcircle (X,y,radius,mpaint); }     Public voidstartanimation () {Point StartPoint=NewPoint (Radius,radius); Point EndPoint=NewPoint (GetWidth ()-RADIUS, GetHeight ()-RADIUS); Valueanimator Animator=NewValueanimator (). Ofobject (Newpointevalutor (), startpoint,endpoint); Animator.addupdatelistener (NewValueanimator.animatorupdatelistener () {@Override Public voidonanimationupdate (valueanimator animation) {Currentpoint=(point) animation.getanimatedvalue ();            Invalidate ();        }        }); Animator.setduration (5000);    Animator.start (); }}

4> XML file

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    xmlns:tools=" Http://schemas.android.com/tools "    android:id=" @+id/activity_ Main "    android:layout_width=" Match_parent "    android:layout_height=" Match_parent "     tools:context= "Com.example.valuesanimation.MainActivity" >    <  Com.example.valuesanimation.AnimationView        android:layout_width= "Match_parent"        android: Layout_height= "Match_parent"/></relativelayout>

5> mainactivity

 Public class extends appcompatactivity {    @Override    protectedvoid  onCreate (Bundle Savedinstancestate) {        super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    }}

OK, get it done.

Reference:

Motion Tweens and Frame animations: http://blog.csdn.net/yanbober/article/details/46481171

Advanced usage of property animations: http://blog.csdn.net/guolin_blog/article/details/43816093

Android Animation Basics

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.