3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation.
记一下学习property animation的笔记. 其实前面的也不太熟,只是在学着做仿Path的Menu的时候用到过.
最让人蛋疼的就是,property animation之前的动画,类似移动的是镜像.那Path举例,刚开始几个按钮定义为位置应该是和Menu键(那个加号)一样的起始位置,当他们弹开后,animation 将他们移动到了相应的位置,icon的实际位置还在远处,这样你就不能对个icon进行点击等操作了.
Tween Animation:补间动画,给出两个关键帧,通过一些算法将给定属性值在给定的时间内在两个关键帧间渐变。
Frame Animation:帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果。 (Title栏 上的 下载图标)
在动画start()之前要先stop(),不然在第一次动画之后会停在最后一帧,这样动画就只会触发一次。
这点很重要:SDK中提到的,不要在onCreate中调用start,因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start()。
http://blog.csdn.net/ssstudio/article/details/6228911
http://www.open-open.com/lib/view/open1329994048671.html
所有,其实这个才是我们想要的吧.
animation = ValueAnimator.ofFloat(0f , 1.0f); animation.setDuration(1000); animation.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { button01.setAlpha((Float) animation.getAnimatedValue()); } });
//button01 操作的对象. //对象的属性名(String) animation = ObjectAnimator.ofFloat(button01, "Alpha", 0f , 1.0f); animation.setDuration(1000);
onAnimationUpdate()
回调函数中
调用View.invalidate()方法来刷新屏幕的显示。比如说,设置Drawable对象的color属性。但是,View中的所有setter方法,如setAlpha()
andsetTranslationX()会自动地调用invalidate()方法,因此不需要额外地调用invalidat
可以看到ObjectAnimator是直接对对象进行操作,但是需要对象提供相应的 Seter/Geter 方法.而ValueAnimator需要注册一个AnimatorUpdateListener.根据getAnimatedValue 自由的进行操作.
mAnimationSet = new AnimatorSet(); mAnimationSet.play(fadeOut).before(fadeIn); mAnimationSet.play(fadeIn).with(ScaleX); mAnimationSet.play(ScaleY).after(ScaleX);
执行顺序:fadeOut > fadeIn && ScaleX > ScaleY. 刚才在测试的时候发现不能使用CycleInterpolator 进行循环.
一个按钮横向移动到父类容器的右侧的示例:
@Override public void onWindowFocusChanged(boolean hasFocus) { Log.i(TAG, "button01.getRight():" +button01.getRight()); //获取button01父类宽度. View view = (View)button01.getParent(); Log.i(TAG, "view.getRotationY():" +view.getRight()); ObjectAnimator right = ObjectAnimator.ofFloat(button01, "x", 0 , view.getRight() - button01.getRight()); right.setDuration(1000); //先往变化的相反方向回退,然后甩到尾值并越过尾值,再返回到尾值。 right.setInterpolator(new AnticipateOvershootInterpolator()); mAnimationSet.play(right); super.onWindowFocusChanged(hasFocus); }