《android开发艺术探索》读书笔记--动画

男儿欲遂平生志,六经勤向窗前读。这篇文章主要讲述《android开发艺术探索》读书笔记--动画相关的知识,希望能为你提供帮助。
接上篇《android开发艺术探索》读书笔记(六)--Drawable
【《android开发艺术探索》读书笔记--动画】No1:
自定义动画:派生一种新动画只需要继承Animation这个抽象类,然后重写它的initialize和applyTransformation方法,在initialize方法中做一些初始化工作,在applyTransformation中进行相应的矩阵变换即可,很多时候需要采用Camera来简化矩阵变换的过程。
No2:
属性动画PropertyAnimation   补间动画TweenAnimation   帧动画FrameAnimation No3:                                                                                                                                                                                                                                                                                        LayoutAnimation:LayoutAnimation作用于ViewGroup,为ViewGroup指定一个动画,这样当它的子元素出场时都会具有这种动画效果。这种效果常常被用在ListView上。

//res/anim/anim_layout.xml < layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:animation="@anim/anim_item" android:animationOrder="normal" android:delay="0.5" />

//res/anim/anim_item.xml < ?xml version="1.0" encoding="utf-8"?> < set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true"> < alpha android:fromAlpha="0.0" android:toAlpha="1.0" /> < translate android:fromXDelta="1000" android:toXDelta="0" /> < /set>

< ?xml version="1.0" encoding="utf-8"?> < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.layoutanimation.MainActivity"> < ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff4f7f9" android:cacheColorHint="#00000000" android:divider="#dddbdb" android:dividerHeight="1.0px" android:layoutAnimation="@anim/anim_layout" android:listSelector="@android:color/transparent" /> < /RelativeLayout>

  还可通过LayoutAnimationController来实现
ListView listView = (ListView) findViewById(R.id.list); Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_item); LayoutAnimationController controller = new LayoutAnimationController(animation); controller.setDelay(0.5f); controller.setOrder(LayoutAnimationController.ORDER_NORMAL); listView.setLayoutAnimation(controller);

No4:
Activity切换效果,主要用到overridePendingTransition(int enterAnim,int exitAnim),此方法必须在startActivity(Intent)或者finish()之后调用才能生效
enterAnim--Activity被打开时,所需的动画资源id
exitAnim--Activity被暂停时,所需的动画资源id
Fragment切换动画,可以通过FragmentTransaction中的setCustomAnimations()方法来实现。切换动画需要是View动画
No5:
属性动画可以对任意对象的属性进行动画而不仅仅是View,可以采用开源动画库nineoldandroids来兼容以前的版本
No6:
TimeInterpolator时间插值器,它的作用是根据时间流逝的百分比来计算出当前属性值改变的百分比。
TypeEvaluator类型估值算法(估值器),它的作用是根据当前属性改变的百分比来计算改变后的属性值。
自定义插值器需要实现Interpolator或者TimeInterpolator,自定义估值算法需要实现TypeEvaluator。
No7: AnimatorListener可以监听动画的开始、结束、取消和重复播放。 AnimatorUpdateListener可以监听整个过程,动画每播放一帧,onAnimationUpdate就会被调用一次。 No8: 我们对object的属性abc做动画,如果想让动画生效,需要同时满足两个条件 1)object必须要提供setAbc方法,如果动画的时候没有传递初始值,那么还要提供getAbc方法,因为系统要去取abc属性的初始值(如果这条不满足,程序直接Crash) 2)object的setAbc对属性abc所做的改变必须能够通过某种方式反映出来,比如会带来UI的改变之类的(如果这条不满足,动画无效果但不会Crash) 解决不满足条件二的方法:
private void performAnimate(){ ViewWrapper wrapper = new ViewWrapper(mButton); ObjectAnimator.ofInt(wrapper,"width"500).setDuration(5000).start(); }@Override public void onClick(View v){ if(v == mButton){ performAnimate(); } }private static class ViewWrapper{ private View mTarget; public ViewWrapper(View target){ mTarget = target; }public void setWidth(int width){ mTarget.getLayoutParams().width = width; mTarget.requestLayout(); } }

No9:
ValueAnimator监听动画过程,本身不作用于任何对象。它可以对一个值做动画,然后我们可以监听其动画过程,在动画过程中修改我们的对象的属性值,这样也就相当于我们的对象做了动画。
No10:
使用动画注意事项:
1)OOM--帧动画图片数量较多且图片较大时就极易出现OOM
2)内存泄露--无限循环属性动画需要在Activity退出时及时停止,否则导致Activity无法释放从而造成内存泄露,View动画不存在此问题
3)兼容--动画在3.0以下系统有兼容性问题
4)需要使用px--尽量别使用dp
5)动画元素交互--属性动画和View动画的单击事件触发位置
6)硬件加速--硬件加速会提高动画的流畅性

    推荐阅读