Android實(shí)現(xiàn)簡單旋轉(zhuǎn)動(dòng)畫
本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡單旋轉(zhuǎn)動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下
核心方法
public void startAnimation(Animation animation)
執(zhí)行動(dòng)畫,參數(shù)可以是各種動(dòng)畫的對(duì)象,Animation的多態(tài),也可以是組合動(dòng)畫,后面會(huì)有。
2個(gè)參數(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; }
- 第一個(gè)參數(shù)是圖片旋轉(zhuǎn)的起始度數(shù)
- 第二個(gè)參數(shù)是圖片旋轉(zhuǎn)結(jié)束的度數(shù)
用法
RotateAnimation ta = new RotateAnimation(0, 360); // 設(shè)置動(dòng)畫播放的時(shí)間 ta.setDuration(1000); // 開始播放動(dòng)畫 iv.startAnimation(ta);
效果
以圖片左上角為旋轉(zhuǎn)中心,順時(shí)針旋轉(zhuǎn)360度
4個(gè)參數(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(); ? ? }
- 頭兩個(gè)參數(shù)和上面兩個(gè)參數(shù)的構(gòu)造方法一樣,是開始和結(jié)束的角度
- 后兩個(gè)參數(shù)是設(shè)置圖片的旋轉(zhuǎn)中心
用法
RotateAnimation ta = new RotateAnimation(0, 360, iv.getWidth() / 2, iv.getHeight() / 2); // 設(shè)置動(dòng)畫播放的時(shí)間 ta.setDuration(1000); // 開始播放動(dòng)畫 iv.startAnimation(ta);
效果
以圖片中心為旋轉(zhuǎn)中心,順時(shí)針旋轉(zhuǎn)360度
6個(gè)參數(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個(gè)參數(shù)的構(gòu)造方法多了第三個(gè)和第五個(gè)參數(shù),其他用法一樣,第三個(gè)和四五個(gè)參數(shù)分別設(shè)置第四個(gè)和第六個(gè)參數(shù)的類型,四個(gè)參數(shù)的構(gòu)造沒有設(shè)置,是默認(rèn)設(shè)置了Animation.ABSOLUTE類型
用法
// 創(chuàng)建旋轉(zhuǎn)的動(dòng)畫對(duì)象 RotateAnimation ta = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f); // 設(shè)置動(dòng)畫播放的時(shí)間 ta.setDuration(1000); // 開始播放動(dòng)畫 iv.startAnimation(ta);
效果
和上面一樣,以圖片中心為旋轉(zhuǎn)中心,順時(shí)針旋轉(zhuǎn)360度。
設(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);
一直重復(fù)
sa.setRepeatCount(Animation.INFINITE);
設(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);
動(dòng)畫的監(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 ? ? ? ? ? ? } ?});
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)四個(gè)方向準(zhǔn)確監(jiān)聽
- Android如何監(jiān)聽屏幕旋轉(zhuǎn)
- Android屏幕旋轉(zhuǎn)之橫屏豎屏切換的實(shí)現(xiàn)
- Android6.0開發(fā)中屏幕旋轉(zhuǎn)原理與流程分析
- Android webview旋轉(zhuǎn)屏幕導(dǎo)致頁面重新加載問題解決辦法
- Android屏幕旋轉(zhuǎn) 處理Activity與AsyncTask的最佳解決方案
- 詳解Android中Runtime解決屏幕旋轉(zhuǎn)問題(推薦)
- Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)方法總結(jié)
- Android開發(fā) 旋轉(zhuǎn)屏幕導(dǎo)致Activity重建解決方法
- Android App獲取屏幕旋轉(zhuǎn)角度的方法
相關(guān)文章
Android?Flutter實(shí)現(xiàn)評(píng)分組件的示例代碼
在很多應(yīng)用中,我們都需要收集用戶的評(píng)分,比如商品滿意度、配送滿意度、應(yīng)用使用體驗(yàn)等等。本文就利用flutter_rating_bar實(shí)現(xiàn)簡易的評(píng)分組件,感興趣的可以2022-11-11android開發(fā)教程之時(shí)間對(duì)話框核心代碼
這篇文章主要介紹了android的時(shí)間對(duì)話框核心代碼,需要的朋友可以參考下2014-04-04Android開發(fā)中的Surface庫及用其制作播放器UI的例子
這篇文章主要介紹了Android開發(fā)中的Surface庫及用其制作播放器界面的例子,利用SurfaceView和SurfaceHolder可以高效地繪制和控制圖形界面,需要的朋友可以參考下2016-04-04android 拷貝sqlite數(shù)據(jù)庫到本地sd卡的方法
下面小編就為大家?guī)硪黄猘ndroid 拷貝sqlite數(shù)據(jù)庫到本地sd卡的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03OpenGL Shader實(shí)現(xiàn)簡單轉(zhuǎn)場效果詳解
轉(zhuǎn)場效果常出現(xiàn)再視頻剪輯當(dāng)中,用于銜接兩段視頻片段切換的過渡效果。本文將介紹如何利用OpenGL Shader實(shí)現(xiàn)簡單的轉(zhuǎn)場效果,需要的小伙伴可以參考一下2022-02-02