歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android圖形與圖像處理-補間動畫

Android圖形與圖像處理-補間動畫

日期:2017/3/1 10:15:09   编辑:Linux編程

Android圖形與圖像處理-補間動畫

創建項目:TweenAnim

項目運行效果:

項目代碼:

動畫資源文件:兩個動畫資源文件

anim.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- 指定動畫勻速改變 -->
  3. <set xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:interpolator="@android:anim/linear_interpolator">
  5. <!-- 定義縮放變換 -->
  6. <scale android:fromXScale="1.0"
  7. android:toXScale="0.01"
  8. android:fromYScale="1.0"
  9. android:toYScale="0.01"
  10. android:pivotX="50%"
  11. android:pivotY="50%"
  12. android:fillAfter="true"
  13. android:duration="3000"/>
  14. <!-- 定義透明度的變換 -->
  15. <alpha
  16. android:fromAlpha="1"
  17. android:toAlpha="0.05"
  18. android:duration="3000"
  19. />
  20. <!-- 定義旋轉變換 -->
  21. <rotate
  22. android:fromDegrees="0"
  23. android:toDegrees="1800"
  24. android:pivotX="50%"
  25. android:pivotY="50%"
  26. android:duration="3000"/>
  27. </set>

reverse.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- 指定動畫勻速改變 -->
  3. <set xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:interpolator="@android:anim/linear_interpolator"
  5. android:startOffset="3000">
  6. <!-- 定義縮放變換 -->
  7. <scale android:fromXScale="0.01"
  8. android:toXScale="1"
  9. android:fromYScale="0.01"
  10. android:toYScale="1"
  11. android:pivotX="50%"
  12. android:pivotY="50%"
  13. android:fillAfter="true"
  14. android:duration="3000"
  15. />
  16. <!-- 定義透明度的變換 -->
  17. <alpha
  18. android:fromAlpha="0.05"
  19. android:toAlpha="1"
  20. android:duration="3000"
  21. />
  22. <!-- 定義旋轉變換 -->
  23. <rotate
  24. android:fromDegrees="1800"
  25. android:toDegrees="0"
  26. android:pivotX="50%"
  27. android:pivotY="50%"
  28. android:duration="3000"
  29. />
  30. </set>

布局文件:main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/LinearLayout1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. android:gravity="center_horizontal" >
  8. <Button
  9. android:id="@+id/bn"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="播放動畫"
  13. />
  14. <ImageView
  15. android:id="@+id/flower"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:src="@drawable/flower"
  19. />
  20. </LinearLayout>
TweenAnim.java
  1. package wwj.tweenanim;
  2. import java.util.Timer;
  3. import java.util.TimerTask;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.view.animation.Animation;
  10. import android.view.animation.AnimationUtils;
  11. import android.widget.Button;
  12. import android.widget.ImageView;
  13. public class TweenAnim extends Activity {
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. final ImageView flower = (ImageView)findViewById(R.id.flower);
  19. //加載第一份動畫資源
  20. final Animation anim = AnimationUtils.loadAnimation(this, R.anim.anim);
  21. //設置動畫結束後保留結束狀態
  22. anim.setFillAfter(true);
  23. //加載第二份動畫資源
  24. final Animation reverse = AnimationUtils.loadAnimation(this, R.anim.reverse);
  25. //設置動畫結束後保留結束狀態
  26. reverse.setFillAfter(true);
  27. Button bn = (Button)findViewById(R.id.bn);
  28. final Handler handler = new Handler(){
  29. public void handleMessage(android.os.Message msg) {
  30. if(msg.what == 0x123){
  31. flower.startAnimation(reverse);
  32. }
  33. };
  34. };
  35. bn.setOnClickListener(new OnClickListener() {
  36. @Override
  37. public void onClick(View v) {
  38. // TODO Auto-generated method stub
  39. flower.startAnimation(anim);
  40. //設置3。5秒後啟動第二個動畫
  41. new Timer().schedule(new TimerTask() {
  42. @Override
  43. public void run() {
  44. // TODO Auto-generated method stub
  45. handler.sendEmptyMessage(0x123);
  46. }
  47. }, 3500);
  48. }
  49. });
  50. }
  51. }
Copyright © Linux教程網 All Rights Reserved