網站首頁 編程語言 正文
本文實例為大家分享了Android實現縮放動畫的具體代碼,供大家參考,具體內容如下
核心方法
public void startAnimation(Animation animation)
執行動畫,參數可以是各種動畫的對象,Animation的多態,也可以是組合動畫,后面會有。
4個參數構造方法
/**
?* Constructor to use when building a ScaleAnimation from code
?*?
?* @param fromX Horizontal scaling factor to apply at the start of the animation
?* @param toX Horizontal scaling factor to apply at the end of the animation
?* @param fromY Vertical scaling factor to apply at the start of the animation
?* @param toY Vertical scaling factor to apply at the end of the animation
?*/
public ScaleAnimation(float fromX, float toX, float fromY, float toY) {
? ? mResources = null;
? ? mFromX = fromX;
? ? mToX = toX;
? ? mFromY = fromY;
? ? mToY = toY;
? ? mPivotX = 0;
? ? mPivotY = 0;
}
用法
public void scale(View view) {
? ? // 創建縮放的動畫對象
? ? ScaleAnimation sa = new ScaleAnimation(0f,1.0f,0f,1.0f);
? ? // 設置動畫播放的時間
? ? sa.setDuration(1000);
? ? // 開始播放動畫
? ? iv.startAnimation(sa);
}
效果
以圖片左上角為原點,從沒有,放大到圖片原大小
6個參數構造方法
/**
? ? * Constructor to use when building a ScaleAnimation from code
? ? *?
? ? * @param fromX Horizontal scaling factor to apply at the start of the animation
? ? * @param toX Horizontal scaling factor to apply at the end of the animation
? ? * @param fromY Vertical scaling factor to apply at the start of the animation
? ? * @param toY Vertical scaling factor to apply at the end of the animation
? ? * @param pivotX The X coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the left edge. (This point remains fixed while the object changes size.)
? ? * @param pivotY The Y coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the top edge. (This point remains fixed while the object changes size.)
? ? */
? ?public ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY) {
? ? ? ?mResources = null;
? ? ? ?mFromX = fromX;
? ? ? ?mToX = toX;
? ? ? ?mFromY = fromY;
? ? ? ?mToY = toY;
? ? ? ?mPivotXType = ABSOLUTE;
? ? ? ?mPivotYType = ABSOLUTE;
? ? ? ?mPivotXValue = pivotX;
? ? ? ?mPivotYValue = pivotY;
? ? ? ?initializePivotPoint();
? ?}
前4個參數和上面的用法一樣,后兩個參數是設置圖片縮放的原點,四個參數的構造默認將這兩個參數都設置了0,所以是在圖片左上角開始縮放
用法
ScaleAnimation sa = new ScaleAnimation(0f, 1.0f, 0f, 1.0f, iv.getWidth() / 2, iv.getHeight() / 2);
// 設置動畫播放的時間
sa.setDuration(1000);
// 開始播放動畫
iv.startAnimation(sa);
效果
以圖片的中心為原點,從沒有放大到圖片原大小
8個參數構造方法
/**
? ? * Constructor to use when building a ScaleAnimation from code
? ? *?
? ? * @param fromX Horizontal scaling factor to apply at the start of the animation
? ? * @param toX Horizontal scaling factor to apply at the end of the animation
? ? * @param fromY Vertical scaling factor to apply at the start of the animation
? ? * @param toY Vertical scaling factor 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 scaled, specified as an absolute number where 0 is the left edge. (This point remains fixed while the object changes size.) 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 scaled, specified as an absolute number where 0 is the top edge. (This point remains fixed while the object changes size.) This value can either be an absolute number if pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
? ? */
? ?public ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {
? ? ? ?mResources = null;
? ? ? ?mFromX = fromX;
? ? ? ?mToX = toX;
? ? ? ?mFromY = fromY;
? ? ? ?mToY = toY;
? ? ? ?mPivotXValue = pivotXValue;
? ? ? ?mPivotXType = pivotXType;
? ? ? ?mPivotYValue = pivotYValue;
? ? ? ?mPivotYType = pivotYType;
? ? ? ?initializePivotPoint();
? ?}
用法
// 創建縮放的動畫對象
ScaleAnimation sa = new ScaleAnimation(0f, 1.0f, 0f, 1.0f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
// 設置動畫播放的時間
sa.setDuration(1000);
// 開始播放動畫
iv.startAnimation(sa);
和上面6個參數的相比只是多了第5和第7個參數,分別設置他們的類型,注釋里面已經說明了,可以設置Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT類型
效果
效果和上面一樣,以圖片的中心為原點,從沒有放大到圖片原大小。
設置動畫重復播放的次數的方法
/**
?* 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);
設置動畫重復播放的模式的方法
/**
?* 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);
原文鏈接:https://kongqw.blog.csdn.net/article/details/47084435
相關推薦
- 2022-02-13 Chrome控制臺報錯:無法加載 SourceMap 錯誤:狀態代碼 404,net::ERR_HT
- 2022-05-13 當你敲完Hello World后的第一步——C語言
- 2022-07-06 使用client?go實現自定義控制器的方法_Golang
- 2022-06-19 Go語言列表List獲取元素的4種方式_Golang
- 2022-08-15 使用mybatis-plus 的baseMapper的selectById 出現查詢不到值
- 2022-11-26 利用Python讀取Excel表內容的詳細過程_python
- 2022-11-23 Pandas?DataFrame操作數據增刪查改_python
- 2022-08-14 kvm虛擬機配置NAT端口轉發的實現方法_Kvm
- 最近更新
-
- 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同步修改后的遠程分支