How to Use Animation for Android

Source: Internet
Author: User

We will provide a detailed description of how Android uses Animation in this article. You can interpret the Code mentioned here and learn the relevant operation skills to help us develop applications in the future and deepen our understanding of this operating system.

  • Android SDK
  • Android power management related application skills
  • Introduction to how to create an Android NDK Environment
  • Basic knowledge of Android interface Layout
  • Android logcat Application Guide

In Android, you can define Animation in xml or in program code. The following small example uses RotateAnimation to briefly show the two Android methods using Animation, for other animations, such as ScaleAnimation and AlphaAnimation, the principle is the same.

Android uses Animation Method 1: Define an Animation in xml:

Xml Code

 
 
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Set xmlns: android =
    Http://schemas.android.com/apk/res/android>
  3. <Rotate
  4. Android: interpolator = "@ android: anim/accelerate _
    Decelerate_interpolator"
  5. Android: fromDegrees = "0"
  6. Android: toDegrees = "+ 360"
  7. Android: duration = "3000"/>
  8. <! -- Rotate rotation animation effect
  9. Attribute: interpolator specifies an animation plug-in to control the animation speed changes.
  10. The fromDegrees attribute is the object angle when the animation starts.
  11. The toDegrees attribute is the Rotation Angle of the object at the end of the animation. + represents clockwise.
  12. The duration attribute is the animation duration in milliseconds.
  13. -->
  14. </Set>
  15. <? Xml version = "1.0" encoding = "UTF-8"?>
  16. <Set xmlns: android =
    Http://schemas.android.com/apk/res/android>
  17. <Rotate
  18. Android: interpolator =
    "@ Android: anim/accelerate_decelerate_interpolator"
  19. Android: fromDegrees = "0"
  20. Android: toDegrees = "+ 360"
  21. Android: duration = "3000"/>
  22. <! -- Rotate rotation animation effect
  23. Attribute: interpolator specifies an animation plug-in to control the animation speed changes.
  24. The fromDegrees attribute is the object angle when the animation starts.
  25. The toDegrees attribute is the Rotation Angle of the object at the end of the animation. + represents clockwise.
  26. The duration attribute is the animation duration in milliseconds.
  27. -->
  28. </Set>

The Java code of the animation is used. The effect of the program is to click the button and the TextView is rotated for one week:

Java code

 
 
  1. package com.ray.animation;   
  2. import android.app.Activity;   
  3. import android.os.Bundle;   
  4. import android.view.View;   
  5. import android.view.View.OnClickListener;   
  6. import android.view.animation.Animation;   
  7. import android.view.animation.AnimationUtils;   
  8. import android.widget.Button;   
  9. import android.widget.TextView;   
  10. public class TestAnimation extends Activity 
    implements OnClickListener{   
  11. public void onCreate(Bundle savedInstanceState) {   
  12. super.onCreate(savedInstanceState);   
  13. setContentView(R.layout.main);   
  14. Button btn = (Button)findViewById(R.id.Button01);   
  15. btn.setOnClickListener(this);   
  16. }   
  17. @Override   
  18. public void onClick(View v) {   
  19. Animation anim = AnimationUtils.loadAnimation(this, 
    R.anim.my_rotate_action);   
  20. findViewById(R.id.TextView01).startAnimation(anim);   
  21. }   
  22. }   
  23. package com.ray.animation;  
  24. import android.app.Activity;  
  25. import android.os.Bundle;  
  26. import android.view.View;  
  27. import android.view.View.OnClickListener;  
  28. import android.view.animation.Animation;  
  29. import android.view.animation.AnimationUtils;  
  30. import android.widget.Button;  
  31. import android.widget.TextView;  
  32. public class TestAnimation extends Activity 
    implements OnClickListener{  
  33. public void onCreate(Bundle savedInstanceState) {  
  34. super.onCreate(savedInstanceState);  
  35. setContentView(R.layout.main);  
  36. Button btn = (Button)findViewById(R.id.Button01);  
  37. btn.setOnClickListener(this);   
  38. }  
  39. @Override  
  40. public void onClick(View v) {  
  41. Animation anim = AnimationUtils.loadAnimation(this, 
    R.anim.my_rotate_action);  
  42. findViewById(R.id.TextView01).startAnimation(anim);  
  43. }  

Android uses Animation Method 2: directly defining the Animation effect in the Code is similar to the method ):

Java code

 
 
  1. package com.ray.animation;   
  2. import android.app.Activity;   
  3. import android.os.Bundle;   
  4. import android.view.View;   
  5. import android.view.View.OnClickListener;   
  6. import android.view.animation.AccelerateDecelerateInterpolator;   
  7. import android.view.animation.Animation;   
  8. import android.view.animation.RotateAnimation;   
  9. import android.widget.Button;   
  10. public class TestAnimation extends Activity 
    implements OnClickListener{   
  11. public void onCreate(Bundle savedInstanceState) {   
  12. super.onCreate(savedInstanceState);   
  13. setContentView(R.layout.main);   
  14. Button btn = (Button)findViewById(R.id.Button);   
  15. btn.setOnClickListener(this);   
  16. }   
  17. public void onClick(View v) {   
  18. Animation anim = null;   
  19. anim = new RotateAnimation(0.0f,+360.0f);   
  20. anim.setInterpolator(new AccelerateDecelerateInterpolator());   
  21. anim.setDuration(3000);   
  22. findViewById(R.id.TextView01).startAnimation(anim);   
  23. }   

Android uses the relevant implementation methods of Animation.

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.