Android animations-animation classification and code examples

Source: Internet
Author: User

The original has been on the animation smattering, only know in accordance with the method on the Internet will be used on the line, but I feel really a bit hard to write, today finally study the code implementation, suddenly feel more clear. Let's summarize the following, with detailed comments in the code.

Animation classification

1.Peoperty Animation

This animation is Android3.0 after the launch of the present is not very useful.

2.View Animation

This type of animation is also called Tween animation is mainly divided into gradient animation (alphaanimation) rotation animation (rotateanimation)

Scale animation (scaleanimation) Displacement animation (translateanimation)

3.Drawable Animation

This type of animation is also called frame animation frameanimation


First tween animation .

Mainactivity.java

Package Com.example.testanimation;import Android.app.activity;import Android.os.bundle;import android.view.Menu; Import Android.view.view;import Android.view.view.onclicklistener;import android.view.animation.AlphaAnimation; Import Android.view.animation.animation;import Android.view.animation.animationset;import Android.view.animation.rotateanimation;import Android.view.animation.scaleanimation;import Android.view.animation.translateanimation;import Android.widget.button;import Android.widget.ImageView;public Class Mainactivity extends Activity {private ImageView imgview; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); ImgView = ( ImageView) This.findviewbyid (R.id.imgview); Button Btnalpha = (button) This.findviewbyid (R.id.btnalpha); Btnalpha.setonclicklistener (new Animationonclicklistener (Animationtype.alpha)); Button btnrotate = (button) This.findviewbyid (r.id.btnrotate); Btnrotate.setonclicklistener (newAnimationonclicklistener (animationtype.rotate)); Button btntraslate = (button) This.findviewbyid (r.id.btntraslate); Btntraslate.setonclicklistener (new Animationonclicklistener (animationtype.traslate)); Button Btnscale = (button) This.findviewbyid (R.id.btnscale); Btnscale.setonclicklistener (new Animationonclicklistener (Animationtype.scale)); Button Btncomplex = (button) This.findviewbyid (R.id.btncomplex); Btncomplex.setonclicklistener (new Animationonclicklistener (Animationtype.complex));} Enum Animationtype {Alpha,rotate,traslate,scale,complex}class Animationonclicklistener implements OnClickListener { Private Animationtype manimationtype;public Animationonclicklistener (Animationtype animationtype) { This.manimationtype = Animationtype;} @Overridepublic void OnClick (View v) {switch (manimationtype) {case alpha:/** * Transparency is changed from opaque to 0.2 transparency */alphaanimation _alphaan Imation = new Alphaanimation (1.0f, 0.2f); _alphaanimation.setduration (+); _alphaanimation.setfillafter (true);// The status display Imgview.startanimati after the animation is executedOn (_alphaanimation); break;case rotate:/** * rotateanimation rotates 360 degrees at the midpoint of the picture * params: * Pivotxtype center point x coordinate type relative_to_ Self relative to himself, Relative_to_parent relative to the parent view * Pivotytype Ibid * * Pivotxvalue,pivotyvalue (center) * */rotateanimation _rotateanimati on = new Rotateanimation (0, Animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f); _ Rotateanimation.setduration,//_rotateanimation.setrepeatmode (Animation.reverse); ImgView.startAnimation (_ rotateanimation); break;case traslate:/** * Move */translateanimation _translateanimation = new by twice times the width of the picture Translateanimation (Animation.relative_to_self, 0f, animation.relative_to_self, 2f, animation.relative_to_self, 0f, animation.relative_to_self, 2f); _translateanimation.setduration (+); _translateanimation.setfillafter (true); Imgview.startanimation (_translateanimation); break;case scale:/** * scaleanimation with the top left corner of the picture as a stationary point, enlarge by 1.5 times times the size of the * params: * Pivotxtype Center point x coordinate type relative_to_self relative to itself, relative_to_parent relative to Parent view * Pivotytype Ibid * pivotxvalue,pivoTyvalue (stationary point) */scaleanimation _scaleanimation = new Scaleanimation (1.0f, 1.5f, 1.0f, 1.5f, animation.relative_to_parent , 0f, Animation.relative_to_self, 0f); _scaleanimation.setduration (_scaleanimation.setzadjustment); Animation.zorder_top); _scaleanimation.setrepeatcount (1); _scaleanimation.setrepeatmode (Animation.REVERSE);// You must set setrepeatcount this setting to take effect, and the animation will return Imgview.startanimation (_scaleanimation) after it is finished, in inverse mode; Break;case complex: Animationset _animationset = new Animationset (false); Alphaanimation _alphaanimation2 = new Alphaanimation (1.0f, 0.2f); _alphaanimation2.setduration (1000); _ Alphaanimation2.setrepeatcount (1); _alphaanimation2.setrepeatmode (animation.reverse);//_ Alphaanimation2.setfillafter (TRUE);//Setting this place is not so, must be set to Animationset translateanimation _translateanimation2 = new Translateanimation (Animation.relative_to_self, 0f, animation.relative_to_self, 2f, animation.relative_to_self, 0f, animation.relative_to_self, 2f); _translateanimation2.setduration (+); _translateanimation2.setrepeatcount(1); _translateanimation2.setrepeatmode (Animation.reverse);//_translateanimation2.setfillafter (true); _ Animationset.addanimation (_alphaanimation2); _animationset.addanimation (_translateanimation2); _ Animationset.setfillafter (True);//_animationset.setrepeatcount (1);//_animationset.setrepeatmode ( Animation.reverse);//These two properties are not set here and must be set imgview.startanimation (_animationset); break;default:break;}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
Activity_main.xml

<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" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=". Mainactivity "> <imageview android:id=" @+id/imgview "android:layout_width=" Wrap_content "and roid:layout_height= "Wrap_content" android:src= "@drawable/ic_launcher"/> <button android:id= "@+id/b Tnalpha "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" alpha "/> <button android:id=" @+id/btnrotate "android:layout_width=" Wrap_content "Android:layout_ Height= "Wrap_content"android:text=" rotate "/> <button android:id=" @+id/btntraslate "android:layout_width=" Wrap_ Content "android:layout_height=" wrap_content "android:text=" traslate "/> <button android:id= "@+id/btnscale" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:text = "scale"/> <button android:id= "@+id/btncomplex" android:layout_width= "Wrap_content" Android : layout_height= "wrap_content" android:text= "Animationset"/></linearlayout>

Below is frame animation

public class Frameanimationacitvity extends Activity {private ImageView imageview;private animationdrawable ad;    @Override    protected void onCreate (Bundle savedinstancestate) {    super.oncreate (savedinstancestate);    Setcontentview (r.layout.animation_list);    Button button= (button) Findviewbyid (r.id.button1);    imageview= (ImageView) Findviewbyid (r.id.imageview1);    Imageview.setbackgroundresource (r.drawable.framedrawable);    Ad= (animationdrawable) imageview.getbackground ();    Button.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {                  ad.start ();}});}    }
Framedrawable.xml

<animation-list xmlns:android= "Http://schemas.android.com/apk/res/android" >    <item android:drawable= "@drawable/grid_liushui" android:duration= "/>    <item android:drawable=" @drawable/grid_payout " android:duration= "/>    <item android:drawable=" @drawable/grid_report "android:duration=" (/>) </animation-list></span>


Attach the source download link of tween animation

http://download.csdn.net/detail/duanyu218/7449471


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.