Android使用Animation技巧講解
Android 使用Animation的具體操作方法我們將會在這篇文章中做一個詳細(xì)的介紹。大家可以通過這里舉出的代碼進(jìn)行解讀,并從中了解到相關(guān)操作技巧,方便我們將來開發(fā)應(yīng)用,并且加深對這一操作系統(tǒng)的理解程度。#t#
在Android中,分別可以在xml中定義Animation,也可以在程序代碼中定義,下面的小例子是利用RotateAnimation簡單展示一下兩種Android使用Animation的方法,對于其他動畫,如ScaleAnimation,AlphaAnimation,原理是一樣的。
Android使用Animation方法一:在xml中定義動畫:
Xml代碼
- < ?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 旋轉(zhuǎn)動畫效果
 - 屬性:interpolator 指定一個動畫的插入器,用來控制動畫的速度變化
 - fromDegrees 屬性為動畫起始時物件的角度
 - toDegrees 屬性為動畫結(jié)束時物件旋轉(zhuǎn)的角度,+代表順時針
 - duration 屬性為動畫持續(xù)時間,以毫秒為單位
 - -->
 - < /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 旋轉(zhuǎn)動畫效果
 - 屬性:interpolator 指定一個動畫的插入器,用來控制動畫的速度變化
 - fromDegrees 屬性為動畫起始時物件的角度
 - toDegrees 屬性為動畫結(jié)束時物件旋轉(zhuǎn)的角度,+代表順時針
 - duration 屬性為動畫持續(xù)時間,以毫秒為單位
 - -->
 - < /set>
 
使用動畫的Java代碼,程序的效果是點(diǎn)擊按鈕,TextView旋轉(zhuǎn)一周:
Java代碼
- 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使用Animation方法二:直接在代碼中定義動畫(效果跟方法一類似):
Java代碼
- 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使用Animation相關(guān)實(shí)現(xiàn)方法就為大家介紹到這里。















 
 
 

 
 
 
 