Android 動畫之ScaleAnimation應用詳解

  1. android中提供了4中動畫:   
  2. AlphaAnimation 透明度動畫效果   
  3. ScaleAnimation 縮放動畫效果   
  4. TranslateAnimation 位移動畫效果   
  5. RotateAnimation 旋轉動畫效果   
  6.   
  7. 本節講解ScaleAnimation 動畫,   
  8. ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)   
  9. 參數說明:   
  10.   
  11. 複製代碼 代碼如下:  
  12.   
  13. float fromX 動畫起始時 X座標上的伸縮尺寸   
  14. float toX 動畫結束時 X座標上的伸縮尺寸   
  15. float fromY 動畫起始時Y座標上的伸縮尺寸   
  16. float toY 動畫結束時Y座標上的伸縮尺寸   
  17. int pivotXType 動畫在X軸相對於物件位置類型   
  18. float pivotXValue 動畫相對於物件的X座標的開始位置   
  19. int pivotYType 動畫在Y軸相對於物件位置類型   
  20. float pivotYValue 動畫相對於物件的Y座標的開始位置   
  21.   
  22.   
  23. 代碼:   
  24.   
  25. 複製代碼 代碼如下:  
  26.   
  27. public class MainActivity extends Activity {   
  28. ImageView image;   
  29. Button start;   
  30. Button cancel;   
  31. @Override   
  32. public void onCreate(Bundle savedInstanceState) {   
  33. super.onCreate(savedInstanceState);   
  34. setContentView(R.layout.activity_main);   
  35. image = (ImageView) findViewById(R.id.main_img);   
  36. start = (Button) findViewById(R.id.main_start);   
  37. cancel = (Button) findViewById(R.id.main_cancel);   
  38. /** 設置縮放動畫 */   
  39. final ScaleAnimation animation =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,   
  40. Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);   
  41. animation.setDuration(2000);//設置動畫持續時間   
  42. /** 常用方法 */   
  43. //animation.setRepeatCount(int repeatCount);//設置重複次數   
  44. //animation.setFillAfter(boolean);//動畫執行完後是否停留在執行完的狀態   
  45. //animation.setStartOffset(long startOffset);//執行前的等待時間   
  46. start.setOnClickListener(new OnClickListener() {   
  47. public void onClick(View arg0) {   
  48. image.setAnimation(animation);   
  49. /** 開始動畫 */   
  50. animation.startNow();   
  51. }   
  52. });   
  53. cancel.setOnClickListener(new OnClickListener() {   
  54. public void onClick(View v) {   
  55. /** 結束動畫 */   
  56. animation.cancel();   
  57. }   
  58. });   
  59. }   
  60. }  
[java] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. 本節講解ScaleAnimation 動畫,   
  2. ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)   
  3. 參數說明:   
  4. 複製代碼 代碼如下:  
  5. float fromX 動畫起始時 X座標上的伸縮尺寸   
  6. float toX 動畫結束時 X座標上的伸縮尺寸   
  7. float fromY 動畫起始時Y座標上的伸縮尺寸   
  8. float toY 動畫結束時Y座標上的伸縮尺寸   
  9. int pivotXType 動畫在X軸相對於物件位置類型   
  10. float pivotXValue 動畫相對於物件的X座標的開始位置   
  11. int pivotYType 動畫在Y軸相對於物件位置類型   
  12. float pivotYValue 動畫相對於物件的Y座標的開始位置   
  13.   
  14. 代碼:   
  15. 複製代碼 代碼如下:  
  16. public class MainActivity extends Activity {   
  17. ImageView image;   
  18. Button start;   
  19. Button cancel;   
  20. @Override   
  21. public void onCreate(Bundle savedInstanceState) {   
  22. super.onCreate(savedInstanceState);   
  23. setContentView(R.layout.activity_main);   
  24. image = (ImageView) findViewById(R.id.main_img);   
  25. start = (Button) findViewById(R.id.main_start);   
  26. cancel = (Button) findViewById(R.id.main_cancel);   
  27. /** 設置縮放動畫 */   
  28. final ScaleAnimation animation =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,   
  29. Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);   
  30. animation.setDuration(2000);//設置動畫持續時間   
  31. /** 常用方法 */   
  32. //animation.setRepeatCount(int repeatCount);//設置重複次數   
  33. //animation.setFillAfter(boolean);//動畫執行完後是否停留在執行完的狀態   
  34. //animation.setStartOffset(long startOffset);//執行前的等待時間   
  35. start.setOnClickListener(new OnClickListener() {   
  36. public void onClick(View arg0) {   
  37. image.setAnimation(animation);   
  38. /** 開始動畫 */   
  39. animation.startNow();   
  40. }   
  41. });   
  42. cancel.setOnClickListener(new OnClickListener() {   
  43. public void onClick(View v) {   
  44. /** 結束動畫 */   
  45. animation.cancel();   
  46. }   
  47. });   
  48. }   
  49. }  
