標籤:
RotateAnimation旋轉座標係為以旋轉點為座標系(0,0)點。x軸為0度,順時針方向旋轉一定的角度。
1.RotateAnimation(fromDegrees, toDegrees) [預設以View左上方頂點為旋轉點]。
X軸順時針轉動到fromDegrees為旋轉的起始點,
X軸順時針轉動到toDegrees為旋轉的起始點。
如fromDegrees=0,toDegrees=90;為左上方頂點為旋轉點。0度為起始點,90度為終點。進行旋轉,旋轉了90度
如fromDegrees=60,toDegrees=90;為左上方頂點為旋轉點。60度為起始點,90度為終點。進行旋轉,旋轉了90-60=30度
2.RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
(pivotX,pivotY)為旋轉點。pivotX為距離左側的位移量,pivotY為距離頂部的位移量。即為相對於View左上方(0,0)的座標點。
假設:
View width=100px,height=100px
RotateAnimation(0,10,100,100);則以右下角頂點為旋轉點,從原始位置順時針旋轉10度
RotateAnimation(0,90,50,50);則以View的中心點為旋轉點,旋轉90度
3.RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue)
pivotXType, pivotXValue, pivotYType, pivotYValue 旋轉點類型及其值。
Animation.ABSOLUTE為絕對值 其他為百分比。這個和平移動畫的一樣,不瞭解可以去那看
假設
RotateAnimation(0, 90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 按中心點旋轉90度
效果和2中的RotateAnimation(0,90,50,50);則以View的中心點為旋轉點,旋轉90度 。效果一樣
new RotateAnimation(0, 180, centerX,centerY);
第一個參數表示動畫的起始角度,第二個參數表示動畫的結束角度,第三個表示動畫的旋轉中心x軸,第四個表示動畫旋轉中心y軸。
rotateAnimation.setDuration(1000 * 20);
表動畫持續20s。
rotateAnimation.setFillAfter(true);
ture表示動畫結束後停留在動畫的最後位置,false表示動畫結束後回到初始位置,預設為false。
mView.startAnimation(rotateAnimation);
表示在mView中啟動動畫。
列子:隨便找張圖片 兩個按鈕就行.
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軸的伸縮模式,可以取值為ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
float pivotYValue:Y座標的伸縮值。
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(); } }); } }
Android RotateAnimation詳解