This article mainly introduces the animation animation in Android.
Android offers 2 animations: Tween animations and frame animations.
This article mainly explains the tween animation, the next article will talk about frame animation.
Tween animations:
Through a series of graphical transformations of the contents of the view (including panning, zooming, rotating, changing the transparency) to achieve the effect of animation, the definition of animation can be used in XML or encoding to do tween animation (the article will give two ways to animate the source code demo).
Types of animations |
XML defines the configuration node used by the animation |
Encoding defines the classes used by animations |
Gradient Transparency animation effect (for short, transparent animation) |
<alpha/> |
Alphaanimation |
Gradient size scale animation effect (zoom animation) |
<scale/> |
Scaleanimation |
Picture Position move animation effect (shift animation) |
<translate/> |
Translateanimation |
Picture Rotation animation effect (rotate animation) |
<rotate/> |
Rotateanimation |
Realize:
Source:
:
Layout file Activity_main:
<linearlayout 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 "Android:orien tation= "vertical" > <textview android:id= "@+id/textview" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_gravity= "Center_horizontal" android:layout_margintop= "1 5DP "android:text=" uses an XML file to implement the animation "Android:textcolor=" @android: Color/holo_orange_dark "android:textsize = "20DP"/> <!--use each Button to animate it-<button android:id= "@+id/button_alphaanim" Android: Layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "Transparent animation alphaanimation"/> <button android:id= "@+id/button_rotateanim" android:layout_width= "Match_parent" Android:layout_hei ght= "wrap_content" android:text= "Rotate animation rotAteanimation "/> <button android:id=" @+id/button_translateanim "android:layout_width=" Match_parent " android:layout_height= "Wrap_content" android:text= "Shift animation Translateanimation"/> <button Andro Id:id= "@+id/button_scaleanim" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "Scaling animation scaleanimation"/> <button android:id= "@+id/button_setanim1" Android:layout_wid Th= "Match_parent" android:layout_height= "wrap_content" android:text= "Transparent animation + Shift animation"/> <button A Ndroid:id= "@+id/button_setanim2" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "Rotate animation + zoom Animation"/></linearlayout>
Animation layout under the Anim folder:
The corresponding animation properties are not detailed, the reader can self-study, and strive to achieve the purpose of modifying the animation effect.
Transparent animation Aa.xml file:
<?xml version= "1.0" encoding= "Utf-8"?><!--transparent animation XML file--><alpha xmlns:android= "/http Schemas.android.com/apk/res/android " android:duration=" " android:fromalpha=" 0 " Android:toalpha = "1" ></alpha>
Rotate animation Ra.xml:
<?xml version= "1.0" encoding= "utf-8"?><!--rotate Animation xml file--><rotate xmlns:android= "/http Schemas.android.com/apk/res/android " android:duration=" " android:fromdegrees=" 0 " android: pivotx= "50%" android:pivoty= "50%" android:todegrees= "></rotate>"
Zoom Animation Sa.xml:
<?xml version= "1.0" encoding= "utf-8"?><!--scale animation XML file--><scale xmlns:android= "/http/ Schemas.android.com/apk/res/android " android:duration=" " android:fromxscale=" 0 " android: fromyscale= "0" android:pivotx= "50%" android:pivoty= "50%" android:toxscale= "1" Android:toyscale = "1" ></scale>
Shift Animation Ta.xml:
<?xml version= "1.0" encoding= "Utf-8"?><!--shift animation XML file--><translate xmlns:android= "/http Schemas.android.com/apk/res/android " android:fromxdelta=" 100% " android:fromydelta=" 100% " android: Toxdelta= "0%" android:toydelta= "0%" android:duration= "></translate>"
Transparent animation + Shift animation XML file, Set1.xml:
<?xml version= "1.0" encoding= "Utf-8"?><!--transparent animation + Shift animation XML file--><set xmlns:android= "/http Schemas.android.com/apk/res/android " android:duration=" " android:shareinterpolator=" true "> <alpha android:fromalpha= "0" android:toalpha= "1"/> <translate android:fromxdelta= "100 % " android:fromydelta=" 100% " android:toxdelta=" 0% "Android:toydelta=" 0% " /></set>
Rotate animation + Scale The animated XML file, Set2.xml:
<?xml version= "1.0" encoding= "utf-8"?><!--rotate Animation + scale animated XML file--><set xmlns:android= "/http Schemas.android.com/apk/res/android " android:duration=" " android:shareinterpolator=" true "> <rotate android:fromdegrees= "0" android:pivotx= "50%" android:pivoty= "50%" android:todegrees = "/>" <scale android:fromxscale= "0" android:fromyscale= "0" android:pivotx= "50%" android:pivoty= "50%" android:toxscale= "1" android:toyscale= "1"/></set>
Mainactivity Code:
Package Com.myanimationdemo2;import Android.app.activity;import Android.os.bundle;import android.view.View;import Android.view.view.onclicklistener;import Android.view.animation.animationutils;import Android.widget.Button; Import Com.myanimationdemo2.r.anim;public class Mainactivity extends Activity implements Onclicklistener {private Button Button_alphaanim, Button_rotateanim, Button_translateanim,button_scaleanim, BUTTON_SETANIM1, button_setAnim2 ; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main) Button_alphaanim = (Button) Findviewbyid (R.id.button_alphaanim); Button_rotateanim = (Button ) Findviewbyid (r.id.button_rotateanim); Button_translateanim = (Button) Findviewbyid (R.id.button_translateanim); Button_scaleanim = (Button) Findviewbyid (R.id.button_scaleanim); button_setanim1 = (Button) Findviewbyid (R.id.button_ SETANIM1) button_setanim2 = (Button) Findviewbyid (R.ID.BUTTON_SETANIM2); button_alphaanim.setoncLicklistener (This), Button_rotateanim.setonclicklistener (This), Button_translateanim.setonclicklistener (this); Button_scaleanim.setonclicklistener (This), Button_setanim1.setonclicklistener (this); Button_ Setanim2.setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {/** * transparent animation */case r.id.button_alphaanim:v.startanimation ( Animationutils.loadanimation (Getapplicationcontext (), ANIM.AA)); break;/** * Rotate animation */case R.id.button_rotateanim: V.startanimation (Animationutils.loadanimation (Getapplicationcontext (), Anim.ra)) break;/** * Shift Animation */case R.id.button_translateanim:v.startanimation (Animationutils.loadanimation (Getapplicationcontext (), anim.ta)); break;/** * Zoom animation */case r.id.button_scaleanim:v.startanimation (Animationutils.loadanimation (GetApplicationContext ( , anim.sa)); break;/** * Transparent animation + Shift animation */case r.id.button_setanim1:v.startanimation (Animationutils.loadanimation ( Getapplicationcontext (), Anim.set1)); break;/** * Rotate animation + zoom animation */case r.id.button_setanim2:v.startanimation (anImationutils.loadanimation (Getapplicationcontext (), Anim.set2)); break;default:break;}}}
The above is tween animation, in the form of XML animation demo code, directly in the code to implement the animation demo is not detailed introduction.
Here is a demo of the source code for both:
Animated demo source code in XML format:
Click to download the source code
The demo source code written directly in the code:
Click to download the source code