当前位置:K88软件开发文章中心编程语言APP编程Android01 → 文章内容

8.4.3 Android动画合集之属性动画-初见

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-12 6:27:45

@Override public void onAnimationUpdate(ValueAnimator animation) { // 轨迹方程 x = width / 2 int y = (Integer) animation.getAnimatedValue(); int x = width / 2; moveView(img_babi, x, y); } }); xValue.setInterpolator(new LinearInterpolator()); xValue.start(); } //缩放效果 private void scaleAnimator(){ //这里故意用两个是想让大家体会下组合动画怎么用而已~ final float scale = 0.5f; AnimatorSet scaleSet = new AnimatorSet(); ValueAnimator valueAnimatorSmall = ValueAnimator.ofFloat(1.0f, scale); valueAnimatorSmall.setDuration(500); ValueAnimator valueAnimatorLarge = ValueAnimator.ofFloat(scale, 1.0f); valueAnimatorLarge.setDuration(500); valueAnimatorSmall.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float scale = (Float) animation.getAnimatedValue(); img_babi.setScaleX(scale); img_babi.setScaleY(scale); } }); valueAnimatorLarge.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float scale = (Float) animation.getAnimatedValue(); img_babi.setScaleX(scale); img_babi.setScaleY(scale); } }); scaleSet.play(valueAnimatorLarge).after(valueAnimatorSmall); scaleSet.start(); //其实可以一个就搞定的// ValueAnimator vValue = ValueAnimator.ofFloat(1.0f, 0.6f, 1.2f, 1.0f, 0.6f, 1.2f, 1.0f);// vValue.setDuration(1000L);// vValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {// @Override// public void onAnimationUpdate(ValueAnimator animation) {// float scale = (Float) animation.getAnimatedValue();// img_babi.setScaleX(scale);// img_babi.setScaleY(scale);// }// });// vValue.setInterpolator(new LinearInterpolator());// vValue.start(); } //旋转的同时透明度变化 private void raAnimator(){ ValueAnimator rValue = ValueAnimator.ofInt(0, 360); rValue.setDuration(1000L); rValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int rotateValue = (Integer) animation.getAnimatedValue(); img_babi.setRotation(rotateValue); float fractionValue = animation.getAnimatedFraction(); img_babi.setAlpha(fractionValue); } }); rValue.setInterpolator(new DecelerateInterpolator()); rValue.start(); } //圆形旋转 protected void circleAnimator() { width = ly_root.getWidth(); height = ly_root.getHeight(); final int R = width / 4; ValueAnimator tValue = ValueAnimator.ofFloat(0, (float) (2.0f * Math.PI)); tValue.setDuration(1000); tValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { // 圆的参数方程 x = R * sin(t) y = R * cos(t) float t = (Float) animation.getAnimatedValue(); int x = (int) (R * Math.sin(t) + width / 2); int y = (int) (R * Math.cos(t) + height / 2); moveView(img_babi, x, y); } }); tValue.setInterpolator(new DecelerateInterpolator()); tValue.start(); }}好的,使用的流程非常简单,先创建ValueAnimator对象,调用ValueAnimator.ofInt/ofFloat获得,然后设置动画持续时间,addUpdateListener添加AnimatorUpdateListener事件监听,然后使用参数animation的getAnimatedValue()获得当前的值,然后我们可以拿着这个值来修改View的一些属性,从而形成所谓的动画效果,接着设置setInterpolator动画渲染模式,最后调用start()开始动画的播放~卧槽,直线方程,圆的参数方程,我都开始方了,这不是高数的东西么,挂科学渣连三角函数都忘了...例子参考自github:MoveViewValueAnimator3.ObjectAnimator简单使用比起ValueAnimator,ObjectAnimator显得更为易用,通过该类我们可以直接对任意对象的任意属性进行动画操作!没错,是任意对象,而不单单只是View对象,不断地对对象中的某个属性值进行赋值,然后根据对象属性值的改变再来决定如何展现出来!比如为TextView设置如下动画:ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f);这里就是不断改变alpha的值,从1f - 0f,然后对象根据属性值的变化来刷新界面显示,从而展现出淡入淡出的效果,而在TextView类中并没有alpha这个属性,ObjectAnimator内部机制是:寻找传输的属性名对应的get和set方法~,而非找这个属性值!不信的话你可以到TextView的源码里找找是否有alpha这个属性!好的,下面我们利用ObjectAnimator来实现四种补间动画的效果吧~运行效果图:代码实现:布局直接用的上面那个布局,加了个按钮,把ImageView换成了TextView,这里就不贴代码了,直接上MainAc

上一页  [1] [2] [3] [4]  下一页


8.4.3 Android动画合集之属性动画-初见