读书人

[转]Animation自定义卡通片效果的实现

发布时间: 2012-09-20 09:36:50 作者: rapoo

[转]Animation自定义动画效果的实现

提供了三种动画效果:逐帧动画(frame-by-frame animation),这种动画和GIF一样,一帧一帧的显示来组成动画效果;布局动画(layout animation),这种动画用来设置layout内的所有UI控件;控件动画(view animation),这种是应用到具体某个view上的动画。

?在这三种动画实现中逐帧动画是最简单的,而控件动画是有点复杂的,要涉及到线性代数中的矩阵运算,下面就由易到难逐个介绍,先来看看逐帧动画如何实现。?逐帧动画逐帧动画是通过OPhone中的android.graphics.drawable.AnimationDrawable类来实现的,在该类中保存了帧序列以及显示的时间,为了简化动画的创建OPhone提供了一种通过XML来创建逐帧动画的方式,这样把动画的创建和代码分来以后如果需要修改动画内容,只需要修改资源文件就可以了不用修改代码,简化开发维护工作。在res/drawable/文件夹下创建一个XML文件,下面是一个示例文件(res\drawable\qq_animation.xml):view plaincopy to clipboardprint?
  1. <animation-list? ??
  2. ????xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ????android:oneshot="false"> ??
  4. <item?android:drawable="@drawable/qq001"?android:duration="80"/> ??
  5. <item?android:drawable="@drawable/qq002"?android:duration="80"/> ??
  6. <item?android:drawable="@drawable/qq003"?android:duration="80"/> ??
  7. <item?android:drawable="@drawable/qq004"?android:duration="80"/> ??
  8. <item?android:drawable="@drawable/qq005"?android:duration="80"/> ??
  9. <item?android:drawable="@drawable/qq006"?android:duration="80"/> ??
  10. <item?android:drawable="@drawable/qq007"?android:duration="80"/> ??
  11. <item?android:drawable="@drawable/qq008"?android:duration="80"/> ??
  12. </animation-list>??
Java代码??[转]Animation自定义卡通片效果的实现
  1. <animation-list?xmlns:android="http://schemas.android.com/apk/res/android"?android:oneshot="false">?<item?android:drawable="@drawable/qq001"?android:duration="80"/>?<item?android:drawable="@drawable/qq002"?android:duration="80"/>?<item?android:drawable="@drawable/qq003"?android:duration="80"/>?<item?android:drawable="@drawable/qq004"?android:duration="80"/>?<item?android:drawable="@drawable/qq005"?android:duration="80"/>?<item?android:drawable="@drawable/qq006"?android:duration="80"/>?<item?android:drawable="@drawable/qq007"?android:duration="80"/>?<item?android:drawable="@drawable/qq008"?android:duration="80"/>?</animation-list>??
??在上面的定义中通过animation-list来指定这是个AnimationDrawable动画定义,里面的item来指定每帧图片和显示时间(单位为毫秒),帧显示的顺序就是item定义的顺序。如果android:oneshot设置为true表明该动画只播放一次,否则该动画会循环播放。这些设置也可以通过AnimationDrawable提供的函数来设置。动画中引用的文件为QQ表情文件,包含在示例项目代码中。然后在layout中定义一个ImageView来显示上面定义的AnimationDrawable,layout代码如下(res\layout\main.xml):view plaincopy to clipboardprint?
  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="fill_parent"??
  6. ????android:layout_height="fill_parent"> ??
  7. ????<ImageView? ??
  8. ???????android:id="@+id/animation_view"??
  9. ???????android:layout_width="fill_parent"? ??
  10. ???????android:layout_height="wrap_content"??
  11. ???????android:src="@drawable/qq_animation"?/> ??
  12. ????<Button? ??
  13. ???????android:id="@+id/animation_btn"? ??
  14. ???????android:layout_width="fill_parent"??
  15. ???????android:layout_height="wrap_content"? ??
  16. ???????android:text="@string/start_animation"?/> ??
  17. ????<Button? ??
  18. ???????android:id="@+id/one_shot_btn"? ??
  19. ???????android:layout_width="fill_parent"??
  20. ???????android:layout_height="wrap_content"? ??
  21. ???????android:text="@string/play_once"?/> ??
  22. </LinearLayout>??
Java代码??[转]Animation自定义卡通片效果的实现
  1. <?xml?version="1.0"?encoding="utf-8"?>?<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"?android:orientation="vertical"?android:layout_width="fill_parent"?android:layout_height="fill_parent">?<ImageView?android:id="@+id/animation_view"?android:layout_width="fill_parent"?android:layout_height="wrap_content"?android:src="@drawable/qq_animation"?/>?<Button?android:id="@+id/animation_btn"?android:layout_width="fill_parent"?android:layout_height="wrap_content"?android:text="@string/start_animation"?/>?<Button?android:id="@+id/one_shot_btn"?android:layout_width="fill_parent"?android:layout_height="wrap_content"?android:text="@string/play_once"?/>?</LinearLayout>??
