小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Android動(dòng)畫基礎(chǔ)

 levinLee 2011-05-05

Android動(dòng)畫基礎(chǔ)

動(dòng)畫類型

Android animation 由四種類型組成

XML

alpha
漸變透明度動(dòng)畫效果
scale
漸變尺寸伸縮動(dòng)畫效果
translate
畫面轉(zhuǎn)換位置移動(dòng)動(dòng)畫效果
rotate
畫面轉(zhuǎn)移旋轉(zhuǎn)動(dòng)畫效果



Java Code

AlphaAnimation
漸變透明度動(dòng)畫效果
ScaleAnimation
漸變尺寸伸縮動(dòng)畫效果
TranslateAnimation
畫面轉(zhuǎn)換位置移動(dòng)動(dòng)畫效果
RotateAnimation
畫面轉(zhuǎn)移旋轉(zhuǎn)動(dòng)畫效果


Android動(dòng)畫模式

Animation主要有兩種動(dòng)畫模式:

一種是tweened animation( 漸變動(dòng)畫 )

XML
JavaCode
alpha
AlphaAnimation
scale
ScaleAnimation



一種是frame by frame( 畫面轉(zhuǎn)換動(dòng)畫 )

XML
JavaCode
translate
TranslateAnimation
rotate
RotateAnimation




如何在XML 文件定義 動(dòng)畫

①  打開Eclipse ,新建 Android 工程
②  res 目錄中新建 anim 文件夾
③  anim 目錄中新建一個(gè) myanim.xml( 注意文件名小寫 )
④  加入XML 的動(dòng)畫代碼

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android ="http://schemas./apk/res/android">
  3.   <alpha/>
  4.   <scale/>
  5.   <translate/>
  6.   <rotate/>
  7. </set>
復(fù)制代碼


Android動(dòng)畫解析 --XML

<alpha>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas./apk/res/android" >
  3. <alpha
  4. android:fromAlpha="0.1"
  5. android:toAlpha="1.0"
  6. android:duration="3000"
  7. />
  8. <!-- 透明度控制動(dòng)畫效果 alpha
  9.         浮點(diǎn)型值:
  10.             fromAlpha 屬性為動(dòng)畫起始時(shí)透明度
  11.             toAlpha   屬性為動(dòng)畫結(jié)束時(shí)透明度
  12.             說明:
  13.                 0.0表示完全透明
  14.                 1.0表示完全不透明
  15.             以上值取0.0-1.0之間的float數(shù)據(jù)類型的數(shù)字
  16.         
  17.         長(zhǎng)整型值:
  18.             duration  屬性為動(dòng)畫持續(xù)時(shí)間
  19.             說明:     
  20.                 時(shí)間以毫秒為單位
  21. -->
  22. </set>
復(fù)制代碼


