将所需要旋转的图片导入到 drawable 中,记住文件名全小写,只能有下划线、字母,数字,编写文件布局:
rotate 为旋转,fromDegrees 代表旋转的起始角度,toDegrees为终止角度359°(总共360°),pivotX与pivotY代表图片的旋转中心,repeatCount 返回值为-1则为无限循环,duration 代表旋转时间
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.wangban.yzbbanban.myapplicationturnaround.MainActivity">
<ImageView
android:id="@+id/iv_revolve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/default_play_activity_bg1"
android:layout_centerInParent="true"
/>
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
android:layout_alignParentBottom="true"
/>
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
应用简单的相对布局创建
如图所示,然后设置 xml 文件,即旋转条件,参数
<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="359"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:duration="9000" />
</se
rotate 为旋转,fromDegrees 代表旋转的起始角度,toDegrees为终止角度359°(总共360°),pivotX与pivotY代表图片的旋转中心,repeatCount 返回值为-1则为无限循环,duration 代表旋转时间
代码如下:
package com.wangban.yzbbanban.myapplicationturnaround;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btnStart;
private Button btnStop;
private ImageView ivRevolve;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart= (Button) findViewById(R.id.btn_start);
btnStop= (Button) findViewById(R.id.btn_stop);
ivRevolve= (ImageView) findViewById(R.id.iv_revolve);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_start:
Animation animation;
animation = AnimationUtils.loadAnimation(this, R.anim.set_revolve);
LinearInterpolator i= new LinearInterpolator();
animation.setInterpolator(i);
if (animation != null) {
ivRevolve.startAnimation(animation);
}
break;
case R.id.btn_stop:
ivRevolve.clearAnimation();
break;
}
}
}
两个按钮设置监听器,声明 Animation,设置动画的格式内容,setInterpolator 匀速运动