Android实现简单旋转动画
作者:Android-kongqw 发布时间:2023-11-07 09:50:58
标签:Android,旋转动画
本文实例为大家分享了Android实现简单旋转动画的具体代码,供大家参考,具体内容如下
核心方法
public void startAnimation(Animation animation)
执行动画,参数可以是各种动画的对象,Animation的多态,也可以是组合动画,后面会有。
2个参数的构造方法
/**
* Constructor to use when building a RotateAnimation from code.
* Default pivotX/pivotY point is (0,0).
*
* @param fromDegrees Rotation offset to apply at the start of the animation.
* @param toDegrees Rotation offset to apply at the end of the animation.
*/
public RotateAnimation(float fromDegrees, float toDegrees) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mPivotX = 0.0f;
mPivotY = 0.0f;
}
第一个参数是图片旋转的起始度数
第二个参数是图片旋转结束的度数
用法
RotateAnimation ta = new RotateAnimation(0, 360);
// 设置动画播放的时间
ta.setDuration(1000);
// 开始播放动画
iv.startAnimation(ta);
效果
以图片左上角为旋转中心,顺时针旋转360度
4个参数的构造方法
/**
* Constructor to use when building a RotateAnimation from code
*
* @param fromDegrees Rotation offset to apply at the start of the animation.
* @param toDegrees Rotation offset to apply at the end of the animation.
* @param pivotX The X coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the left edge.
* @param pivotY The Y coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the top edge.
*/
public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mPivotXType = ABSOLUTE;
mPivotYType = ABSOLUTE;
mPivotXValue = pivotX;
mPivotYValue = pivotY;
initializePivotPoint();
}
头两个参数和上面两个参数的构造方法一样,是开始和结束的角度
后两个参数是设置图片的旋转中心
用法
RotateAnimation ta = new RotateAnimation(0, 360, iv.getWidth() / 2, iv.getHeight() / 2);
// 设置动画播放的时间
ta.setDuration(1000);
// 开始播放动画
iv.startAnimation(ta);
效果
以图片中心为旋转中心,顺时针旋转360度
6个参数的构造方法
/**
* Constructor to use when building a RotateAnimation from code
*
* @param fromDegrees Rotation offset to apply at the start of the animation.
* @param toDegrees Rotation offset to apply at the end of the animation.
* @param pivotXType Specifies how pivotXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
* @param pivotXValue The X coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the left edge. This value can either be an absolute number if pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
* @param pivotYType Specifies how pivotYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
* @param pivotYValue The Y coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the top edge. This value can either be an absolute number if pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mPivotXValue = pivotXValue;
mPivotXType = pivotXType;
mPivotYValue = pivotYValue;
mPivotYType = pivotYType;
initializePivotPoint();
}
比4个参数的构造方法多了第三个和第五个参数,其他用法一样,第三个和四五个参数分别设置第四个和第六个参数的类型,四个参数的构造没有设置,是默认设置了Animation.ABSOLUTE类型
用法
// 创建旋转的动画对象
RotateAnimation ta = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
// 设置动画播放的时间
ta.setDuration(1000);
// 开始播放动画
iv.startAnimation(ta);
效果
和上面一样,以图片中心为旋转中心,顺时针旋转360度。
设置动画重复播放的次数的方法
/**
* Sets how many times the animation should be repeated. If the repeat
* count is 0, the animation is never repeated. If the repeat count is
* greater than 0 or {@link #INFINITE}, the repeat mode will be taken
* into account. The repeat count is 0 by default.
*
* @param repeatCount the number of times the animation should be repeated
* @attr ref android.R.styleable#Animation_repeatCount
*/
public void setRepeatCount(int repeatCount) {
if (repeatCount < 0) {
repeatCount = INFINITE;
}
mRepeatCount = repeatCount;
}
使用
sa.setRepeatCount(2);
一直重复
sa.setRepeatCount(Animation.INFINITE);
设置动画重复播放的模式的方法
/**
* Defines what this animation should do when it reaches the end. This
* setting is applied only when the repeat count is either greater than
* 0 or {@link #INFINITE}. Defaults to {@link #RESTART}.
*
* @param repeatMode {@link #RESTART} or {@link #REVERSE}
* @attr ref android.R.styleable#Animation_repeatMode
*/
public void setRepeatMode(int repeatMode) {
mRepeatMode = repeatMode;
}
使用
sa.setRepeatMode(ScaleAnimation.REVERSE);
动画的监听
rotateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
});
来源:https://blog.csdn.net/q4878802/article/details/47085187
0
投稿
猜你喜欢
- 本文实例讲述了C# WinForm控件对透明图片重叠时出现图片不透明的简单解决方法。分享给大家供大家参考,具体如下:在Winform中如果将
- 希尔排序,也称递减增量排序算法,是插入排序的一种更高效的改进版本。希尔排序是非稳定排序算法。希尔排序是基于插入排序的以下两点性质而提出改进方
- 前言Java作为一种平台无关性的语言,其主要依靠于Java虚拟机——JVM,我们写好的代码会被编译成class文件,再由JVM进行加载、解析
- 一、什么是iText?在企业的信息系统中,报表处理一直占比较重要的作用,iText是一种生成PDF报表的Java组件。通过在服务器端使用Js
- 以前使用spring的使用要注入property要配置PropertyPlaceholder的bean对象。在springboot除&nbs
- 开发过程中经常遇到需要用某些http://maven.apache.org/中没有的jar包,这个时候可以用maven命令自己添加通常这些j
- Java8 LocalDateTime与timestamp转换将timestamp转为LocalDateTimepublic LocalDa
- 一、错误处理原理分析使用SpringBoot创建的web项目中,当我们请求的页面不存在(http状态码为404),或者器发生异常(http状
- 传播行为定义关于客户端和被调用方法的事务边界。Spring定义了7种截然不同的传播行为。1,PROPAGATION_MANDATORY,表示
- 前面博客我们在讲解数组中,知道数组作为数据存储结构有一定的缺陷。在无序数组中,搜索性能差,在有序数组中,插入效率又很低,而且这两种数组的删除
- 一、SpringBoot中的main方法注入service在springboot中使用main方法常规无法注入service,因为以后也可能
- 首先来一道思考题:String str1 = "111111";String str2 = "222222&q
- 前言记一次为了节省代码没有在方法体中声明HttpServletRequest,而用autowire直接注入所钻的坑结论:给心急的人。 直接在
- 要求:用DateTimeFormatter实现: 用扫描器获取输入的时间(年月日时分),这个时间的格式是常用的格式,然后格式化这个时间,把格
- 1. 概述官方JavaDocsApi: javax.swing.JComboBoxJComboBox,下拉列表框。JComboBox以下列列
- 本文实例讲述了Java实现的简单音乐播放器功能。分享给大家供大家参考,具体如下:应用名称:Java简单的音乐播放器用到的知识:Java GU
- 1.初始化顺序当Java创建一个对象时,系统先为该对象的所有实例属性分配内存(前提是该类已经被加载过了),接着程序开始对这些实例属性执行初始
- 在搭建Spring Cloud Eureka环境前先要了解整个架构的组成,常用的基础模式如下图:服务提供者:将springboot服务编写好
- 代码实例:<%@ page language="java" contentType="text/html
- 集成引入依赖在项目的pom.xml中增加以下依赖<dependency> <groupId>org.s