Android實現(xiàn)簡單旋轉(zhuǎn)動畫
本文實例為大家分享了Android實現(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
? ? ? ? ? ? }
?});以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實現(xiàn)屏幕旋轉(zhuǎn)四個方向準(zhǔn)確監(jiān)聽
- Android如何監(jiān)聽屏幕旋轉(zhuǎn)
- Android屏幕旋轉(zhuǎn)之橫屏豎屏切換的實現(xiàn)
- Android6.0開發(fā)中屏幕旋轉(zhuǎn)原理與流程分析
- Android webview旋轉(zhuǎn)屏幕導(dǎo)致頁面重新加載問題解決辦法
- Android屏幕旋轉(zhuǎn) 處理Activity與AsyncTask的最佳解決方案
- 詳解Android中Runtime解決屏幕旋轉(zhuǎn)問題(推薦)
- Android實現(xiàn)屏幕旋轉(zhuǎn)方法總結(jié)
- Android開發(fā) 旋轉(zhuǎn)屏幕導(dǎo)致Activity重建解決方法
- Android App獲取屏幕旋轉(zhuǎn)角度的方法
相關(guān)文章
Android getViewById和getLayoutInflater().inflate()的詳解及比較
這篇文章主要介紹了Android getViewById和getLayoutInflater().inflate()的詳解及比較的相關(guān)資料,這里對這兩種方法進行了詳細(xì)的對比,對于開始學(xué)習(xí)Android的朋友使用這兩種方法是個很好的資料,需要的朋友可以參考下2016-11-11
詳解Android App卸載后跳轉(zhuǎn)到指定的反饋頁面的方法
這篇文章主要介紹了Android App卸載后跳轉(zhuǎn)到指定的反饋頁面的方法,關(guān)鍵點是相關(guān)線程要判斷在目錄被消除以前作出響應(yīng),需要的朋友可以參考下2016-04-04
Android控件PullRefreshViewGroup實現(xiàn)下拉刷新和上拉加載
這篇文章主要為大家詳細(xì)介紹了Android控件PullRefreshViewGroup實現(xiàn)下拉刷新和上拉加載效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
android自定義組件實現(xiàn)儀表計數(shù)盤
這篇文章主要為大家詳細(xì)介紹了android自定義組件實現(xiàn)儀表計數(shù)盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
Android通訊錄開發(fā)之刪除功能的實現(xiàn)方法
這篇文章主要介紹了Android通訊錄開發(fā)之刪除功能的實現(xiàn)方法,有需要的朋友可以參考一下2014-01-01

