歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android動畫Animation之Tween用代碼實現動畫效果

Android動畫Animation之Tween用代碼實現動畫效果

日期:2017/3/1 10:39:52   编辑:Linux編程

1、效果圖跟上一篇一樣,請參考Android動畫效果Animation之Tween實現簡單動畫;見 http://www.linuxidc.com/Linux/2012-01/51449.htm

接下來直接java代碼實現如何實現動畫:

定義 [java]

  1. private Animation animation_alpha,animation_scale,animation_translate,animation_rotate;
  2. private AnimationSet animationSet;
分別是透明度動畫、旋轉動畫、尺寸伸縮動畫、移動動畫

具體實現代碼如下:

[java]

  1. private void initAnimation() {
  2. //透明度控制動畫效果 alpha
  3. animation_alpha=new AlphaAnimation(0.1f,1.0f);
  4. //第一個參數fromAlpha為 動畫開始時候透明度
  5. //第二個參數toAlpha為 動畫結束時候透明度
  6. animation_alpha.setRepeatCount(-1);//設置循環
  7. animation_alpha.setDuration(5000);//設置時間持續時間為 5000毫秒
  8. // 旋轉效果rotate
  9. animation_rotate = new RotateAnimation(0, -720,
  10. RotateAnimation.RELATIVE_TO_SELF, 0.5f,
  11. RotateAnimation.RELATIVE_TO_SELF, 0.5f);
  12. //第一個參數fromDegrees為動畫起始時的旋轉角度 //第二個參數toDegrees為動畫旋轉到的角度
  13. //第三個參數pivotXType為動畫在X軸相對於物件位置類型 //第四個參數pivotXValue為動畫相對於物件的X坐標的開始位置
  14. //第五個參數pivotXType為動畫在Y軸相對於物件位置類型 //第六個參數pivotYValue為動畫相對於物件的Y坐標的開始位置
  15. animation_rotate.setRepeatCount(-1);
  16. animation_rotate.setDuration(5000);//設置時間持續時間為 5000毫秒
  17. //尺寸伸縮動畫效果 scale
  18. animation_scale=new ScaleAnimation(0.1f,3.0f,0.1f,3.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
  19. //第一個參數fromX為動畫起始時 X坐標上的伸縮尺寸
  20. //第二個參數toX為動畫結束時 X坐標上的伸縮尺寸
  21. //第三個參數fromY為動畫起始時Y坐標上的伸縮尺寸
  22. //第四個參數toY為動畫結束時Y坐標上的伸縮尺寸
  23. /*說明:
  24. 以上四種屬性值
  25. 0.0表示收縮到沒有
  26. 1.0表示正常無伸縮
  27. 值小於1.0表示收縮
  28. 值大於1.0表示放大
  29. */
  30. //第五個參數pivotXType為動畫在X軸相對於物件位置類型
  31. //第六個參數pivotXValue為動畫相對於物件的X坐標的開始位置
  32. //第七個參數pivotXType為動畫在Y軸相對於物件位置類型
  33. //第八個參數pivotYValue為動畫相對於物件的Y坐標的開始位置
  34. animation_scale.setRepeatCount(-1);
  35. animation_scale.setDuration(5000);//設置時間持續時間為 5000毫秒
  36. //移動動畫效果translate
  37. animation_translate=new TranslateAnimation(-20f,300f,-20f,300f);
  38. //第一個參數fromXDelta為動畫起始時 X坐標上的移動位置
  39. //第二個參數toXDelta為動畫結束時 X坐標上的移動位置
  40. //第三個參數fromYDelta為動畫起始時Y坐標上的移動位置
  41. //第三個參數toYDelta為動畫結束時Y坐標上的移動位置
  42. animation_translate.setRepeatCount(-1);//設置動畫執行多少次,如果是-1的話就是一直重復
  43. animation_translate.setDuration(5000);//設置時間持續時間為 5000毫秒
  44. animationSet=new AnimationSet(true);
  45. animationSet.addAnimation(animation_alpha);//透明度
  46. animationSet.addAnimation(animation_rotate);//旋轉
  47. animationSet.addAnimation(animation_scale);//尺寸伸縮
  48. animationSet.addAnimation(animation_translate);//移動
  49. image.startAnimation(animationSet);//開始播放
  50. }

nimationSet這個類是:

Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform. If AnimationSet sets any properties that its children also set (for example, duration or fillBefore), the values of AnimationSet override the child values.

意思是:代表一群動畫應該一同玩耍。轉變是由每個個體的動畫在一起,成為一個單一的變換。如果AnimationSet設置任何屬性,其子女也設置(例如,持續時間或fillBefore)的值AnimationSet推翻這孩子的價值觀。
Copyright © Linux教程網 All Rights Reserved