<scale>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas./apk/res/android">
  3.    <scale  
  4.           android:interpolator=
  5.                      "@android:anim/accelerate_decelerate_interpolator"
  6.           android:fromXScale="0.0"
  7.           android:toXScale="1.4"
  8.           android:fromYScale="0.0"
  9.           android:toYScale="1.4"
  10.           android:pivotX="50%"
  11.           android:pivotY="50%"
  12.           android:fillAfter="false"
  13.           android:duration="700" />
  14. </set>
  15. <!-- 尺寸伸縮動(dòng)畫效果 scale
  16.        屬性:interpolator 指定一個(gè)動(dòng)畫的插入器
  17.         在我試驗(yàn)過程中,使用android.res.anim中的資源 時(shí)候發(fā)現(xiàn)
  18.         有三種動(dòng)畫插入器:
  19.             accelerate_decelerate_interpolator  加速-減速 動(dòng)畫插入器
  20.             accelerate_interpolator        加速-動(dòng)畫插入器
  21.             decelerate_interpolator        減速- 動(dòng)畫插入器
  22.         其他的屬于特定的動(dòng)畫效果
  23.       浮點(diǎn)型值:
  24.          
  25.             fromXScale 屬性為動(dòng)畫起始時(shí) X坐標(biāo)上的伸縮尺寸   
  26.             toXScale   屬性為動(dòng)畫結(jié)束時(shí) X坐標(biāo)上的伸縮尺寸     
  27.         
  28.             fromYScale 屬性為動(dòng)畫起始時(shí)Y坐標(biāo)上的伸縮尺寸   
  29.             toYScale   屬性為動(dòng)畫結(jié)束時(shí)Y坐標(biāo)上的伸縮尺寸   
  30.         
  31.             說明:
  32.                  以上四種屬性值   
  33.    
  34.                     0.0表示收縮到?jīng)]有
  35.                     1.0表示正常無伸縮     
  36.                     值小于1.0表示收縮  
  37.                     值大于1.0表示放大
  38.         
  39.             pivotX     屬性為動(dòng)畫相對(duì)于物件的X坐標(biāo)的開始位置
  40.             pivotY     屬性為動(dòng)畫相對(duì)于物件的Y坐標(biāo)的開始位置
  41.         
  42.             說明:
  43.                     以上兩個(gè)屬性值 從0%-100%中取值
  44.                     50%為物件的X或Y方向坐標(biāo)上的中點(diǎn)位置
  45.         
  46.         長(zhǎng)整型值:
  47.             duration  屬性為動(dòng)畫持續(xù)時(shí)間
  48.             說明:   時(shí)間以毫秒為單位
  49.         布爾型值:
  50.             fillAfter 屬性 當(dāng)設(shè)置 為true ,該動(dòng)畫轉(zhuǎn)化在動(dòng)畫結(jié)束后被應(yīng)用
  51. -->
復(fù)制代碼


<translate>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas./apk/res/android">
  3. <translate
  4. android:fromXDelta="30"
  5. android:toXDelta="-80"
  6. android:fromYDelta="30"
  7. android:toYDelta="300"
  8. android:duration="2000"
  9. />
  10. <!-- translate 位置轉(zhuǎn)移動(dòng)畫效果
  11.         整型值:
  12.             fromXDelta 屬性為動(dòng)畫起始時(shí) X坐標(biāo)上的位置   
  13.             toXDelta   屬性為動(dòng)畫結(jié)束時(shí) X坐標(biāo)上的位置
  14.             fromYDelta 屬性為動(dòng)畫起始時(shí) Y坐標(biāo)上的位置
  15.             toYDelta   屬性為動(dòng)畫結(jié)束時(shí) Y坐標(biāo)上的位置
  16.             注意:
  17.                      沒有指定fromXType toXType fromYType toYType 時(shí)候,
  18.                      默認(rèn)是以自己為相對(duì)參照物            
  19.         長(zhǎng)整型值:
  20.             duration  屬性為動(dòng)畫持續(xù)時(shí)間
  21.             說明:   時(shí)間以毫秒為單位
  22. -->
  23. </set>
復(fù)制代碼


<rotate>

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas./apk/res/android">
  3. <rotate
  4.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"
  5.         android:fromDegrees="0"
  6.         android:toDegrees="+350"         
  7.         android:pivotX="50%"
  8.         android:pivotY="50%"     
  9.         android:duration="3000" />  
  10. <!-- rotate 旋轉(zhuǎn)動(dòng)畫效果
  11.        屬性:interpolator 指定一個(gè)動(dòng)畫的插入器
  12.              在我試驗(yàn)過程中,使用android.res.anim中的資源時(shí)候發(fā)現(xiàn)
  13.              有三種動(dòng)畫插入器:
  14.                 accelerate_decelerate_interpolator   加速-減速 動(dòng)畫插入器
  15.                 accelerate_interpolator               加速-動(dòng)畫插入器
  16.                 decelerate_interpolator               減速- 動(dòng)畫插入器
  17.              其他的屬于特定的動(dòng)畫效果
  18.                            
  19.        浮點(diǎn)數(shù)型值:
  20.             fromDegrees 屬性為動(dòng)畫起始時(shí)物件的角度   
  21.             toDegrees   屬性為動(dòng)畫結(jié)束時(shí)物件旋轉(zhuǎn)的角度 可以大于360度   
  22.         
  23.             說明:
  24.                      當(dāng)角度為負(fù)數(shù)——表示逆時(shí)針旋轉(zhuǎn)
  25.                      當(dāng)角度為正數(shù)——表示順時(shí)針旋轉(zhuǎn)              
  26.                      (負(fù)數(shù)from——to正數(shù):順時(shí)針旋轉(zhuǎn))   
  27.                      (負(fù)數(shù)from——to負(fù)數(shù):逆時(shí)針旋轉(zhuǎn))
  28.                      (正數(shù)from——to正數(shù):順時(shí)針旋轉(zhuǎn))
  29.                      (正數(shù)from——to負(fù)數(shù):逆時(shí)針旋轉(zhuǎn))      
  30.             pivotX     屬性為動(dòng)畫相對(duì)于物件的X坐標(biāo)的開始位置
  31.             pivotY     屬性為動(dòng)畫相對(duì)于物件的Y坐標(biāo)的開始位置
  32.                
  33.             說明:        以上兩個(gè)屬性值 從0%-100%中取值
  34.                          50%為物件的X或Y方向坐標(biāo)上的中點(diǎn)位置
  35.         長(zhǎng)整型值:
  36.             duration  屬性為動(dòng)畫持續(xù)時(shí)間
  37.             說明:       時(shí)間以毫秒為單位
  38. -->
  39. </set>