注意这里的ImageView 通过android:src="@drawable/qq_animation"引用了前面定义的AnimationDrawable,下面是两个按钮用来控制播放动画和设置AnimationDrawable的oneshot属性。
下面就是控制动画播放的类代码(src\org\goodev\animation\AnimActivity.java):
?view plaincopy to clipboardprint?
  1. public?class?AnimActivity?extends?Activity?{ ??
  2. ????/**?Called?when?the?activity?is?first?created.?*/??
  3. ????AnimationDrawable?mAd; ??
  4. ????Button?mPlayBtn; ??
  5. ????Button?mOneShotBtn; ??
  6. ????boolean?mIsOneShot; ??
  7. ? ??
  8. ????@Override??
  9. ????public?void?onCreate(Bundle?savedInstanceState)?{ ??
  10. ???????super.onCreate(savedInstanceState); ??
  11. ???????setContentView(R.layout.main); ??
  12. ? ??
  13. ???????ImageView?iv?=?(ImageView)?findViewById(R.id.animation_view); ??
  14. ???????mAd?=?(AnimationDrawable)?iv.getDrawable(); ??
  15. ? ??
  16. ???????mPlayBtn?=?(Button)?findViewById(R.id.animation_btn); ??
  17. ???????mPlayBtn.setOnClickListener(new?OnClickListener()?{ ??
  18. ???????????@Override??
  19. ???????????public?void?onClick(View?view)?{ ??
  20. ??????????????startAnimation(); ??
  21. ???????????} ??
  22. ???????}); ??
  23. ? ??
  24. ???????mOneShotBtn?=?(Button)?findViewById(R.id.one_shot_btn); ??
  25. ???????mOneShotBtn.setOnClickListener(new?OnClickListener()?{ ??
  26. ???????????@Override??
  27. ???????????public?void?onClick(View?view)?{ ??
  28. ??????????????if?(mIsOneShot)?{ ??
  29. ??????????????????mOneShotBtn.setText("Play?Once"); ??
  30. ??????????????}?else?{ ??
  31. ??????????????????mOneShotBtn.setText("Play?Repeatly"); ??
  32. ??????????????} ??
  33. ??????????????mAd.setOneShot(!mIsOneShot); ??
  34. ??????????????mIsOneShot?=?!mIsOneShot; ??
  35. ???????????} ??
  36. ???????}); ??
  37. ? ??
  38. ????} ??
  39. ? ??
  40. ????/**??
  41. ?????*?通过AnimationDrawable的start函数播放动画,??
  42. ?????*?stop函数停止动画播放,??
  43. ?????*?isRunning来判断动画是否正在播放。??
  44. ?????*/??
  45. ????public?void?startAnimation()?{ ??
  46. ???????if?(mAd.isRunning())?{ ??
  47. ???????????mAd.stop(); ??
  48. ???????}?else?{ ??
  49. ???????????mAd.stop(); ??
  50. ???????????mAd.start(); ??
  51. ???????} ??
  52. ????} ??
  53. } ??
  54. ???
Java代码??[转]Animation自定义卡通片效果的实现
  1. public?class?AnimActivity?extends?Activity?{?/**?Called?when?the?activity?is?first?created.?*/?AnimationDrawable?mAd;?Button?mPlayBtn;?Button?mOneShotBtn;?boolean?mIsOneShot;?@Override?public?void?onCreate(Bundle?savedInstanceState)?{?super.onCreate(savedInstanceState);?setContentView(R.layout.main);?ImageView?iv?=?(ImageView)?findViewById(R.id.animation_view);?mAd?=?(AnimationDrawable)?iv.getDrawable();?mPlayBtn?=?(Button)?findViewById(R.id.animation_btn);?mPlayBtn.setOnClickListener(new?OnClickListener()?{?@Override?public?void?onClick(View?view)?{?startAnimation();?}?});?mOneShotBtn?=?(Button)?findViewById(R.id.one_shot_btn);?mOneShotBtn.setOnClickListener(new?OnClickListener()?{?@Override?public?void?onClick(View?view)?{?if?(mIsOneShot)?{?mOneShotBtn.setText("Play?Once");?}?else?{?mOneShotBtn.setText("Play?Repeatly");?}?mAd.setOneShot(!mIsOneShot);?mIsOneShot?=?!mIsOneShot;?}?});?}?/**?*?通过AnimationDrawable的start函数播放动画,?*?stop函数停止动画播放,?*?isRunning来判断动画是否正在播放。?*/?public?void?startAnimation()?{?if?(mAd.isRunning())?{?mAd.stop();?}?else?{?mAd.stop();?mAd.start();?}?}?}??
?布局动画介绍?布局动画和逐帧动画是由本质的不同的,逐帧动画是一帧帧图片组成的,而布局动画是渐变动画,OPhone通过改变UI的属性(大小、位置、透明度等)来实现动画效果。?在OPhone显示系统中,每个view都对应一个矩阵来控制该view显示的位置,通过不同的方式来改变该控制矩阵就可以实现动画效果,例如旋转、移动、缩放等。?不同的矩阵变换有不同的类来实现,android.view.animation.Animation类代表所有动画变换的基类,目前在OPhone系统中有如下五个实现(都位于android.view.animation包中):
读书人网 >移动开发

热点推荐