網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡單旋轉(zhuǎn)動畫的具體代碼,供大家參考,具體內(nèi)容如下
核心方法
public void startAnimation(Animation animation)
執(zhí)行動畫,參數(shù)可以是各種動畫的對象,Animation的多態(tài),也可以是組合動畫,后面會有。
2個參數(shù)的構(gòu)造方法
/**
?* 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;
}
- 第一個參數(shù)是圖片旋轉(zhuǎn)的起始度數(shù)
- 第二個參數(shù)是圖片旋轉(zhuǎn)結(jié)束的度數(shù)
用法
RotateAnimation ta = new RotateAnimation(0, 360);
// 設(shè)置動畫播放的時間
ta.setDuration(1000);
// 開始播放動畫
iv.startAnimation(ta);
效果
以圖片左上角為旋轉(zhuǎn)中心,順時針旋轉(zhuǎn)360度
4個參數(shù)的構(gòu)造方法
/**
? ? ?* 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();
? ? }
- 頭兩個參數(shù)和上面兩個參數(shù)的構(gòu)造方法一樣,是開始和結(jié)束的角度
- 后兩個參數(shù)是設(shè)置圖片的旋轉(zhuǎn)中心
用法
RotateAnimation ta = new RotateAnimation(0, 360, iv.getWidth() / 2, iv.getHeight() / 2);
// 設(shè)置動畫播放的時間
ta.setDuration(1000);
// 開始播放動畫
iv.startAnimation(ta);
效果
以圖片中心為旋轉(zhuǎn)中心,順時針旋轉(zhuǎn)360度
6個參數(shù)的構(gòu)造方法
/**
* 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個參數(shù)的構(gòu)造方法多了第三個和第五個參數(shù),其他用法一樣,第三個和四五個參數(shù)分別設(shè)置第四個和第六個參數(shù)的類型,四個參數(shù)的構(gòu)造沒有設(shè)置,是默認(rèn)設(shè)置了Animation.ABSOLUTE類型
用法
// 創(chuàng)建旋轉(zhuǎn)的動畫對象
RotateAnimation ta = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
// 設(shè)置動畫播放的時間
ta.setDuration(1000);
// 開始播放動畫
iv.startAnimation(ta);
效果
和上面一樣,以圖片中心為旋轉(zhuǎn)中心,順時針旋轉(zhuǎn)360度。
設(shè)置動畫重復(fù)播放的次數(shù)的方法
/**
?* 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);
一直重復(fù)
sa.setRepeatCount(Animation.INFINITE);
設(shè)置動畫重復(fù)播放的模式的方法
/**
?* 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);
動畫的監(jiān)聽
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
相關(guān)推薦
- 2022-05-02 詳解在Python中使用OpenCV進(jìn)行直線檢測_python
- 2023-10-25 解決使用window.open()或window.location.href跳轉(zhuǎn)后返回/后退原頁面不
- 2022-08-25 C#中的高效IO庫System.IO.Pipelines_C#教程
- 2022-06-28 ES6基礎(chǔ)語法之?dāng)?shù)組拓展_基礎(chǔ)知識
- 2024-03-14 Linux Nginx自定義安裝目錄
- 2022-11-20 解析rust中的struct_Rust語言
- 2022-04-19 IDEA 快捷生成注釋 配置(完美不報警告)
- 2022-11-23 TypeScript前端上傳文件到MinIO示例詳解_其它
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支