天天看點

android下Rotate旋轉動畫實作效果

旋轉動畫在我開發以來并未過多用到,但這個效果确實給人以很炫的感覺,旋轉嘛,一直不停的轉,比起其它動畫效果可能就美觀一些。另外在這次的動畫中也寫到了幾個動畫的綜合使用。接下來讓我們從代碼中學習吧。

首先在src檔案中定義一個anim檔案夾,在anim檔案夾下再定義一個動畫的xml檔案。如下:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="2000"
        android:fromAlpha="1"
        android:toAlpha="0.5" />

    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="1800" />

    <scale
        android:duration="2000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0"
        android:toYScale="0" />

    <translate
        android:duration="2000"
        android:fromXDelta="0%p"
        android:fromYDelta="0%p"
        android:toXDelta="45%p"
        android:toYDelta="-45%p" />

</set>
           

在這裡我們最外層包圍了一個set,所謂set就是無序集合的意思,也就是說四種動畫可以同時執行。我們在set裡面分别對每個動畫加以設定,這樣就可以實作我們想要的動畫聯合效果。

接下來再說說avtivity檔案裡的代碼,有注釋,不懂的可以多看看。如下:

public class RotateAnimationDemoActivity extends Activity implements OnClickListener {
    private ImageView mImageView;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        findViewById(R.id.btn_rotate).setOnClickListener(this);
        mImageView = (ImageView) findViewById(R.id.imageview);
    }

	public void onClick(View v) {
		//startRotateAnimationJavaCode();
		startRotateAnimationXml();
	}

	private void startRotateAnimationXml() {
		//四種動畫疊加效果
		AnimationSet setAnim = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
		mImageView.startAnimation(setAnim);
	}

	private void startRotateAnimationJavaCode() {
		//旋轉動畫  參數分别是  起始的角度  轉多少度  相對于自身的多少旋轉 
		RotateAnimation retateAnim = new RotateAnimation(
				0.0f, 720f, 
				Animation.RELATIVE_TO_SELF, 0.5f, 
				Animation.RELATIVE_TO_SELF, 0.5f);
		retateAnim.setDuration(1000);
		mImageView.startAnimation(retateAnim);
	}
}
           

這段代碼裡面有兩個方法//startRotateAnimationJavaCode();

startRotateAnimationXml();  有興趣的朋友可以通過反注釋來看到單純的旋轉效果。這裡我就不細講了。想要見證奇迹,那就運作去看吧。

以上有什麼不準的地方或者寫的不到位的地方還望廣大博友多多提起,本人以後會多加注意。以下附圖。

android下Rotate旋轉動畫實作效果

                         完畢!