網站首頁 編程語言 正文
本文實例為大家分享了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
相關推薦
- 2022-06-14 ASP.NET?Core?MVC中的標簽助手(TagHelper)用法_實用技巧
- 2022-08-23 C++?primer超詳細講解關聯容器_C 語言
- 2022-10-11 利用React實現虛擬列表的示例代碼_React
- 2022-03-27 C++浮點數在內存中的存儲詳解_C 語言
- 2023-01-23 redis實現多級緩存同步方案詳解_Redis
- 2022-04-21 C#?TrackBar拖動條改變滑塊顏色_C#教程
- 2023-02-27 python定時任務schedule庫用法詳細講解_python
- 2022-09-20 Go?Redis客戶端使用的兩種對比_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支