歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:簡單的animation動畫效果的實現過程

Android開發教程:簡單的animation動畫效果的實現過程

日期:2017/3/1 10:14:06   编辑:Linux編程
今天學習動畫:
Animations的分類:
1.TweenedAnimation :提供旋轉、移動、伸展、和淡出的效果
Alpha:淡入淡出效果 Scale:縮放效果
Rotate:旋轉效果 Translate: 移動效果
2.Frame-by-Frame Animation :創建Drawable的序列,可以按照指定的時間間隙一個個顯示(1s中播放24張以上的圖片就可以變成視頻了)也可以做成動態廣告的形式

3.res文件加下加入XMl文件,可以使用XML文件實現控制動畫,XML文件中添加Set標簽,標簽中加入rotate、alpha、scale、或者translate 代碼中實現AnimationUtil裝載xml

文件並生成,Animation對象需要注意的是

<!--pivotX pivotY旋轉時候 的坐標絕對位置的

50這種方法使用的是絕對位置
50%相對於空間本身定位

"50%p"的意思是相對於空控件的父控件的定位 -->

4.接著咱們看看代碼的實現過程

首先看看兩種方法共同的布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="horizontal">
  10. <Button
  11. android:id="@+id/Alpha"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="淡入淡出" />
  15. <Button
  16. android:id="@+id/scale"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="縮放效果" />
  20. <Button
  21. android:id="@+id/rotate"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="旋轉效果" />
  25. <Button
  26. android:id="@+id/translate"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="移動效果" />
  30. </LinearLayout>
  31. <ImageView
  32. android:id="@+id/image"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:src="@drawable/a" />
  36. </LinearLayout>
5.看看第一種方法的活動實現的過程
  1. package com.wang;
  2. import com.wang.R.id;
  3. import android.R.anim;
  4. import android.R.integer;
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.provider.ContactsContract.CommonDataKinds.Im;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.view.animation.AccelerateDecelerateInterpolator;
  11. import android.view.animation.AccelerateInterpolator;
  12. import android.view.animation.AlphaAnimation;
  13. import android.view.animation.Animation;
  14. import android.view.animation.AnimationSet;
  15. import android.view.animation.RotateAnimation;
  16. import android.view.animation.ScaleAnimation;
  17. import android.view.animation.Transformation;
  18. import android.view.animation.TranslateAnimation;
  19. import android.widget.Button;
  20. import android.widget.ImageView;
  21. public class AnimationTestActivity extends Activity {
  22. private ImageView image = null;
  23. private Button Alphabutton = null;
  24. private Button scalebutton = null;
  25. private Button rotatebutton = null;
  26. private Button translatebutton = null;
  27. public void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.main);
  30. image = (ImageView) findViewById(R.id.image);
  31. Alphabutton = (Button) findViewById(R.id.Alpha);
  32. scalebutton = (Button) findViewById(R.id.scale);
  33. rotatebutton = (Button) findViewById(R.id.rotate);
  34. translatebutton = (Button) findViewById(R.id.translate);
  35. // 淡入淡出按鈕的監聽時間
  36. Alphabutton.setOnClickListener(new OnClickListener() {
  37. public void onClick(View v) {
  38. // 創建一個AnimationSet對象
  39. AnimationSet animationSet = new AnimationSet(true);
  40. // j加速播放
  41. animationSet.setInterpolator(new AccelerateInterpolator());
  42. // //創建一個AnimationSet對象淡出旋轉二合一
  43. AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
  44. RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
  45. Animation.RELATIVE_TO_PARENT, 1f,// X軸
  46. Animation.RELATIVE_TO_PARENT, 0f);// y軸
  47. // 將alphaAnimation對象添加到animationSet中
  48. animationSet.addAnimation(alphaAnimation);
  49. animationSet.addAnimation(rotateAnimation);
  50. // 顯示的時間為1s
  51. alphaAnimation.setDuration(3000);
  52. // 開始執行動畫
  53. image.startAnimation(animationSet);
  54. // 設置重復的次數
  55. animationSet.setRepeatCount(4);
  56. }
  57. });
  58. // 縮放按鈕的監聽事件
  59. scalebutton.setOnClickListener(new OnClickListener() {
  60. @Override
  61. public void onClick(View v) {
  62. AnimationSet animationSet = new AnimationSet(true);
  63. // 從1縮放的0.1
  64. ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,
  65. 0.1f, Animation.RELATIVE_TO_SELF, 0.5f,
  66. Animation.RELATIVE_TO_SELF, 0.5f);
  67. animationSet.addAnimation(scaleAnimation);
  68. // 執行前等待的時間
  69. animationSet.setStartOffset(1000);
  70. // dd動畫執行之前和之後的狀態
  71. animationSet.setFillAfter(true);
  72. animationSet.setFillBefore(false);
  73. animationSet.setDuration(5000);
  74. image.startAnimation(animationSet);
  75. }
  76. });
  77. // 旋轉按鈕的單擊事件
  78. rotatebutton.setOnClickListener(new OnClickListener() {
  79. @Override
  80. public void onClick(View v) {
  81. AnimationSet animationSet = new AnimationSet(true);
  82. // 創建對象 參數 從0度轉到360度
  83. /*********
  84. **一個動畫控制旋轉的一個對象。這個旋轉發生int xy平面。 您可以指定點使用的中心旋轉,在那裡(0,0)是左上角點。
  85. * 如果未指定,(0,0)是默認的旋轉點
  86. **/
  87. RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
  88. Animation.RELATIVE_TO_PARENT, 1f,// X軸
  89. Animation.RELATIVE_TO_PARENT, 0f);// y軸
  90. rotateAnimation.setDuration(5000);
  91. animationSet.addAnimation(rotateAnimation);
  92. image.startAnimation(animationSet);
  93. }
  94. });
  95. // 移動按鈕的點擊事件
  96. translatebutton.setOnClickListener(new OnClickListener() {
  97. public void onClick(View v) {
  98. AnimationSet animationSet = new AnimationSet(true);
  99. TranslateAnimation translatebutton = new TranslateAnimation(
  100. Animation.RELATIVE_TO_SELF, 0f,// //X軸
  101. Animation.RELATIVE_TO_SELF, 0.5f,// y軸
  102. Animation.RELATIVE_TO_SELF, 0f,// X軸
  103. Animation.RELATIVE_TO_SELF, 1.0f);// y軸
  104. translatebutton.setDuration(5000);
  105. }
  106. });
  107. }
  108. }
