Android RotateAnimation詳解

來源:互聯網
上載者:User

標籤:

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詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.