Android绘制旋转动画方法详解
作者:吃橘子的季节呢 发布时间:2021-11-30 20:11:07
标签: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
0
投稿
猜你喜欢
- 前言下面大部分内容来源于网上的相关帖子和官网,自己简单写了个demo体验了下,个人感觉mybatis的缓存并不是很合适查询做缓存时,遇到更新
- 这篇文章主要介绍了java读取xml配置参数代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- 一、简述mybatis驼峰式命名规则自动转换:使用前提:数据库表设计按照规范“字段名中各单词使用下划线"_"划分”;使用
- 文件上传是网站非常常用的功能,直接使用Servlet获取上传文件还得解析请求参数,比较麻烦,所以一般选择采用apache的开源工具,comm
- SVN出现提示org.apache.subversion.javahl.ClientException: Attempted to lock
- 如题,主要使用AsReadOnly这个方法就可以了List<int> a = new List<int> {1, 2
- 前言前面文章我们介绍了Broker是如何将消息全量存储到CommitLog文件中,并异步生成dispatchRequest任务更新Consu
- 同步日志的业务流程处理和日志打印是在同一个线程,日志打印的过程实际上是写文件IO的过程,这个过程是相对耗时的,并且会阻塞主线程的执行,只有日
- 前言:我们都知道Android开发者的必备工具:AndroidStudio。是开发人员用来编译、测试的专用工具。今天在使用And
- Java中字符串中子串的查找共有四种方法,如下:1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串
- 什么是自动填充有些表中会有更新时间、创建时间、更新人或者创建人这些字段。每次对数据进行新增、删除、修改时都需要对这些字段进行设置。传统的做法
- 一、PriorityQueue的数据结构JDK7中PriorityQueue(优先级队列)的数据结构是二叉堆。准确的说是一个最小堆。二叉堆是
- 一、题目描述题目:有五个学生,每个学生有 3 门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),把这些数据存放在磁盘文件 &q
- 本文实例讲述了Java 8新增的方法参数反射。分享给大家供大家参考,具体如下:一 点睛Java 8在java.lang.reflect包下新
- 目录一、新建简单窗口二、编写窗口中的按键三、简单的按键运行1.流布局管理器:2.静态文本框:四、窗口画图五、窗口鼠标响应六、总结好了,sto
- 相对于Swing来说,JavaFX在UI上改善了很多,不仅可以通过FXML来排版布局界面,同时也可以通过CSS样式表来美化UI。其实在开发J
- 本文实例为大家分享了Unity实现文本转贴图的具体代码,供大家参考,具体内容如下导入字体导入ttf字体,修改Character为Custom
- 开发环境:android4.1.1实验功能:在第一个Hello World!为标签的activity中显示good,该界面中有一个名为Nex
- 一种可以设置滑动动画的控件,只显示一行布局,在布局文件中的ViewFlipper控件中顺序写好每一行的布局(1).MainActivity.
- idea手动刷新git分支相信很多小伙伴都遇到过这样的问题,在git上新建的分支却在idea的git分支中找不到又不知在哪里刷新,博主最近也