Android Animation in two ways, where the frame animation on the article has been said, this is the main explanation is the motion tweens, tweened animation is the animation business scene commonly used in rotation, translation, scaling, and gradient effect, frame animation is through the Carousel animation to achieve the effect, The tweened animation is implemented by adding a gradient animation between two keyframes, in contrast to the space for the tweened animation to take up a smaller amount, there are two ways to animate, one is directly in the code is implemented, the other is defined in the XML file, and then called through the Code, If you need to change the XML file at a later time, you do not need to change the code.
Layout File
The first look is the effect of the implementation:
XML settings in Layout:
<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 "Tools:context = "Com.example.googletween.MainActivity" > <linearlayout android:id= "@+id/line" android:layout_width = "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <Button android:onclick= "Alphaevent" android:layout_width= "0dip" android:layout_height= "Wrap_con Tent "android:layout_weight=" 1 "android:text=" gradient "/> <button android:onclick= "Roateevent" android:layout_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "Rotation"/> <button android:onclick= "Tranevent" AndroidOid:layout_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" a ndroid:text= "Displacement"/> <button android:onclick= "Scaleevent" Androi D:layout_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" and roid:text= "Zoom"/> <button android:onclick= "Multievent" Android Oid:layout_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "mixed"/> </LinearLayout> <imageview android:id= "@+id/image_cha Nge "android:layout_width=" wrap_content "android:layout_below=" @id/line "android:layout_height=" Wrap_content " Android:layout_centerhorizontal= "true" android:layout_centervertical= "true" android:src= "@drawable/ic_launcher"/ > </relativelayout&Gt
animation effects
Gradient transparency, when initializing a constructor two digits minimum transparency and maximum transparency:
Alphaanimation alphaanimation=new alphaanimation (0.1f, 1f); Set animation time alphaanimation.setduration (+); Number of repetitions alphaanimation.setrepeatcount (1); Alphaanimation.setrepeatmode (animation.reverse); Image.startanimation (alphaanimation);
The rotation effect, when initialized, is rotated 0 degrees to 360 degrees:
Rotateanimation rotateanimation=new rotateanimation (0f, 360f); Rotateanimation.setduration (2000); Image.startanimation (rotateanimation);
displacement effect, The first parameter Fromxdelta, the second parameter Toxdelta: The start of the animation, the end of the x-coordinate, the third parameter Fromydelta, the fourth parameter Toydelta: The beginning of the animation, the end of the y-coordinate:
Translateanimation translateanimation=new translateanimation (0f, 100f, 0f, 100f); Translateanimation.setduration (+); Image.startanimation (translateanimation);
Zoom effect
Scaleanimation scaleanimation=new scaleanimation (0.1f, 1f, 0.1f, 1f); Scaleanimation.setduration (+); Image.startanimation (scaleanimation);
Move while zooming (the last two effects blend):
Animationset animationset=new Animationset (true); Translateanimation translateanimation=new translateanimation (0f, 100f, 0f, 100f); Scaleanimation scaleanimation=new scaleanimation (0.1f, 1f, 0.1f, 1f); Animationset.addanimation (translateanimation); Animationset.addanimation (scaleanimation); Animationset.setduration (+); Image.startanimation (Animationset);
The second is defined in the XML file, setting the attribute values in the code in XML:
Gradient XML
<?xml version= "1.0" encoding= "Utf-8"? ><alpha xmlns:android= "http://schemas.android.com/apk/res/ Android " android:fromalpha=" 0.1 " android:toalpha=" 1.0 " android:duration=" " android: Repeatcount= "1" android:repeatmode= "reverse" ></alpha>
Call:
Animation alphaanimation=animationutils.loadanimation (this, r.anim.alpha); Image.startanimation (alphaanimation);
Rotate xml:
<?xml version= "1.0" encoding= "Utf-8"? ><rotate xmlns:android= "http://schemas.android.com/apk/res/ Android " android:duration=" " android:fromdegrees=" 0 " android:todegrees=" > </ Rotate>
Call:
Animation rotateanimation=animationutils.loadanimation (this, r.anim.roate); Image.startanimation (rotateanimation);
To scale XML:
<?xml version= "1.0" encoding= "Utf-8"? ><scale xmlns:android= "http://schemas.android.com/apk/res/ Android " android:duration=" " android:fromxscale=" 0 " android:toxscale=" 1 " android: fromyscale= "0" android:toyscale= "1" > </scale>
Call:
Animation scaleanimation=animationutils.loadanimation (this, r.anim.scale); Image.startanimation (scaleanimation);
Displacement xml:
<?xml version= "1.0" encoding= "Utf-8"? ><translate xmlns:android= "http://schemas.android.com/apk/res/ Android " android:duration=" " android:fromxdelta=" 0 " android:toxdelta=" android: Fromydelta= "0" android:toydelta= ">" </translate>
Call:
Animation translateanimation=animationutils.loadanimation (this, R.anim.tran); Image.startanimation (translateanimation);
Combining XML:
<?xml version= "1.0" encoding= "Utf-8"?><set> <alpha xmlns:android= "http://schemas.android.com/apk/ Res/android "android:duration=" "Android:fromalpha=" 0.1 "android:repeatcount=" 1 "android:r Epeatmode= "Reverse" android:toalpha= "1.0" > </alpha> <rotate xmlns:android= "Http://schemas . Android.com/apk/res/android "android:duration=" "android:fromdegrees=" 0 "android:todegrees=" 360 " > </rotate> <scale xmlns:android= "Http://schemas.android.com/apk/res/android" Android:dur ation= "android:fromxscale=" 0 "android:fromyscale=" 0 "android:toxscale=" 1 "Android:toysca le= "1" > </scale> <translate xmlns:android= "Http://schemas.android.com/apk/res/android" an droid:duration= "android:fromxdelta=" 0 "android:fromydelta=" 0 "android:toxdelta=" and " Roid:toydelta= ">" </translate></set>
Call:
Animation animationset=animationutils.loadanimation (this, r.anim.set); Image.startanimation (Animationset);
Android Animation-Tween (Tween) animation