旋转动画效果(rotate)
第一种:
xml:
<Button android:id="@+id/btn_rotate" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btn_rotate" />
java:
private Button btn_rotate;
btn_rotate = findViewById(R.id.btn_rotate); btn_rotate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 从0开始,旋转360度 // RotateAnimation ra = new RotateAnimation(0,360); // 前两个参数是旋转的度数 后两个参数是X,Y坐标(单位为像素):即从哪个点开始旋转 // RotateAnimation ra = new RotateAnimation(0, 360, 100, 500); // 前两个参数是旋转的度数 后两个参数是X,Y坐标,Animation.RELATIVE_TO_SELF:以自身为坐标点 0.5f:中心点 RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // 设置动画时长 ra.setDuration(2000); // 启动动画 v.startAnimation(ra); } });
第二种方式:
anim.xml:
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360"> <!--fromDegrees:起始旋转度数 toDegrees:终始旋转度数 duration:动画时长--> <!--pivotX:pivotY: 写百分比是以自身为旋转点,写数值是以像素为坐标点 --> </rotate>
java:
v.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim));