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


猜你喜欢
- JNI中的java接口使用项目需求,需要在c++函数中监听相应的状态,并在java端进行一些列的处理。这个需要在JNI中写一个subscri
- 接口回调是指:可以把使用某一接口的类创建的对象的引用赋给该接口声明的接口变量,那么该接口变量就可以调用被类实现的接口的方法。实际上,当接口变
- 一、Android前端实现新建一个login的项目,主要的几个文件在这里1、gradle引入OKhttp3依赖implementation
- MyBatis简介MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参
- 上一小节简单分析了directArena内存分配大概流程 ,知道其先命中缓存, 如果命中不到, 则区分配一款连续内存, 这一小节带
- 前言本文主要介绍了关于spring boot中servlet启动过程与原理的相关内容,下面话不多说了,来一起看看详细的介绍吧启动过程与原理:
- 在 Android 加载图片一般使用 ImageView,这里简单记录一下这个控件的使用方法。最简单就是在 xml 里直接使用 ImageV
- Android仿通话来电界面,供大家参考,具体内容如下简介:开发中需要模拟来电时的通话界面,仿照来电界面实现来电时播放铃声,界面通过动画模拟
- 一、基本概念Task优势ThreadPool相比Thread来说具备了很多优势,但是ThreadPool却又存在一些使用上的不方便,例如:T
- 常量是固定值,程序执行期间不会改变。常量可以是任何基本数据类型,比如整数常量、浮点常量、字符常量或者字符串常量,还有枚举常量。常量可以被当作
- c#控件实现类似c++中ocx控件功能c++中ocx控件1、控件方法2、控件事件c#很容易实现c++中ocx中控件方法的功能,但是实现类似c
- private static char[] constant = &
- Java语言的历程丰富多彩,被现在众多程序员和企业广泛使用,不用质疑这是Java的领先技术的结果。Java是Sun公司开发的一种编程语言,S
- 悲观锁和乐观锁是面试高频问题之一,本文将对悲观锁和乐观锁简单的进行一个介绍。悲观锁(Pessimistic Locking)悲观锁在并发环境
- 一般我们在java中运行其它类中的方法时,无论是静态调用,还是动态调用,都是在当前的进程中执行的,也就是说,只有一个java虚拟机实例在运行
- 将通用算法放入具体类(HeapSorter),并将通用算法必须调用的方法定义在接口(HeapSorterHandle)中,从这个接口派生出D
- 在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Di
- GestureDetectorGestureDetector 是手势识别的组件,可以识别点击、双击、长按事件、拖动、缩放等手势点击事件点击相
- 何为系统APP何为三方APP?位于system分区内的是系统软件,位于data分区得的是第三方后安装的软件系统软件是指控制和协调计算机及外部
- 一、添加插件apply plugin: 'maven-publish'二、添加如下配置//打包源码task sourceJa