Android实现旋转动画
作者:吃橘子的季节呢 发布时间:2022-05-15 15:26:15
标签:Android,旋转
本文实例为大家分享了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>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="150dp">
<Button
android: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" />
<Button
android: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" />
<Button
android: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" />
<Button
android: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>
<ImageView
android: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;
@Override
protected 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);
}
@Override
protected 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() {
@Override
public void onClick(View v) {
fan.startAnimation(rotate);
}
});
accelerate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (duration>10){
duration/=2;}
rotate.setDuration(duration);
fan.startAnimation(rotate);
}
});
decelerate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (duration<10000){
duration*=2;}
rotate.setDuration(duration);
fan.startAnimation(rotate);
}
});
/*
fan.setOnClickListener(new View.OnClickListener() {
@Override
public 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() {
@Override
public void onClick(View v) {
fan.clearAnimation();
}
});
}
}
需要有三个图片:
fan.png 风扇扇叶
border.png 风扇边框
title.png 贴图
(随便找一个能看就行)
来源:https://blog.csdn.net/w_Eternal/article/details/122296375


猜你喜欢
- java两个对象之间传值及封装在项目里面使用SSH框架的过程中, 经常涉及到修改,我们只要对部门的部门属性进行修改我们通常的做法是先从数据库
- 首先安装consul环境,参照之前的文章:https://www.jb51.net/article/141789.htm项目规划,2个服务端
- 本文介绍了什么是压缩纹理,以及加载压缩纹理的核心步骤。并在 Android OpenGLES 平台上实现了压缩纹理的显示。一、压缩纹理概念传
- 本文实例讲述了C# linq查询之动态OrderBy用法。分享给大家供大家参考。具体分析如下:groupList是原始数据集合,List&l
- 数据层测试事务回滚pom.xml导入对应的一些坐标,mysql,Mp,等<dependency> &
- 实现效果:注意:using system.io; 往Form1上添加控件picturebox,再添加imagelist,并设置imageli
- 本文实例为大家分享了java基于UDP实现在线聊天的具体代码,供大家参考,具体内容如下效果图:一、学习UDP的简单使用步骤接收端:Datag
- 需求是要做几个小游戏的抽奖功能,需要根据不同的游戏有不同的抽奖规则,其中也有很多共性,可归纳为只按奖品占比抽取、奖品占比与奖品数量抽取、分段
- 首先我先声明一点,本文单纯就是技术探讨,要从实际应用中来说的话,我并不建议这样去玩分布式事务、也不建议这样去玩多数据源,毕竟分布式事务主要还
- 利用apache ftp工具实现文件的上传下载和删除,具体如下1、下载相应的jar包 com
- #region 提示信息#endregion作用:折叠并隐藏代码 ,别且折叠以后能够显示白字“提示信息”如下图就是使用了#region和#e
- 效果视频简述本Demo采用Hilt+Retrofit+Paging3完成,主要为了演示paging3分页功能的使用,下列为Demo所需要的相
- 1 redis主从复制的概念多机环境下,一个redis服务接收写命令,当自身数据与状态发生变化,将其复制到一个或多个redis。这种模式称为
- XListview是一个非常受欢迎的下拉刷新控件,但是已经停止维护了。之前写过一篇XListview的使用介绍,用起来非常简单,这两天放假无
- 本文实例为大家分享了C# winform登陆框验证码的具体代码,供大家参考,具体内容如下1、 新建一个简单的 windows 应
- 我们有时候会遇到这样的情况,需要获取某些中文的拼音、中文首字母缩写和中文首字母,下面我将为大家介绍一下如何获取中文拼音的缩写。1、项目建立和
- RESTful 一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设
- 现在项目中有使用到音视频相关技术,在参考了网上各种大牛的资料及根据自己项目实际情况(兼容安卓6.0以上版本动态权限管理等),把声音录制及播放
- 什么是Flyweight模式?享元模式(Flyweight Pattern)是一种软件开发中的设计模式,其主要解决的问题是通过类对象的共享,
- 本文实例讲述了C#生成随机ArrayList的方法。分享给大家供大家参考。具体实现方法如下:public static void Rando