歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android --- Tween動畫示例

Android --- Tween動畫示例

日期:2017/3/1 10:23:24   编辑:Linux編程

1、在res/anim目錄下新建XML文件:tween_anim.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:Android="http://schemas.android.com/apk/res/android">
  3. <alpha
  4. android:fromAlpha="0.2"
  5. android:toAlpha="1.0"
  6. android:duration="3000"
  7. android:repeatMode="reverse"
  8. android:repeatCount="10" />
  9. <scale
  10. android:fromXScale="0.2"
  11. android:toXScale="1.0"
  12. android:fromYScale="0.2"
  13. android:toYScale="1.0"
  14. android:pivotX="50%"
  15. android:pivotY="50%"
  16. android:duration="3000"
  17. android:repeatMode="reverse"
  18. android:repeatCount="10" />
  19. <translate
  20. android:fromXDelta="50"
  21. android:toXDelta="100"
  22. android:fromYDelta="50"
  23. android:toYDelta="100"
  24. android:duration="3000"
  25. android:repeatMode="restart"
  26. android:repeatCount="10" />
  27. <rotate
  28. android:fromDegrees="0"
  29. android:toDegrees="360"
  30. android:duration="3000"
  31. android:repeatMode="restart"
  32. android:repeatCount="10" />
  33. </set>
2、在res/layout目錄下新建XML文件:tween_anim_layout.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <ImageView
  8. android:id="@+id/imgTween"
  9. android:src="@drawable/c01"
  10. android:layout_height="wrap_content"
  11. android:layout_width="wrap_content"
  12. android:layout_weight="1.0" />
  13. <Button
  14. android:id="@+id/btnControl"
  15. android:text="開始"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content" />
  18. </LinearLayout>
3、Activity裡面添加代碼:
  1. package com.bison;
  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 TweenAnimationDemo extends Activity {
  11. // 聲明一個開始停止的標識符
  12. private boolean flags = true;
  13. private ImageView imgTween;
  14. private Button btnCtrl;
  15. /** 初始化 */
  16. public void init() {
  17. imgTween = (ImageView) findViewById(R.id.imgTween);
  18. // 聲明Tween動畫
  19. final Animation anim = AnimationUtils.loadAnimation(this,
  20. R.anim.tween_anim);
  21. btnCtrl = (Button) findViewById(R.id.btnControl);
  22. btnCtrl.setOnClickListener(new OnClickListener() {
  23. public void onClick(View v) {
  24. if (flags) {
  25. btnCtrl.setText("停止");
  26. imgTween.startAnimation(anim);
  27. flags = false;
  28. } else {
  29. btnCtrl.setText("開始");
  30. imgTween.clearAnimation();
  31. flags = true;
  32. }
  33. }
  34. });
  35. }
  36. @Override
  37. protected void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.tween_anim_layout);
  40. init();
  41. }
  42. }
PS:Tween動畫有alpha(透明)、scale(縮放)、translate(移動)、rotate(旋轉),每個有不同的定義,仔細研究。
Copyright © Linux教程網 All Rights Reserved