您的位置:首页 > 其它

RotateAnimation旋转动画Demo

2016-01-12 15:42 274 查看
一、在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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: