網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文實(shí)例為大家分享了Android實(shí)現(xiàn)縮放動(dòng)畫(huà)的具體代碼,供大家參考,具體內(nèi)容如下
核心方法
public void startAnimation(Animation animation)
執(zhí)行動(dòng)畫(huà),參數(shù)可以是各種動(dòng)畫(huà)的對(duì)象,Animation的多態(tài),也可以是組合動(dòng)畫(huà),后面會(huì)有。
4個(gè)參數(shù)構(gòu)造方法
/**
?* 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) {
? ? // 創(chuàng)建縮放的動(dòng)畫(huà)對(duì)象
? ? ScaleAnimation sa = new ScaleAnimation(0f,1.0f,0f,1.0f);
? ? // 設(shè)置動(dòng)畫(huà)播放的時(shí)間
? ? sa.setDuration(1000);
? ? // 開(kāi)始播放動(dòng)畫(huà)
? ? iv.startAnimation(sa);
}
效果
以圖片左上角為原點(diǎn),從沒(méi)有,放大到圖片原大小
6個(gè)參數(shù)構(gòu)造方法
/**
? ? * 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個(gè)參數(shù)和上面的用法一樣,后兩個(gè)參數(shù)是設(shè)置圖片縮放的原點(diǎn),四個(gè)參數(shù)的構(gòu)造默認(rèn)將這兩個(gè)參數(shù)都設(shè)置了0,所以是在圖片左上角開(kāi)始縮放
用法
ScaleAnimation sa = new ScaleAnimation(0f, 1.0f, 0f, 1.0f, iv.getWidth() / 2, iv.getHeight() / 2);
// 設(shè)置動(dòng)畫(huà)播放的時(shí)間
sa.setDuration(1000);
// 開(kāi)始播放動(dòng)畫(huà)
iv.startAnimation(sa);
效果
以圖片的中心為原點(diǎn),從沒(méi)有放大到圖片原大小
8個(gè)參數(shù)構(gòu)造方法
/**
? ? * 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();
? ?}
用法
// 創(chuàng)建縮放的動(dòng)畫(huà)對(duì)象
ScaleAnimation sa = new ScaleAnimation(0f, 1.0f, 0f, 1.0f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
// 設(shè)置動(dòng)畫(huà)播放的時(shí)間
sa.setDuration(1000);
// 開(kāi)始播放動(dòng)畫(huà)
iv.startAnimation(sa);
和上面6個(gè)參數(shù)的相比只是多了第5和第7個(gè)參數(shù),分別設(shè)置他們的類(lèi)型,注釋里面已經(jīng)說(shuō)明了,可以設(shè)置Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT類(lèi)型
效果
效果和上面一樣,以圖片的中心為原點(diǎn),從沒(méi)有放大到圖片原大小。
設(shè)置動(dòng)畫(huà)重復(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);
設(shè)置動(dòng)畫(huà)重復(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);
原文鏈接:https://kongqw.blog.csdn.net/article/details/47084435
相關(guān)推薦
- 2022-11-06 Nginx如何配置多個(gè)服務(wù)域名解析共用80端口詳解_nginx
- 2022-09-25 springBoot自動(dòng)裝配的原理
- 2022-03-25 Mybatis聯(lián)合查詢(xún)的實(shí)現(xiàn)方法(多表聯(lián)合查詢(xún))
- 2022-08-03 docker安裝redis掛載容器卷同時(shí)開(kāi)啟持久化_docker
- 2022-10-24 React報(bào)錯(cuò)之Parameter?event?implicitly?has?an?any?type
- 2023-01-07 一篇文章徹底弄懂Python中的if?__name__?==?__main___python
- 2022-10-27 Python使用pandas將表格數(shù)據(jù)進(jìn)行處理_python
- 2022-07-06 C語(yǔ)言超細(xì)致講解函數(shù)遞歸_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- 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)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤: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)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支