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
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Set xmlns: android =
Http://schemas.android.com/apk/res/android>
- <Rotate
- Android: interpolator = "@ android: anim/accelerate _
Decelerate_interpolator"
- Android: fromDegrees = "0"
- Android: toDegrees = "+ 360"
- Android: duration = "3000"/>
- <! -- Rotate rotation animation effect
- Attribute: interpolator specifies an animation plug-in to control the animation speed changes.
- The fromDegrees attribute is the object angle when the animation starts.
- The toDegrees attribute is the Rotation Angle of the object at the end of the animation. + represents clockwise.
- The duration attribute is the animation duration in milliseconds.
- -->
- </Set>
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Set xmlns: android =
Http://schemas.android.com/apk/res/android>
- <Rotate
- Android: interpolator =
"@ Android: anim/accelerate_decelerate_interpolator"
- Android: fromDegrees = "0"
- Android: toDegrees = "+ 360"
- Android: duration = "3000"/>
- <! -- Rotate rotation animation effect
- Attribute: interpolator specifies an animation plug-in to control the animation speed changes.
- The fromDegrees attribute is the object angle when the animation starts.
- The toDegrees attribute is the Rotation Angle of the object at the end of the animation. + represents clockwise.
- The duration attribute is the animation duration in milliseconds.
- -->
- </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
- package com.ray.animation;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.Button;
- import android.widget.TextView;
- public class TestAnimation extends Activity
implements OnClickListener{
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button btn = (Button)findViewById(R.id.Button01);
- btn.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- Animation anim = AnimationUtils.loadAnimation(this,
R.anim.my_rotate_action);
- findViewById(R.id.TextView01).startAnimation(anim);
- }
- }
- package com.ray.animation;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.Button;
- import android.widget.TextView;
- public class TestAnimation extends Activity
implements OnClickListener{
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button btn = (Button)findViewById(R.id.Button01);
- btn.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- Animation anim = AnimationUtils.loadAnimation(this,
R.anim.my_rotate_action);
- findViewById(R.id.TextView01).startAnimation(anim);
- }
- }
Android uses Animation Method 2: directly defining the Animation effect in the Code is similar to the method ):
Java code
- package com.ray.animation;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.animation.AccelerateDecelerateInterpolator;
- import android.view.animation.Animation;
- import android.view.animation.RotateAnimation;
- import android.widget.Button;
- public class TestAnimation extends Activity
implements OnClickListener{
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button btn = (Button)findViewById(R.id.Button);
- btn.setOnClickListener(this);
- }
- public void onClick(View v) {
- Animation anim = null;
- anim = new RotateAnimation(0.0f,+360.0f);
- anim.setInterpolator(new AccelerateDecelerateInterpolator());
- anim.setDuration(3000);
- findViewById(R.id.TextView01).startAnimation(anim);
- }
- }
Android uses the relevant implementation methods of Animation.