Android实现简单旋转动画
作者:Android-kongqw 时间:2023-11-07 09:50:58
本文实例为大家分享了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
标签:Android,旋转动画
0
投稿
猜你喜欢
C#程序执行时间长查询速度慢解决方案
2023-10-31 08:49:05
Spring Cloud Zuul集成Swagger实现过程解析
2021-05-26 12:36:09
C# Xamarin利用ZXing.Net.Mobile进行扫码的方法
2021-06-16 00:46:31
基于java实现画图板功能
2022-12-09 07:04:49
Android添加用户组及自定义App权限的方法
2022-09-15 20:08:53
Android利用Sensor(传感器)实现指南针小功能
2021-12-27 14:22:36
C# 中的动态创建组件(属性及事件)的实现思路及方法
2021-07-20 04:58:31
ArrayList在for循环中使用remove方法移除元素方法介绍
2022-11-20 03:50:18
Spring interceptor拦截器配置及用法解析
2023-06-26 06:08:15
Java使用5个线程计算数组之和
2022-07-25 21:41:37
Java如何实现树的同构?
2023-11-28 09:55:19
Android 列表选择框 Spinner详解及实例
2021-11-18 14:59:08
java实现马踏棋盘的算法
2023-11-29 17:00:29
anroid开发教程之spinner下拉列表的使用示例
2023-10-05 05:42:22
通过C++程序示例理解设计模式中的外观模式
2022-02-06 19:38:15
Java命令设计模式优雅解耦命令和执行提高代码可维护性
2023-11-23 06:25:46
Android通过startService实现文件批量下载
2022-11-02 05:29:52
android实现常驻通知栏遇到的问题及解决办法
2022-10-12 06:06:58
Java ThreadPoolExecutor 线程池的使用介绍
2021-06-28 12:40:35
Mybatis参数传递示例代码
2023-05-20 09:43:08