復(fù)制代碼


如何使用XML 中的動(dòng)畫效果
  1. public static Animation loadAnimation (Context context, int id)
  2. //第一個(gè)參數(shù)Context為程序 的上下文   
  3. //第二個(gè)參數(shù)id為動(dòng)畫XML文件的引用
  4. //例子
  5. myAnimation= AnimationUtils.loadAnimation(this,R.anim.my_action);
  6. //使用AnimationUtils類的靜態(tài)方法loadAnimation()來加載XML中的動(dòng)畫XML文件
復(fù)制代碼


如何在Java 代碼中定義動(dòng)畫
  1. //在代碼中定義 動(dòng)畫實(shí)例 對(duì)象
  2. private Animation myAnimation_Alpha;
  3. private Animation myAnimation_Scale;
  4. private Animation myAnimation_Translate;
  5. private Animation myAnimation_Rotate;
  6.    
  7.     //根據(jù)各自的構(gòu)造方法來初始化一個(gè)實(shí)例對(duì)象
  8. myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
  9. myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
  10.              Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  11. myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
  12. myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
  13.                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
復(fù)制代碼


Android動(dòng)畫解析 --JavaCode


AlphaAnimation

① AlphaAnimation類對(duì)象定義
  1. private AlphaAnimation myAnimation_Alpha;
復(fù)制代碼

② AlphaAnimation類對(duì)象構(gòu)造

  1. AlphaAnimation(float fromAlpha, float toAlpha)
  2. //第一個(gè)參數(shù)fromAlpha為 動(dòng)畫開始時(shí)候透明度
  3. //第二個(gè)參數(shù)toAlpha為 動(dòng)畫結(jié)束時(shí)候透明度
  4. myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
  5. //說明:
  6. //                0.0表示完全透明
  7. //                1.0表示完全不透明
復(fù)制代碼

③ 設(shè)置動(dòng)畫持續(xù)時(shí)間

  1. myAnimation_Alpha.setDuration(5000);
  2. //設(shè)置時(shí)間持續(xù)時(shí)間為 5000毫秒
復(fù)制代碼




ScaleAnimation

① ScaleAnimation類對(duì)象定義

  1. private AlphaAnimation myAnimation_Alpha;
復(fù)制代碼

