Android實(shí)現(xiàn)縮放動(dòng)畫
本文實(shí)例為大家分享了Android實(shí)現(xiàn)縮放動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下
核心方法
public void startAnimation(Animation animation)
執(zhí)行動(dòng)畫,參數(shù)可以是各種動(dòng)畫的對(duì)象,Animation的多態(tài),也可以是組合動(dòng)畫,后面會(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)畫對(duì)象 ? ? ScaleAnimation sa = new ScaleAnimation(0f,1.0f,0f,1.0f); ? ? // 設(shè)置動(dòng)畫播放的時(shí)間 ? ? sa.setDuration(1000); ? ? // 開始播放動(dòng)畫 ? ? iv.startAnimation(sa); }
效果
以圖片左上角為原點(diǎn),從沒有,放大到圖片原大小
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,所以是在圖片左上角開始縮放
用法
ScaleAnimation sa = new ScaleAnimation(0f, 1.0f, 0f, 1.0f, iv.getWidth() / 2, iv.getHeight() / 2); // 設(shè)置動(dòng)畫播放的時(shí)間 sa.setDuration(1000); // 開始播放動(dòng)畫 iv.startAnimation(sa);
效果
以圖片的中心為原點(diǎn),從沒有放大到圖片原大小
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)畫對(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)畫播放的時(shí)間 sa.setDuration(1000); // 開始播放動(dòng)畫 iv.startAnimation(sa);
和上面6個(gè)參數(shù)的相比只是多了第5和第7個(gè)參數(shù),分別設(shè)置他們的類型,注釋里面已經(jīng)說明了,可以設(shè)置Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT類型
效果
效果和上面一樣,以圖片的中心為原點(diǎn),從沒有放大到圖片原大小。
設(shè)置動(dòng)畫重復(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)畫重復(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);
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android學(xué)習(xí)筆記之ContentProvider和Uri詳解
本篇文章主要介紹了Android學(xué)習(xí)筆記之ContentProvider和Uri詳解,對(duì)于學(xué)習(xí)Android的朋友具有一定的參考價(jià)值,有需要可以可以了解一下。2016-11-11Android?Flutter控件封裝之視頻進(jìn)度條的實(shí)現(xiàn)
這篇文章主要來和大家分享一個(gè)很簡單的控制器封裝案例,包含了基本的播放暫停,全屏和退出全屏,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-06-06Android中使用開源框架eventbus3.0實(shí)現(xiàn)fragment之間的通信交互
本文主要介紹了Android中使用開源框架eventbus3.0實(shí)現(xiàn)fragment之間的通信交互的方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02Android Intent 用法全面總結(jié)及實(shí)例代碼
這篇文章主要介紹了Android Intent 用法全面總結(jié)的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下2016-09-09android文件存儲(chǔ)和SharedPreferences存儲(chǔ)的項(xiàng)目實(shí)例
本文主要介紹了android文件存儲(chǔ)和SharedPreferences存儲(chǔ)的項(xiàng)目實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Android App中制作仿MIUI的Tab切換效果的實(shí)例分享
這篇文章主要介紹了Android App中制作仿MIUI的Tab切換效果的實(shí)例分享,實(shí)現(xiàn)具有跟隨手指滾動(dòng)而滾動(dòng)功能的ViewPagerIndicator,需要的朋友可以參考下2016-04-04