本文实例为大家分享了Android实现旋转动画的具体代码,供大家参考,具体内容如下
旋转动画(可加速、减速)
1、准备工作
首先需要有一个用于旋转的图片
需要考虑如何开始、结束、加速、减速
2、加速减速原理
本次的动画采用RotateAnimation,初始化需要的参数如下public RotateAnimation(float fromDegrees,float toDegrees,int pivotXType,float pivotXValue,int pivotYType, float pivotYValue) {
mFromDegrees = fromDegrees;//开始角度
mToDegrees = toDegrees;//结束角度
mPivotXValue = pivotXValue;//确定x轴坐标的类型
mPivotXType = pivotXType;//x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
mPivotYValue = pivotYValue;//确定y轴坐标的类型
mPivotYType = pivotYType;//y轴的值,0.5f表明是以自身这个控件的一半长度为y轴
initializePivotPoint();
}
所谓旋转动画,在本质上就是在如上的对象初始化之后,规定在一定的周期内旋转
所谓加速,本质上就是在设定好的周期内变换旋转角度
或者修改周期,在预设周期内旋转一定角度
总之,角度和周期一定会变化一个,就可以决定动画的快慢。
如: 从 2秒内旋转360度 到 1秒内旋转360度 就是一种加速,从 2秒内旋转360度 到 2秒内旋转720度 也是一种加速。
反之就是减速。
3、初始化RotateAnimation rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setInterpolator(new LinearInterpolator());
rotate.setDuration(2000);//设置动画持续周期
rotate.setRepeatCount(-1);//设置重复次数
// rotate.setFillAfter(true);//动画执行完后是否停留在执行完的状态
rotate.setStartOffset(10);//执行前的等待时间
4、开始start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fan.startAnimation(rotate);
}
});
5、加速
首先需要创建全局变量private int duration=2000;
加速样例
`accelerate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {if (duration>10){duration/=2; //周期除2角度不变加速(需要考虑极端,所以加一个判断)}rotate.setDuration(duration); //设置周期fan.startAnimation(rotate); //开始旋转}});`
6、减速
`decelerate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {if (duration<10000){duration*=2; //周期乘2角度不变减速(需要考虑极端,所以加一个判断)}rotate.setDuration(duration); //设置周期fan.startAnimation(rotate); //开始旋转}
});7、停止stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fan.clearAnimation(); //停止
}
});8、项目源码
Layout部分<?xml version=”1.0” encoding=”utf-8”?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DEECFA"
tools:context=".MainActivity">
<RelativeLayout
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:id="@+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/border"
tools:layout_editor_absoluteX="566dp"
tools:layout_editor_absoluteY="132dp">
<ImageView
android:id="@+id/fan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/fan"
tools:layout_editor_absoluteX="552dp"
tools:layout_editor_absoluteY="122dp" />
</RelativeLayout>
<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="150dp"><Buttonandroid:id="@+id/start"android:layout_width="60dp"android:layout_height="60dp"android:background="@mipmap/border"android:text="开始"tools:layout_editor_absoluteX="525dp"tools:layout_editor_absoluteY="596dp" /><Buttonandroid:id="@+id/accelerate"android:layout_marginLeft="100dp"android:layout_width="60dp"android:layout_height="60dp"android:background="@mipmap/border"android:text="加速"tools:layout_editor_absoluteX="650dp"tools:layout_editor_absoluteY="596dp" /><Buttonandroid:layout_marginLeft="100dp"android:id="@+id/decelerate"android:layout_width="60dp"android:layout_height="60dp"android:background="@mipmap/border"android:text="减速"tools:layout_editor_absoluteX="795dp"tools:layout_editor_absoluteY="596dp" /><Buttonandroid:id="@+id/stop"android:layout_marginLeft="100dp"android:layout_width="60dp"android:layout_height="60dp"android:background="@mipmap/border"android:text="结束"tools:layout_editor_absoluteX="950dp"tools:layout_editor_absoluteY="596dp" /></LinearLayout><ImageViewandroid:layout_centerVertical="true"android:layout_marginLeft="90dp"android:id="@+id/imageView"android:layout_width="261dp"android:layout_height="527dp"app:srcCompat="@mipmap/title"tools:layout_editor_absoluteX="141dp"tools:layout_editor_absoluteY="132dp" />
</RelativeLayout>MainActivity部分package com.suk.rotate;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RotateDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.suk.rotate.R;
public class MainActivity extends AppCompatActivity {
private ImageView fan;private Button start;private Button stop;private Button accelerate;private RotateAnimation rotate;private Button decelerate;private int duration=2000;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);fan = findViewById(R.id.fan);start = findViewById(R.id.start);stop = findViewById(R.id.stop);accelerate = findViewById(R.id.accelerate);decelerate = findViewById(R.id.decelerate);}@Overrideprotected void onStart() {super.onStart();rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);rotate.setInterpolator(new LinearInterpolator());
// rotate.setInterpolator(lin);
rotate.setDuration(2000);//设置动画持续周期
rotate.setRepeatCount(-1);//设置重复次数
// rotate.setFillAfter(true);//动画执行完后是否停留在执行完的状态
rotate.setStartOffset(10);//执行前的等待时间
start.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {fan.startAnimation(rotate);}});accelerate.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (duration>10){duration/=2;}rotate.setDuration(duration);fan.startAnimation(rotate);}});decelerate.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (duration<10000){duration*=2;}rotate.setDuration(duration);fan.startAnimation(rotate);}});/*fan.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);dialog.setTitle("点我没用");dialog.setIcon(R.drawable.fan);dialog.setPositiveButton("OK",null);dialog.setMessage("这是普通对话框");View view=View.inflate(MainActivity.this,R.layout. activity_main1, null);dialog.setView(view);dialog.create();dialog.show();}});*/stop.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {fan.clearAnimation();}});}
}`
需要有三个图片:
fan.png 风扇扇叶
border.png 风扇边框
title.png 贴图
(随便找一个能看就行)
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号