属性动画的使用
2017-05-04 19:03
134 查看
Android提供了两个类来实现属性动画:ObjectAnimator和ValueAnimator
ObjectAnimator
1.单个动画效果:
提供了ofInt、ofFloat、ofObject,这几个方法都是设置动画作用的元素、作用的属性、动画开始、结束、以及中间的任意个属性值。
当对于属性值,只设置一个的时候,会认为当然对象该属性的值为开始(getPropName反射获取),然后设置的值为终点。如果设置两个,则一个为开始、一个为结束~~~
2.多种动画效果重叠
ValueAnimation
valueAnimation比ObjectAnimation使用方法类似
动画时间的监听
如果只要监听一种事件可以创建AnimatorListener的适配器对象AnimatorListenerAdapter
valueAnimator还有cancel()和end()方法:cancel动画立即停止,停在当前的位置;end动画直接到最终状态。
AnimatorSet
同时执行动画
按顺序执行动画
ObjectAnimator
1.单个动画效果:
ObjectAnimator .ofFloat(view,"rotation",0.0F,180F) .setDuration(500) .start(); //rotationX 绕x轴旋转 //rotation 绕中心旋转
提供了ofInt、ofFloat、ofObject,这几个方法都是设置动画作用的元素、作用的属性、动画开始、结束、以及中间的任意个属性值。
当对于属性值,只设置一个的时候,会认为当然对象该属性的值为开始(getPropName反射获取),然后设置的值为终点。如果设置两个,则一个为开始、一个为结束~~~
2.多种动画效果重叠
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f); PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f, 0, 1f); PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f, 0, 1f); ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY,pvhZ).setDuration(1000).start();
ValueAnimation
valueAnimation比ObjectAnimation使用方法类似
int height = getWindowManager().getDefaultDisplay().getHeight(); int view_height = view.getHeight(); ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, height/2 - view_height); valueAnimator .setDuration(500) .setTarget(view); valueAnimator.start(); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { view.setTranslationY((Float) animation.getAnimatedValue()); } });
动画时间的监听
valueAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { //开始 } @Override public void onAnimationEnd(Animator animation) { //结束 } @Override public void onAnimationCancel(Animator animation) { // 取消 } @Override public void onAnimationRepeat(Animator animation) { //重复 } });
如果只要监听一种事件可以创建AnimatorListener的适配器对象AnimatorListenerAdapter
valueAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); } });
valueAnimator还有cancel()和end()方法:cancel动画立即停止,停在当前的位置;end动画直接到最终状态。
AnimatorSet
同时执行动画
ObjectAnimator anim1 = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 2f); ObjectAnimator anim2 = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, 2f); AnimatorSet animSet = new AnimatorSet(); animSet.setDuration(2000); animSet.setInterpolator(new LinearInterpolator()); //两个动画同时执行 animSet.playTogether(anim1, anim2); animSet.start();
按顺序执行动画
float cx = view.getX(); ObjectAnimator anim1 = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 2f); ObjectAnimator anim2 = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, 2f); ObjectAnimator anim3 = ObjectAnimator.ofFloat(view, "x", cx , 0f); ObjectAnimator anim4 = ObjectAnimator.ofFloat(view, "x", cx); /** * anim1,anim2,anim3同时执行 * anim4接着执行 */ AnimatorSet animSet = new AnimatorSet(); animSet.play(anim1).with(anim2); animSet.play(anim2).with(anim3); animSet.play(anim4).after(anim3); animSet.setD 4000 uration(1000); animSet.start();
相关文章推荐
- 从BouncingBall.java中学习使用属性动画记录
- IOS学习笔记 CALayer & 核心动画 CALayer基本属性的使用(1)
- Android为ViewPager增加切换动画——使用属性动画
- Android属性动画-Property Animation(二) 使用ObjectAnimator完成动画
- Android至ViewPager添加切换动画——使用属性动画
- WPF 触发器(根据属性使用动画)
- 讲述属性动画的使用 —使用动画旋转、平移、渐变和缩放
- Android 使用属性动画实现可折叠布局
- CABasicAnimation(基础属性动画)的简单使用
- android动画之android:interpolator属性使用
- Android使用属性动画ValueAnimator动态改变SurfaceView的背景颜色
- Android中属性动画Property Animation使用示例(二)
- Android中属性动画Property Animation使用示例(三)
- android 属性动画 AnimatorSet的使用
- CALayer的可动画属性和使用CALayer的代理来绘图
- CALayer的可动画属性和使用CALayer的代理来绘图
- Android中属性动画Property Animation使用示例(三)
- Android中属性动画Property Animation使用示例(一)
- Android属性动画--基础使用
- Android至ViewPager添加切换动画——使用属性动画