[javascript] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1.    
[javascript] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. 本節講解RotateAnimation 動畫,   
  2. RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)   
  3. 參數說明:   
  4. float fromDegrees:旋轉的開始角度。   
  5. float toDegrees:旋轉的結束角度。   
  6. int pivotXType:X軸的伸縮模式,可以取值爲ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。   
  7. float pivotXValue:X座標的伸縮值。   
  8. int pivotYType:Y軸的伸縮模式,可以取值爲ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。   
  9. float pivotYValue:Y座標的伸縮值。   
  10. 代碼:   
  11. <div class="codetitle"><span style="CURSOR: pointer"><u>複製代碼</u></span> 代碼如下:</div><div id="code17269" class="codebody">  
  12. public class MainActivity extends Activity {   
  13. ImageView image;   
  14. Button start;   
  15. Button cancel;   
  16. @Override   
  17. public void onCreate(Bundle savedInstanceState) {   
  18. super.onCreate(savedInstanceState);   
  19. setContentView(R.layout.activity_main);   
  20. image = (ImageView) findViewById(R.id.main_img);   
  21. start = (Button) findViewById(R.id.main_start);   
  22. cancel = (Button) findViewById(R.id.main_cancel);   
  23. /** 設置旋轉動畫 */   
  24. final RotateAnimation animation =new RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF,   
  25. 0.5f,Animation.RELATIVE_TO_SELF,0.5f);   
  26. animation.setDuration(3000);//設置動畫持續時間   
  27. /** 常用方法 */   
  28. //animation.setRepeatCount(int repeatCount);//設置重複次數   
  29. //animation.setFillAfter(boolean);//動畫執行完後是否停留在執行完的狀態   
  30. //animation.setStartOffset(long startOffset);//執行前的等待時間   
  31. start.setOnClickListener(new OnClickListener() {   
  32. public void onClick(View arg0) {   
  33. image.setAnimation(animation);   
  34. /** 開始動畫 */   
  35. animation.startNow();   
  36. }   
  37. });   
  38. cancel.setOnClickListener(new OnClickListener() {   
  39. public void onClick(View v) {   
  40. /** 結束動畫 */   
  41. animation.cancel();   
  42. }   
  43. });   
  44. }   
  45. } </div>  