② ScaleAnimation類對(duì)象構(gòu)造

  1. ScaleAnimation(float fromX, float toX, float fromY, float toY,
  2.            int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
  3. //第一個(gè)參數(shù)fromX為動(dòng)畫起始時(shí) X坐標(biāo)上的伸縮尺寸   
  4. //第二個(gè)參數(shù)toX為動(dòng)畫結(jié)束時(shí) X坐標(biāo)上的伸縮尺寸     
  5. //第三個(gè)參數(shù)fromY為動(dòng)畫起始時(shí)Y坐標(biāo)上的伸縮尺寸   
  6. //第四個(gè)參數(shù)toY為動(dòng)畫結(jié)束時(shí)Y坐標(biāo)上的伸縮尺寸  
  7. /*說明:
  8.                     以上四種屬性值   
  9.                     0.0表示收縮到?jīng)]有
  10.                     1.0表示正常無伸縮     
  11.                     值小于1.0表示收縮  
  12.                     值大于1.0表示放大
  13. */
  14. //第五個(gè)參數(shù)pivotXType為動(dòng)畫在X軸相對(duì)于物件位置類型  
  15. //第六個(gè)參數(shù)pivotXValue為動(dòng)畫相對(duì)于物件的X坐標(biāo)的開始位置
  16. //第七個(gè)參數(shù)pivotXType為動(dòng)畫在Y軸相對(duì)于物件位置類型   
  17. //第八個(gè)參數(shù)pivotYValue為動(dòng)畫相對(duì)于物件的Y坐標(biāo)的開始位置
  18. myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
  19.              Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
復(fù)制代碼

③ 設(shè)置動(dòng)畫持續(xù)時(shí)間

  1. myAnimation_Scale.setDuration(700);
  2. //設(shè)置時(shí)間持續(xù)時(shí)間為 700毫秒
復(fù)制代碼




TranslateAnimation


① TranslateAnimation類對(duì)象定義

  1. private AlphaAnimation myAnimation_Alpha;
復(fù)制代碼

② TranslateAnimation類對(duì)象構(gòu)造

  1. TranslateAnimation(float fromXDelta, float toXDelta,
  2.                        float fromYDelta, float toYDelta)
  3. //第一個(gè)參數(shù)fromXDelta為動(dòng)畫起始時(shí) X坐標(biāo)上的移動(dòng)位置   
  4. //第二個(gè)參數(shù)toXDelta為動(dòng)畫結(jié)束時(shí) X坐標(biāo)上的移動(dòng)位置      
  5. //第三個(gè)參數(shù)fromYDelta為動(dòng)畫起始時(shí)Y坐標(biāo)上的移動(dòng)位置     
  6. //第四個(gè)參數(shù)toYDelta為動(dòng)畫結(jié)束時(shí)Y坐標(biāo)上的移動(dòng)位置
復(fù)制代碼

③ 設(shè)置動(dòng)畫持續(xù)時(shí)間

  1. myAnimation_Translate.setDuration(2000);
  2. //設(shè)置時(shí)間持續(xù)時(shí)間為 2000毫秒
復(fù)制代碼



RotateAnimation
①  RotateAnimation類對(duì)象定義

  1. private AlphaAnimation myAnimation_Alpha;
復(fù)制代碼

②  RotateAnimation類對(duì)象構(gòu)造
  1. RotateAnimation(float fromDegrees, float toDegrees,
  2.             int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
  3. //第一個(gè)參數(shù)fromDegrees為動(dòng)畫起始時(shí)的旋轉(zhuǎn)角度   
  4. //第二個(gè)參數(shù)toDegrees為動(dòng)畫旋轉(zhuǎn)到的角度   
  5. //第三個(gè)參數(shù)pivotXType為動(dòng)畫在X軸相對(duì)于物件位置類型  
  6. //第四個(gè)參數(shù)pivotXValue為動(dòng)畫相對(duì)于物件的X坐標(biāo)的開始位置
  7. //第五個(gè)參數(shù)pivotXType為動(dòng)畫在Y軸相對(duì)于物件位置類型   
  8. //第六個(gè)參數(shù)pivotYValue為動(dòng)畫相對(duì)于物件的Y坐標(biāo)的開始位置
  9. myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
  10.                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
復(fù)制代碼

③  設(shè)置動(dòng)畫持續(xù)時(shí)間

  1. myAnimation_Rotate.setDuration(3000);
  2. //設(shè)置時(shí)間持續(xù)時(shí)間為 3000毫秒
復(fù)制代碼


如何使用 Java 代碼中的動(dòng)畫效果

使用從 View 父類繼承過來的方法 startAnimation ()來為 View 或是子類 View 等等添加一個(gè)動(dòng)畫效果
  1. public void startAnimation (Animation animation)

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多