Android属性动画实战(一)

来源:互联网 发布:windows 10家庭版关闭 编辑:程序博客网 时间:2024/05/17 15:58

属性动画,就是通过改变对象的属性产生动画
先看看效果
这里写图片描述

对象的属性有以下几种:

1. Duration动画的持续时间,默认300ms。2. Time interpolation:时间差值,乍一看不知道是什么,但是我说LinearInterpolator、AccelerateDecelerateInterpolator,大家一定知道是干嘛的   了,定义动画的变化率。3. Repeat count and behavior:重复次数、以及重复模式;可以定义重复多少次;重复时从头开始,还是反向。4. Animator sets: 动画集合,你可以定义一组动画,一起执行或者顺序执行。5. Frame refresh delay:帧刷新延迟,对于你的动画,多久刷新一次帧;默认为10ms,但最终依赖系统的当前状态;基本不用管。

操作对象属性的相关的类有:
1. ObjectAnimator 动画的执行类,后面详细介绍
2. ValueAnimator 动画的执行类,后面详细介绍
3. AnimatorSet 用于控制一组动画的执行:线性,一起,每个动画的先后执行等。
4. AnimatorInflater 用户加载属性动画的xml文件
5. TypeEvaluator 类型估值,主要用于设置动画操作属性的值。
6. TimeInterpolator 时间插值,上面已经介绍。

一)ObjectAnimator ,这个比较简单,但是有几点必须条件:
1,有对象
2,对象有get…() set…( )方法

    /**     * setRotationX() 这是View中的方法     * Sets the degrees that the view is rotated around the horizontal axis through the pivot point     * 意思是让View沿着水平防线旋转     * <p/>     * 动画更新中,会不断的调用setRotationX()这个方法,所以必须得有setRotationX() 和getRotationX()方法     */    private void tranXz() {        ObjectAnimator animal = ObjectAnimator                .ofFloat(ivIcon, "rotationX", 0.0f, 360.f)                .setDuration(300);        animal.start();    }
    /**     * 随便设置一个方法jh,虽然看不出来有任何动画,但是确实有动画值了500毫秒     * addUpdateListener这个是监听动画执行的过程     * <p/>     * animatedValue 这个值是从1到0不断的不规律缩减的     * 04-12 23:02:38.773 14675-14675/com.animationexample I/System.out: value====1.0     * 04-12 23:02:38.790 14675-14675/com.animationexample I/System.out: value====0.9971504     * 04-12 23:02:38.808 14675-14675/com.animationexample I/System.out: value====0.9892905     * .............     * 04-12 23:02:39.198 14675-14675/com.animationexample I/System.out: value====0.06646466     * 04-12 23:02:39.224 14675-14675/com.animationexample I/System.out: value====0.02447176     * 04-12 23:02:39.252 14675-14675/com.animationexample I/System.out: value====0.010709524     * 04-12 23:02:39.282 14675-14675/com.animationexample I/System.out: value====0.0     */    private void tansDrop() {        ObjectAnimator dropAnimal = ObjectAnimator                .ofFloat(ivIcon, "jh", 1.0f, 0.0f)                .setDuration(500);        dropAnimal.start();        dropAnimal.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator animation) {                float value = (float) animation.getAnimatedValue();//                System.out.println("value====" + value);//              淡出并且缩小                ivIcon.setAlpha(value);                ivIcon.setScaleX(value);                ivIcon.setScaleY(value);            }        });    }

二)ValueAnimator 它只需要设定好初始值和结束值,然后在监听中对对象进行操作就ok了

    /**     * 第一种方法:直接操作属性设置View的translationY属性     * 第二种方法:是自己通过监听来改变View的属性,更加灵活     */    private void tansBelow() {/*        ObjectAnimator dropAnimal = ObjectAnimator                .ofFloat(ivIcon2, "translationY", 0, StringUtil.getScreenHeight() - ivIcon2.getHeight())                .setDuration(500);        dropAnimal.start();*/        //        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, StringUtil.getScreenHeight() - ivIcon2.getHeight())                .setDuration(500);        valueAnimator.start();        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator animation) {                ivIcon2.setTranslationY((Float) animation.getAnimatedValue());            }        });    }
    private void tranShe() {        //先设置好起点和终点        PointF startPoint = new PointF(ivIcon3.getX(), ivIcon3.getY());        PointF endPoint = new PointF(ivIcon3.getX()-300,ivIcon3.getY()-300);        ValueAnimator sheAnimator = ValueAnimator.ofObject(new MyPointEvaluator(), startPoint, endPoint);        sheAnimator.setDuration(500);        sheAnimator.start();        sheAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator animation) {                PointF pontf = (PointF) animation.getAnimatedValue();                System.out.println(pontf.x);                ivIcon3.setX(pontf.x);                ivIcon3.setY(pontf.y);            }        });    }    //Evaluator动态设置属性值    public class MyPointEvaluator implements TypeEvaluator<PointF> {        @Override // 实现TypeEvaluator接口的evaluate()方式,fraction是动画的进度,从0.0到1.0,根据进度,初始值和最终值,给出动画过程的中间值。        public PointF evaluate(float fraction, PointF startValue, PointF endValue) {            PointF point = new PointF();            point.x = startValue.x + fraction * (endValue.x - startValue.x);            point.y = startValue.y + fraction * (endValue.y - startValue.y);            return point;        }    }
0 0
原创粉丝点击