RotateAnimation旋转动画Demo

一、在java中实现动画:

package com.jikexueyuan.rotateanimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;

public class MainActivity extends Activity {
	
	
	private RotateAnimation ra;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ra = new RotateAnimation(
        		0 //fromDegrees起始角度
        		, 360 //toDegrees旋转角度
        		, Animation.RELATIVE_TO_SELF, 0.5f,//pivotXType 旋转中心的X轴
        		//RELATIVE_TO_SELF:相对自身
        		Animation.RELATIVE_TO_SELF, 0.5f//pivotXValue 旋转中心的Y轴
        		);
        ra.setDuration(1000);
        
        findViewById(R.id.btnRotateMe).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				arg0.startAnimation(ra);
			}
		});
    }
}

二、在ra.xml中实现动画:

package com.jikexueyuan.rotateanimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;

public class MainActivity extends Activity {
	
	
	private RotateAnimation ra;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        findViewById(R.id.btnRotateMe).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				arg0.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.ra));
			}
		});
    }
}

ra.xml:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:duration="1000"
    android:pivotX="50%"
    android:pivotY="50%" >

</rotate>








你可能感兴趣的:(旋转动画,RotateAnimation)