[javascript] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. 本節講解TranslateAnimation動畫,TranslateAnimation比較常用,比如QQ,網易新聞菜單條的動畫,就可以用TranslateAnimation實現,   
  2. 通過TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 來定義動畫   
  3.   
  4. 參數說明:   
  5. <div class="codetitle"><span style="CURSOR: pointer"><u>複製代碼</u></span> 代碼如下:</div><div id="code3207" class="codebody">  
  6. float fromXDelta 動畫開始的點離當前View X座標上的差值   
  7. float toXDelta 動畫結束的點離當前View X座標上的差值   
  8. float fromYDelta 動畫開始的點離當前View Y座標上的差值   
  9. float toYDelta 動畫開始的點離當前View Y座標上的差值   
  10. </div>  
  11. 常用方法:   
  12. <div class="codetitle"><span style="CURSOR: pointer"><u>複製代碼</u></span> 代碼如下:</div><div id="code94921" class="codebody">  
  13. animation.setDuration(long durationMillis);//設置動畫持續時間   
  14. animation.setRepeatCount(int i);//設置重複次數   
  15. animation.setRepeatMode(Animation.REVERSE);//設置反方向執行   
  16. </div>  
  17. Xml屬性:   
  18. <div class="codetitle"><span style="CURSOR: pointer"><u>複製代碼</u></span> 代碼如下:</div><div id="code72894" class="codebody">  
  19. android:duration:運行動畫的時間   
  20. android:repeatCount:定義動畫重複的時間   
  21. </div>  
  22. 代碼:   
  23. <div class="codetitle"><span style="CURSOR: pointer"><u>複製代碼</u></span> 代碼如下:</div><div id="code12415" class="codebody">  
  24. public class MainActivity extends Activity {   
  25. ImageView image;   
  26. Button start;   
  27. Button cancel;   
  28. @Override   
  29. public void onCreate(Bundle savedInstanceState) {   
  30. super.onCreate(savedInstanceState);   
  31. setContentView(R.layout.activity_main);   
  32. image = (ImageView) findViewById(R.id.main_img);   
  33. start = (Button) findViewById(R.id.main_start);   
  34. cancel = (Button) findViewById(R.id.main_cancel);   
  35. /** 設置位移動畫 向右位移150 */   
  36. final TranslateAnimation animation = new TranslateAnimation(0, 150,0, 0);   
  37. animation.setDuration(2000);//設置動畫持續時間   
  38. animation.setRepeatCount(2);//設置重複次數   
  39. animation.setRepeatMode(Animation.REVERSE);//設置反方向執行   
  40. start.setOnClickListener(new OnClickListener() {   
  41. public void onClick(View arg0) {   
  42. image.setAnimation(animation);   
  43. /** 開始動畫 */   
  44. animation.startNow();   
  45. }   
  46. });   
  47. cancel.setOnClickListener(new OnClickListener() {   
  48. public void onClick(View v) {   
  49. /** 結束動畫 */   
  50. animation.cancel();   
  51. }   
  52. });   
  53. }   
  54. } </div><div class="codebody"> </div><div class="codebody"> </div><div class="codebody"> </div><div class="codebody"> </div>  
[javascript] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. <pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code">本節講解AlphaAnimation 動畫,窗口的動畫效果,淡入淡出什麼的,有些遊戲的歡迎動畫,logo的淡入淡出效果就使用AlphaAnimation。   
  2. 直接看代碼:   
  3. 複製代碼 代碼如下:  
  4. public class MainActivity extends Activity {   
  5. ImageView image;   
  6. Button start;   
  7. Button cancel;   
  8. @Override   
  9. public void onCreate(Bundle savedInstanceState) {   
  10. super.onCreate(savedInstanceState);   
  11. setContentView(R.layout.activity_main);   
  12. image = (ImageView) findViewById(R.id.main_img);   
  13. start = (Button) findViewById(R.id.main_start);   
  14. cancel = (Button) findViewById(R.id.main_cancel);   
  15. /** 設置透明度漸變動畫 */   
  16. final AlphaAnimation animation = new AlphaAnimation(1, 0);   
  17. animation.setDuration(2000);//設置動畫持續時間   
  18. /** 常用方法 */   
  19. //animation.setRepeatCount(int repeatCount);//設置重複次數   
  20. //animation.setFillAfter(boolean);//動畫執行完後是否停留在執行完的狀態   
  21. //animation.setStartOffset(long startOffset);//執行前的等待時間   
  22. start.setOnClickListener(new OnClickListener() {   
  23. public void onClick(View arg0) {   
  24. image.setAnimation(animation);   
  25. /** 開始動畫 */   
  26. animation.startNow();   
  27. }   
  28. });   
  29. cancel.setOnClickListener(new OnClickListener() {   
  30. public void onClick(View v) {   
  31. /** 結束動畫 */   
  32. animation.cancel();   
  33. }   
  34. });   
  35. }   
  36. }</pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><br>  
  37. <pre></pre>  
  38. <p></p>  
  39. <p> </p>  
  40. <p> </p>  
  41. <p> </p>  
  42. <p> </p>  
  43. <p> </p>  
  44. <pre></pre>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章