RotateAnimation旋转动画
实现有两种方式:
一、xml+java代码
在res下新建anim文件夹,在res/anim下新建rotate.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:duration="200"
android:repeatCount="-1"
android:pivotX="50%"
android:pivotY="50%" />
</set>
android:fromDegrees 起始的角度度数
android:toDegrees 结束的角度度数,负数表示逆时针,正数表示顺时针
android:pivotX 旋转中心的X坐标
android:pivotY 旋转中心的Y坐标
android:duration 完成一次动画的时间
android:interpolator 表示变化率
android:startOffset 在调用start函数之后等待开始运行的时间
android:repeatCount 重复的次数,默认为0,必须是int,可以为-1表示不停止
android:repeatMode 重复的模式:默认为restart:重头开始重新运行 reverse:从结束开始向前重新运行
activity_main.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="rotate" />
</RelativeLayout>
java代码:
[Java] 纯文本查看 复制代码 package com.imooc.helloworld.firstapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewStub;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rotate();
}
});
}
private void rotate() {
Animation rotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
mButton.startAnimation(rotate);
}
}
运行效果:
二、java代码
[Java] 纯文本查看 复制代码 package com.imooc.helloworld.firstapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewStub;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rotate();
}
});
}
private void rotate() {
//创建旋转动画
Animation animation = new RotateAnimation(0, 359);
animation.setDuration(1000);
//动画的重复次数
animation.setRepeatCount(-1);
//开始动画
mButton.startAnimation(animation);
}
}
运行效果:
|