6.第二種方式實現的時候要在res的文件之下在建一個文件夾anim。文件夾裡面包含的四個文件如下
alpha.xml 淡入淡出效果的文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:interpolator="@android:anim/accelerate_interpolator" >
  4. <alpha
  5. android:duration="500"
  6. android:fromAlpha="1.0"
  7. android:startOffset="500"
  8. android:toAlpha="0.0" />
  9. </set>

rotate.xml 旋轉效果的文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:interpolator="@android:anim/accelerate_interpolator" >
  4. <rotate
  5. android:fromDegrees="0"
  6. android:toDegrees="350"
  7. android:pivotX="50%p"
  8. android:pivotY="50%p"
  9. android:duration="3000" />
  10. <!--pivotX pivotY旋轉時候 的坐標絕對位置的
  11. 50這種方法使用的是絕對位置
  12. 50%相對於空間本身定位
  13. "50%p"的意思是相對於空控件的父控件的定位
  14. -->
  15. </set>

sacle.xml 縮放效果的文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:interpolator="@android:anim/accelerate_interpolator" >
  4. <scale
  5. android:fromXScale="1.0"
  6. android:toXScale="0.0"
  7. android:fromYScale="1.0"
  8. android:toYScale="0.0"
  9. android:pivotX="50%"
  10. android:pivotY="50%"
  11. android:duration="3000"/>
  12. </set>

translate.xml 移動效果的文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:interpolator="@android:anim/accelerate_interpolator" >
  4. <translate
  5. android:fromXDelta="50%"
  6. android:toXDelta="100%"
  7. android:fromYDelta="0%"
  8. android:toYDelta="100%"
  9. android:duration="3000" />
  10. </set>

7.接著看看第二種方法的Activity的實現過程

  1. package com.wang;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.view.animation.Animation;
  7. import android.view.animation.AnimationUtils;
  8. import android.widget.Button;
  9. import android.widget.ImageView;
  10. public class AnimationXMLActivity extends Activity {
  11. private ImageView image;
  12. private Button alphabutton;
  13. private Button rotatebutton;
  14. private Button saclebutton;
  15. private Button translatebutton;
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. image = (ImageView) findViewById(R.id.image);
  20. alphabutton = (Button) findViewById(R.id.alpha);
  21. rotatebutton = (Button) findViewById(R.id.roate);
  22. saclebutton = (Button) findViewById(R.id.scale);
  23. translatebutton = (Button) findViewById(R.id.translate);
  24. // 淡入淡出按鈕的監聽事件
  25. alphabutton.setOnClickListener(new OnClickListener() {
  26. @Override
  27. public void onClick(View v) {
  28. // s使用AnimationUtils裝載動畫設置文件
  29. Animation animation = AnimationUtils.loadAnimation(
  30. AnimationXMLActivity.this, R.anim.alpha);
  31. // 啟動
  32. image.startAnimation(animation);
  33. }
  34. });
  35. // 旋轉效果的監聽事件
  36. rotatebutton.setOnClickListener(new OnClickListener() {
  37. @Override
  38. public void onClick(View v) {
  39. // s使用AnimationUtils裝載動畫設置文件
  40. Animation animation = AnimationUtils.loadAnimation(
  41. AnimationXMLActivity.this, R.anim.rotate);
  42. image.startAnimation(animation);// TODO Auto-generated method
  43. // stub
  44. }
  45. });
  46. // 縮放組件的效果監聽
  47. saclebutton.setOnClickListener(new OnClickListener() {
  48. @Override
  49. public void onClick(View v) {
  50. // s使用AnimationUtils裝載動畫設置文件
  51. Animation animation = AnimationUtils.loadAnimation(
  52. AnimationXMLActivity.this, R.anim.sacle);
  53. image.startAnimation(animation); // TODO Auto-generated method
  54. // stub
  55. }
  56. });
  57. // 移動效果的監聽事件
  58. translatebutton.setOnClickListener(new OnClickListener() {
  59. @Override
  60. public void onClick(View v) {
  61. // TODO Auto-generated method stub
  62. // s使用AnimationUtils裝載動畫設置文件
  63. Animation animation = AnimationUtils.loadAnimation(
  64. AnimationXMLActivity.this, R.anim.translate);
  65. image.startAnimation(animation);
  66. }
  67. });
  68. }
  69. }

8.殊路同歸,這兩種方法實現的功能都差不多,第一張圖片是原圖,第二張圖片是淡入淡出的效果,第三張是旋轉的效果圖,第四章是縮放的效果圖,第五章是移動的效果圖

由於是動畫效果,所以只能截取圖片的一部分

Copyright © Linux教程網 All Rights Reserved