這裡來做圖片原地旋轉的動化效果,按下"go"按鈕,圓形的轉盤就會原地旋轉,並且停在結束的角度,用到 Animation  RotateAnimation 的旋轉動畫功能。

程式效果

RotateAnimation_Sample  

 

程式碼:

package com.example.turntable;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.Animation;

import android.view.animation.RotateAnimation;

import android.widget.Button;

import android.widget.ImageView;

 

public class MainActivity extends Activity {

   ImageView imageview01;

   Button btngo01;

  

   @Override

   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

     

      btngo01=(Button) findViewById(R.id.button1); //取得畫面上的Button

      btngo01.setOnClickListener(btngoOnClick); //Button的onclick事件

  }

 

  //Button的onclick事件 

   private OnClickListener btngoOnClick = new OnClickListener() {

       publicvoid onClick(View v) {

              imageview01=(ImageView) findViewById(R.id.imageView1); //取得畫面上的圖片

              int stopturnnum=(int)(Math.random() *3600); //亂數取旋轉角度

 

              //RotateAnimation(開始角度,結束角度,X軸的伸縮模式,X座標的伸縮值,Y軸的伸縮模式,Y座標的伸縮值)

              Animation am = new RotateAnimation(0, stopturnnum, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

 

              // 動畫開始到結束的執行時間 (1000 = 1 )

         am.setDuration( stopturnnum );

 

              // 動畫重複次數 (Animation.INFINITE表示一直重複),這裡只執行1次

             am.setRepeatCount(1); 

 

              //旋轉停止圖片都會回到原點,將setFillAfter設成true,會將圖片停在旋轉停止的角度.

              am.setFillAfter(true);

 

              // 圖片配置動畫

              imageview01.setAnimation(am);

 

              // 動畫開始

        am.startNow(); 

       }

  };

 

   @Override

   public boolean onCreateOptionsMenu(Menu menu) {

      // Inflate the menu; this adds items to the action bar if it is present.

      getMenuInflater().inflate(R.menu.main, menu);

      return true;

  }

}

 

arrow
arrow
    創作者介紹
    創作者 jcgogo 的頭像
    jcgogo

    jcgogo

    jcgogo 發表在 痞客邦 留言(0) 人氣()