Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 動畫之 RotateAnimation 應用詳解

編輯:關於 Android 程式設計

Android 中提供了四種基本動畫:

  • AlphaAnimation:透明度動畫效果
  • ScaleAnimation:縮放動畫效果
  • TranslateAnimation:位移動畫效果
  • RotateAnimation:旋轉動畫效果

本文聚焦於 RotateAnimation(旋轉動畫)。

構造函數與參數說明

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
  • float fromDegrees:旋轉的開始角度。
  • float toDegrees:旋轉的結束角度。
  • int pivotXType:X 軸的伸縮模式,可取 ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
  • float pivotXValue:X 坐標的伸縮值。
  • int pivotYType:Y 軸的伸縮模式,取值同上。
  • float pivotYValue:Y 坐標的伸縮值。

程式碼範例

以下範例讓 ImageView 繞自身中心旋轉一圈(0 度到 360 度):

public class MainActivity extends Activity {
ImageView image;
Button start;
Button cancel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.main_img);
start = (Button) findViewById(R.id.main_start);
cancel = (Button) findViewById(R.id.main_cancel);
/** 設置旋轉動畫 */
final RotateAnimation animation =new RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF,
0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(3000);// 設置動畫持續時間
/** 常用方法 */
//animation.setRepeatCount(int repeatCount);// 設置重複次數
//animation.setFillAfter(boolean);// 動畫執行完後是否停留在執行完的狀態
//animation.setStartOffset(long startOffset);// 執行前的等待時間
start.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
image.setAnimation(animation);
/** 開始動畫 */
animation.startNow();
}
});
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
/** 結束動畫 */
animation.cancel();
}
});
}
}

效果圖:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved