简单的补间动画代码

来源:互联网 发布:逆袭网络剧第5集 编辑:程序博客网 时间:2024/05/22 03:01
补间动画(Tween Animation)
补间动画与逐帧动画在本质上是不同的,逐帧动画通过连续播放图片来模拟动画的效果,而补间动画则是通过在两个关键帧之间补充渐变的动画效果来实现的。补间动画的优点是可以节省空间。目前Android应用框架支持的补间动画效果有以下5种。具体实现在android.view.animation类库中。
AlphaAnimation:透明度(alpha)渐变效果,对应<alpha/>标签。
TranslateAnimation:位移渐变,需要指定移动点的开始和结束坐标,对应<translate/>标签。
ScaleAnimation:缩放渐变,可以指定缩放的参考点,对应<scale/>标签。
RotateAnimation:旋转渐变,可以指定旋转的参考点,对应<rotate/>标签。
AnimationSet:组合渐变,支持组合多种渐变效果,对应<set/>标签。
补间动画的效果同样可以使用XML语言来定义,这些动画模板文件通常会被放在Android项目的res/anim/目录下。
下面是具体实现源码:
MainActivity.java封装实现的方法:
[java]view plaincopy
  1. package com.example.lesson19_tween;

  2. import com.example.lesson19_tween.R;

  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.animation.Animation;
  8. import android.view.animation.AnimationUtils;
  9. import android.view.animation.ScaleAnimation;
  10. import android.view.animation.TranslateAnimation;
  11. import android.widget.ImageView;

  12. publicclass MainActivityextends Activity {

  13. private ImageView imageView;

  14. @Override
  15. protectedvoid onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);

  18. imageView = (ImageView) findViewById(R.id.imageView1);
  19. }

  20. @Override
  21. publicboolean onCreateOptionsMenu(Menu menu) {
  22. // Inflate the menu; this adds items to the action bar if it is present.
  23. getMenuInflater().inflate(R.menu.main, menu);
  24. returntrue;
  25. }

  26. // 透明动画
  27. publicvoid alphaImpl(View v) {

  28. Animation animation = AnimationUtils.loadAnimation(this,
  29. R.anim.alpha_demo);
  30. imageView.startAnimation(animation);
  31. }

  32. // 旋转动画
  33. publicvoid rotateImpl(View v) {
  34. Animation animation = AnimationUtils.loadAnimation(this,
  35. R.anim.rotate_demo);
  36. imageView.startAnimation(animation);
  37. }

  38. // 缩放动画
  39. publicvoid scaleImpl(View v) {
  40. Animation animation = AnimationUtils.loadAnimation(this,
  41. R.anim.scale_demo);
  42. imageView.startAnimation(animation);
  43. }

  44. // 移动效果
  45. publicvoid translateImpl(View v) {
  46. // XML文件
  47. Animation animation = AnimationUtils.loadAnimation(this,
  48. R.anim.translate_demo);

  49. animation.setRepeatCount(Animation.INFINITE);//循环显示
  50. imageView.startAnimation(animation);

  51. /*
  52. * 第一种 imageView.setAnimation(animation); animation.start();
  53. */
  54. // 第二种

  55. // Java代码
  56. /*
  57. * TranslateAnimation translateAnimation = new TranslateAnimation(0,
  58. * 200, 0, 0); translateAnimation.setDuration(2000);
  59. * imageView.startAnimation(translateAnimation);
  60. */
  61. }

  62. // 综合实现set_demo.xml中的动画
  63. publicvoid setAll(View v) {
  64. Animation animation = AnimationUtils.loadAnimation(this,
  65. R.anim.set_demo);
  66. imageView.startAnimation(animation);
  67. }

  68. }

alpha_demo.xml
[html]view plaincopy
  1. <alphaxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:interpolator="@android:anim/accelerate_decelerate_interpolator"
  3. android:fromAlpha="1.0"
  4. android:toAlpha="0.1"
  5. android:duration="2000"/>
  6. <!--
  7. fromAlpha :起始透明度
  8. toAlpha:结束透明度
  9. 1.0表示完全不透明
  10. 0.0表示完全透明
  11. -->
rotate_demo.xml
[html]view plaincopy
  1. <rotatexmlns:android="http://schemas.android.com/apk/res/android"
  2. android:interpolator="@android:anim/accelerate_decelerate_interpolator"
  3. android:fromDegrees="0"
  4. android:toDegrees="360"
  5. android:duration="1000"
  6. android:repeatCount="1"
  7. android:repeatMode="reverse"/>
  8. <!--
  9. fromDegrees:表示旋转的起始角度
  10. toDegrees:表示旋转的结束角度
  11. repeatCount:旋转的次数 默认值是0 代表旋转1次 如果值是repeatCount=4 旋转5次,值为-1或者infinite时,表示补间动画永不停止
  12. repeatMode 设置重复的模式。默认是restart。当repeatCount的值大于0或者为infinite时才有效。
  13. repeatCount=-1 或者infinite 循环了
  14. 还可以设成reverse,表示偶数次显示动画时会做与动画文件定义的方向相反的方向动行。
  15. -->
scale_demo.xml
[html]view plaincopy
  1. <scalexmlns:android="http://schemas.android.com/apk/res/android"
  2. android:interpolator="@android:anim/accelerate_interpolator"
  3. android:fromXScale="0.2"
  4. android:toXScale="1.5"
  5. android:fromYScale="0.2"
  6. android:toYScale="1.5"
  7. android:pivotX="50%"
  8. android:pivotY="50%"
  9. android:duration="2000"/>

  10. <!--
  11. fromXScale:表示沿着x轴缩放的起始比例
  12. toXScale:表示沿着x轴缩放的结束比例

  13. fromYScale:表示沿着y轴缩放的起始比例
  14. toYScale:表示沿着y轴缩放的结束比例

  15. 图片中心点:
  16. android:pivotX="50%"
  17. android:pivotY="50%"

  18. -->

translate_demo.xml
[html]view plaincopy
  1. <translatexmlns:android="http://schemas.android.com/apk/res/android"
  2. android:interpolator="@android:anim/accelerate_decelerate_interpolator"
  3. android:fromXDelta="0"
  4. android:toXDelta="320"
  5. android:fromYDelta="0"
  6. android:toYDelta="0"
  7. android:duration="2000"/>

  8. <!--
  9. android:interpolator 动画的渲染器
  10. 1、accelerate_interpolator(动画加速器) 使动画在开始的时候 最慢,然后逐渐加速
  11. 2、decelerate_interpolator(动画减速器)使动画在开始的时候 最快,然后逐渐减速
  12. 3、accelerate_decelerate_interpolator(动画加速减速器)
  13. 中间位置分层: 使动画在开始的时候 最慢,然后逐渐加速
  14. 使动画在开始的时候 最快,然后逐渐减速 结束的位置最慢
  15. fromXDelta 动画起始位置的横坐标
  16. toXDelta 动画起结束位置的横坐标
  17. fromYDelta 动画起始位置的纵坐标
  18. toYDelta 动画结束位置的纵坐标
  19. duration 动画的持续时间
  20. -->

set_demo.xml
[html]view plaincopy
  1. <setxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:interpolator="@android:anim/decelerate_interpolator"
  3. android:shareInterpolator="true">

  4. <scale
  5. android:duration="2000"
  6. android:fromXScale="0.2"
  7. android:fromYScale="0.2"
  8. android:pivotX="50%"
  9. android:pivotY="50%"
  10. android:toXScale="1.5"
  11. android:toYScale="1.5"/>

  12. <rotate
  13. android:duration="1000"
  14. android:fromDegrees="0"
  15. android:repeatCount="1"
  16. android:repeatMode="reverse"
  17. android:toDegrees="360"/>

  18. <translate
  19. android:duration="2000"
  20. android:fromXDelta="0"
  21. android:fromYDelta="0"
  22. android:toXDelta="320"
  23. android:toYDelta="0"/>

  24. <alpha
  25. android:duration="2000"
  26. android:fromAlpha="1.0"
  27. android:toAlpha="0.1"/>



  28. </set>

布局文件:
[html]view plaincopy
  1. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity">

  10. <Button
  11. android:id="@+id/button1"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_alignParentLeft="true"
  15. android:layout_alignParentTop="true"
  16. android:layout_marginLeft="23dp"
  17. android:layout_marginTop="15dp"
  18. android:onClick="translateImpl"
  19. android:text="@string/text_translate"/>

  20. <Button
  21. android:id="@+id/button2"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_alignBottom="@+id/button1"
  25. android:layout_marginLeft="21dp"
  26. android:onClick="rotateImpl"
  27. android:layout_toRightOf="@+id/imageView1"
  28. android:text="@string/text_rotate"/>

  29. <Button
  30. android:id="@+id/button3"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_alignRight="@+id/button1"
  34. android:layout_below="@+id/button1"
  35. android:layout_marginTop="32dp"
  36. android:onClick="scaleImpl"
  37. android:text="@string/text_scale"/>

  38. <Button
  39. android:id="@+id/button4"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_alignBaseline="@+id/button3"
  43. android:layout_alignBottom="@+id/button3"
  44. android:layout_alignLeft="@+id/button2"
  45. android:onClick="alphaImpl"
  46. android:text="@string/text_alpha"/>

  47. <Button
  48. android:id="@+id/button5"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_below="@+id/button3"
  52. android:layout_centerHorizontal="true"
  53. android:onClick="setAll"
  54. android:text="@string/text_set"/>

  55. <ImageView
  56. android:id="@+id/imageView1"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:layout_below="@+id/button5"
  60. android:layout_marginTop="48dp"
  61. android:layout_toRightOf="@+id/button3"
  62. android:src="@drawable/ic_launcher"/>

  63. </RelativeLayout>
原创粉丝点击