美文网首页
动画的使用

动画的使用

作者: hurtsogood | 来源:发表于2018-09-13 20:11 被阅读0次

1 动画的分类

属性动画 视图动画:a:补间动画 (只能够作用在视图View上,即只可以对一个Button、TextView、甚至是LinearLayout、或者其它继承自View的组件进行动画操作,但无法对非View的对象进行动画操作)b:逐帧动画

属性动画:可以对view的某个属性进行控制,比如对象的颜色等而不是对整个对象,

2 属性动画两个重要的类:

ValueAnimator 类(通过不断控制 值 的变化,再不断 手动 赋给对象的属性,从而实现动画效果,是间接对属性进行操作) 

ValueAnimator.ofInt(int values)自带估值器

ValueAnimator.ofFloat(float values)

ValueAnimator.ofObject(int values)需要自己定义估值

任意举个例子:

ValueAnimator anim = ValueAnimator.ofInt(0,3);alueAnimator.ofInt()内置了整型估值器,直接采用默认的.不需要设置,即默认设置了如何从初始值 过渡到 结束值

anim.setDuration(500);

anim.setStartDelay(500);

anim.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {

@Override

publicvoidonAnimationUpdate(ValueAnimator animation){

intcurrentValue = (Integer) animation.getAnimatedValue();

View.setproperty(currentValue);//将改变后的值赋给对象的属性值

View.requestLayout();//刷新视图,即重新绘制,从而实现动画效果

} });

 anim.start();// 启动动画


ObjectAnimator 类(通过不断控制 值 的变化,再不断 自动 赋给对象的属性,从而实现动画效果,是直接对属性进行操作)

ObjectAnimator animator = ObjectAnimator.ofFloat(Objectobject,Stringproperty, float ....values);

/ /Object object:需要操作的对象// String property:需要操作的对象的属性// float ....values:动画初始值 & 结束值(不固定长度)

mButton = (Button) findViewById(R.id.Button);// 创建动画作用对象:此处以Button为例ObjectAnimator animator = ObjectAnimator.ofFloat(mButton,"rotation",0f,360f);// 表示的是:// 动画作用对象是mButton// 动画作用的对象的属性是旋转alpha// 动画效果是:0 - 360animator.setDuration(5000); animator.start();

3估值器的原理https://www.jianshu.com/p/2412d00a0ce4

public class FloatEvaluator implements TypeEvaluator{// FloatEvaluator实现了TypeEvaluator接口// 重写evaluate()

public Objecte valuate(floatfraction, Object startValue, Object endValue){// 参数说明// fraction:表示动画完成度(根据它来计算当前动画的值)// startValue、endValue:动画的初始值和结束值

float startFloat = ((Number) startValue).floatValue();

return startFloat + fraction * (((Number) endValue).floatValue() - startFloat);// 初始值 过渡 到结束值 的算法是:// 1. 用结束值减去初始值,算出它们之间的差值// 2. 用上述差值乘以fraction系数// 3. 再加上初始值,就得到当前动画的值} }

1if (interpolatedTime ==1) {

view.setVisibility(View.GONE);

        }else {

view.getLayoutParams().height =measuredHeight - (int) (measuredHeight * interpolatedTime);

            view.requestLayout();

        }

}

@Override

    public boolean willChangeBounds() {

return true;

    }

};

ps: 以前只是纸上谈兵 从来没有自己写过动画,这次的项目要求自己写动画,自己实战了一回,有一些心得,写着总结一下,对自己也是很好的总结

相关文章

网友评论

      本文标题:动画的使用

      本文链接:https://www.haomeiwen.com/subject/thnhqftx.html