Android中使用animation的方法
在Android中,分别可以在xml中定義Animation,也可以在程式代碼中定義,
下面的小例子是利用RotateAnimation簡單展示一下兩種方法的用法,對于其他動畫,如ScaleAnimation,AlphaAnimation,原理是一樣的。
方法一:在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 旋轉動畫效果
屬性:interpolator 指定一個動畫的插入器,用來控制動畫的速度變化
fromDegrees 屬性為動畫起始時物件的角度
toDegrees 屬性為動畫結束時物件旋轉的角度,+代表順時針
duration 屬性為動畫持續時間,以毫秒為機關
-->
</set>
使用動畫的Java代碼,程式的效果是點選按鈕,TextView旋轉一周:
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.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動畫解析 --JavaCode
AlphaAnimation
① AlphaAnimation類對象定義
private AlphaAnimation myAnimation_Alpha;
複制代碼
② AlphaAnimation類對象構造
AlphaAnimation(float fromAlpha, float toAlpha)
//第一個參數fromAlpha為 動畫開始時候透明度
//第二個參數toAlpha為 動畫結束時候透明度
myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
//說明:
// 0.0表示完全透明
// 1.0表示完全不透明
複制代碼
③ 設定動畫持續時間
myAnimation_Alpha.setDuration(5000);
//設定時間持續時間為 5000毫秒
複制代碼
ScaleAnimation
① ScaleAnimation類對象定義
private AlphaAnimation myAnimation_Alpha;
複制代碼
② ScaleAnimation類對象構造
ScaleAnimation(float fromX, float toX, float fromY, float toY,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
//第一個參數fromX為動畫起始時 X坐标上的伸縮尺寸
//第二個參數toX為動畫結束時 X坐标上的伸縮尺寸
//第三個參數fromY為動畫起始時Y坐标上的伸縮尺寸
//第四個參數toY為動畫結束時Y坐标上的伸縮尺寸
//第五個參數pivotXType為動畫在X軸相對于物件位置類型
//第六個參數pivotXValue為動畫相對于物件的X坐标的開始位置
//第七個參數pivotXType為動畫在Y軸相對于物件位置類型
//第八個參數pivotYValue為動畫相對于物件的Y坐标的開始位置
myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
複制代碼
③ 設定動畫持續時間
myAnimation_Scale.setDuration(700);
//設定時間持續時間為 700毫秒
複制代碼
TranslateAnimation
① TranslateAnimation類對象定義
private AlphaAnimation myAnimation_Alpha;
複制代碼
② TranslateAnimation類對象構造
TranslateAnimation(float fromXDelta, float toXDelta,
float fromYDelta, float toYDelta)
//第一個參數fromXDelta為動畫起始時 X坐标上的移動位置
//第二個參數toXDelta為動畫結束時 X坐标上的移動位置
//第三個參數fromYDelta為動畫起始時Y坐标上的移動位置
//第四個參數toYDelta為動畫結束時Y坐标上的移動位置
複制代碼
③ 設定動畫持續時間
myAnimation_Translate.setDuration(2000);
//設定時間持續時間為 2000毫秒
複制代碼
RotateAnimation
① RotateAnimation類對象定義
private AlphaAnimation myAnimation_Alpha;
複制代碼
② RotateAnimation類對象構造
RotateAnimation(float fromDegrees, float toDegrees,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
//第一個參數fromDegrees為動畫起始時的旋轉角度
//第二個參數toDegrees為動畫旋轉到的角度
//第三個參數pivotXType為動畫在X軸相對于物件位置類型
//第四個參數pivotXValue為動畫相對于物件的X坐标的開始位置
//第五個參數pivotXType為動畫在Y軸相對于物件位置類型
//第六個參數pivotYValue為動畫相對于物件的Y坐标的開始位置
myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
複制代碼
③ 設定動畫持續時間
myAnimation_Rotate.setDuration(3000);
//設定時間持續時間為 3000毫秒
複制代碼
如何使用 Java 代碼中的動畫效果
使用從 View 父類繼承過來的方法 startAnimation ()來為 View 或是子類 View 等等添加一個動畫效果
public void startAnimation (Animation animation)