本文共 5593 字,大约阅读时间需要 18 分钟。
1.启动页,开始按钮,飞入效果可以用.
2.listview或者recycleview的item进入动画.由于补间动画的只有四种变化的局限性,引入了属性动画.
字面上来说, 属性动画就是通过改变view的属性(比如高度,宽度,坐标等等),达到目的.so...只要你view有的属性,都能改变.无所不能. 原理是但是,当自定义view需要 用属性动画时候,必须要有get和set方法
改变一个对象的动画属性
ObjectAnimator.ofFloat(button,"rotationX",0,360).start(),
改变一个对象的色值,可以改变对象的背景,textcolor等等
//ValueAnimator valueAnimator=ObjectAnimator.ofInt(button,"textColor",0xfff8080,0xff8080ff); ValueAnimator valueAnimator=ObjectAnimator.ofInt(button,"backgroundColor",0xfff8080,0xff8080ff); valueAnimator.setDuration(5000).setEvaluator(new ArgbEvaluator()); valueAnimator.setRepeatCount(ValueAnimator.INFINITE); valueAnimator.setRepeatMode(ValueAnimator.REVERSE); valueAnimator.start();
动画集合, 可以同时开始,也可以顺序开始
AnimatorSet animatorSet =new AnimatorSet(); animatorSet.playTogether( ObjectAnimator.ofFloat(button,"rotationX",0,360), ObjectAnimator.ofFloat(button,"translationX",0,90), ObjectAnimator.ofFloat(button,"scaleX",1,1.5f), ObjectAnimator.ofFloat(button,"alpha",1,0.2f,1) ); animatorSet.setDuration(5000).start();
根据事件流逝的百分比 计算 当前属性改变的百分比.举个例子,就是根据你设置的Durantion流逝的百分比,来改变位移(tanslate)的速度.
实现非线性运动的动画效果
非线性运动:动画改变的速率不是一成不变的,如加速 & 减速运动都属于非线性运动
目前,Android中已经有了几种插值器,如下:
一个父接口,所有Interpolator
都应该实现它.其中方法中的 input 值 是百分比, 也就是取值在0~1之间.
public interface TimeInterpolator { /** * Maps a value representing the elapsed fraction of an animation to a value that represents * the interpolated fraction. This interpolated value is then multiplied by the change in * value of an animation to derive the animated value at the current elapsed animation time. * * @param input A value between 0 and 1.0 indicating our current point * in the animation where 0 represents the start and 1.0 represents * the end * @return The interpolation value. This value can be more than 1.0 for * interpolators which overshoot their targets, or less than 0 for * interpolators that undershoot their targets. */ float getInterpolation(float input);}
设置 属性值 从初始值过渡到结束值 的变化具体数值
插值器(Interpolator)决定 值 的变化规律(匀速、加速blabla),即决定的是变化趋势;
而具体变化数值则交给估值器evaluator.fraction:动画完成度,插值器getInterpolation()的返回值(input的值决定了fraction的值),代表时间流逝的百分比
startValue:动画的初始值
endValue:动画的结束值
public interface TypeEvaluator{ /** * This function returns the result of linearly interpolating the start and end values, with * fraction
representing the proportion between the start and end values. The * calculation is a simple parametric calculation:result = x0 + t * (x1 - x0)
, * wherex0
isstartValue
,x1
isendValue
, * andt
isfraction
. * * @param fraction The fraction from the starting to the ending values * @param startValue The start value. * @param endValue The end value. * @return A linear interpolation between the start and end values, given the *fraction
parameter. */ public T evaluate(float fraction, T startValue, T endValue);}
常见的实现类
input的值决定了fraction的值:input值经过计算后传入到插值器的getInterpolation(),然后通过实现getInterpolation()中的逻辑算法,根据input值来计算出一个返回值,而这个返回值就是fraction了} }
首先这是给ImageView一个属性动画ObjectAnimator
.
修改动画的加载速度和弹性效果--用 差值器BounceInterpolator
小球的运动轨迹,可以用自定义viewondraw()
实现,也可以用自定义估值器MyEvaluator
实现.
运动轨迹,是抛物线,所有用类似y^2=x
,即可实现.可用估值器,动态修改view的x,y的坐标即可;
在public void onAnimationUpdate(ValueAnimator animation)
中,拿到MyEvaluator
修改过的x,y的坐标,然后将imageview的坐标修改即可.
代码如下:
public class MyEvaluator implements TypeEvaluator{ @Override public Point evaluate(float fraction, Point startValue, Point endValue) { Point point = new Point(); //y^2=x;(x>0) 就是抛物线~这里100是为了扩大像素的位移量. point.x=startValue.x+fraction*(endValue.x-startValue.x); point.y=startValue.y+(fraction*(endValue.y-startValue.y))*fraction*fraction; return point; }}
public void paowuxian(final View v) { int animHeigh=getWindowManager().getDefaultDisplay().getHeight();//得到屏幕宽高 int animWidth=getWindowManager().getDefaultDisplay().getWidth(); ValueAnimator objectAnimator = ObjectAnimator.ofObject(new MyEvaluator(), new Point(0, 0),new Point(animWidth-400, animHeigh-400));//加载自定义的估值器,起始点坐标. objectAnimator.setDuration(5000); objectAnimator.setInterpolator(new BounceInterpolator());//设置差值器效果 objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {//设置帧动画 监听. @Override public void onAnimationUpdate(ValueAnimator animation) { Point point = (Point) animation.getAnimatedValue(); mBlueBall.setX(point.x);//动态修改坐标 mBlueBall.setY(point.y); } }); objectAnimator.start(); }
转载地址:http://nmtpo.baihongyu.com/