TypeEvaluator简介

Android提供了以下几个简单的Evalutor实现类:
  • IntEvaluator:属性的值类型为int
  • FloatEvaluator:属性的值类型为float
  • ArgbEvaluator:属性的值类型为十六进制颜色值
TypeEvaluator:一个接口,可以通过实现该接口自定义Evaluator。

TypeEvaluator的实现过程:
首先根据动画【已进行的时间】跟动画【总时间】的比计算出一个时间因子(0~1,即evalute()中的fraction参数,这个值是自动计算得到的)
然后根据【TimeInterpolator】得到出另一个算法因子
TypeEvaluator通过这【两个】因子计算出属性值并将其【传给】我们定义的泛型对象
最后在我们监听动画的绘制过程中,将泛型对象中保存的值【取出】并用于【更改】目标对象的属性

自定义TypeEvaluator时传入的泛型可以根据自己的需求,自己设计个Bean。
只要evaluate中的函数能够满足:当fraction=0时返回值为startValue,并且当fraction=1时返回值为endValue,就是一个比较合理的函数。

TypeEvaluator 接口源码

/**
* Interface for use with the ValueAnimator#setEvaluator(TypeEvaluator) function. Evaluators
* allow developers to create animations on arbitrary任意的 property types, by allowing them to supply
* custom evaluators for types that are not automatically understood and used by the animation system.
*/
public interface TypeEvaluator<T> {
/**
* 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), where "x0" is startValue, "x1" is endValue, and "t" is fraction.
* @param fraction The fraction from the starting to the ending values
* @return A linear interpolation between the start and end values, given the fraction parameter.
*/
public T evaluate(float fraction, T startValue, T endValue);
}

IntEvaluator 源码

/** This evaluator can be used to perform type interpolation between int values. */
public class IntEvaluator implements TypeEvaluator<Integer> {
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
return (int)(startValue + fraction * (endValue - startValue));
}
}

FloatEvaluator 源码

public Float evaluate(float fraction, Number startValue, Number endValue) {
return startValue.floatValue() + fraction * (endValue.floatValue() - startValue.floatValue());
}

ArgbEvaluator 源码

/** integer values that represent ARGB colors. */
public class ArgbEvaluator implements TypeEvaluator {
/**
* This function returns the calculated in-between value for a color given integers that represent
* the start and end values in the four bytes of the 32-bit int. Each channel is separately单独的
* linearly interpolated and the resulting calculated values are recombined into the return value.
* @param fraction The fraction from the starting to the ending values
* @param startValue A 32-bit int value representing colors in the separate bytes of the parameter
* @param endValue A 32-bit int value representing colors in the separate bytes of the parameter
* @return A value that is calculated to be the linearly interpolated result, derived by separating
* the start and end values into separate color channels and interpolating each one separately,
* recombining the resulting values in the same way.
*/
public Object evaluate(float fraction, Object startValue, Object endValue) {
int startInt = (Integer) startValue;
int startA = (startInt >> 24) & 0xff;
int startR = (startInt >> 16) & 0xff;
int startG = (startInt >> 8) & 0xff;
int startB = startInt & 0xff;

int endInt = (Integer) endValue;
int endA = (endInt >> 24) & 0xff;
int endR = (endInt >> 16) & 0xff;
int endG = (endInt >> 8) & 0xff;
int endB = endInt & 0xff;

return (int)((startA + (int)(fraction * (endA - startA))) << 24) |
(int)((startR + (int)(fraction * (endR - startR))) << 16) |
(int)((startG + (int)(fraction * (endG - startG))) << 8) |
(int)((startB + (int)(fraction * (endB - startB))));
}
}

自定义TypeEvaluator实现抛物线效果

自定义TypeEvaluator时传入的泛型可以根据自己的需求,自己设计个Bean。
为了以不同的函数分别设置x和y的位置,我们自定义的TypeEvaluator传入的泛型为PointF对象,每次根据当前时间返回一个PointF对象,PointF中的x、y代表View当前的位置,然后在监听动画绘制过程时更新UI。

自定义TypeEvaluator

public class PointFEvaluator implements TypeEvaluator<PointF> {
    @Override
    public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
        //只要能保证:当fraction=0时返回值为startValue,并且当fraction=1时返回值为endValue,就是一个比较合理的函数
        PointF pointF = new PointF();
        pointF.x = startValue.x + fraction * (endValue.x - startValue.x);// x方向匀速移动
        pointF.y = startValue.y + fraction * fraction * (endValue.y - startValue.y);// y方向抛物线加速移动
        return pointF;
    }

}

在Activity中的使用

// 自定义TypeEvaluator实现抛物线动画效果
public class TypeEvaluatorActivity extends Activity {
    private ImageView iv_src;
    private boolean b = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题栏

        Bitmap bitmap = Bitmap.createBitmap(20, 20, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        paint.setDither(true);
        canvas.drawCircle(10, 10, 10, paint);

        iv_src = new ImageView(this);
        iv_src.setImageBitmap(bitmap);
        setContentView(iv_src, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            b = !b;
            Point point = new Point();
            ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getSize(point);

            ValueAnimator animator = new ValueAnimator().setDuration(1000);
            //通过new创建的ValueAnimator要明确的setObjectValues和setEvaluator,并且一定要先setObjectValues,再setEvaluator
            if (b) animator.setObjectValues(new PointF(0, 0), new PointF(point.x - iv_src.getWidth(), point.y - iv_src.getHeight()));
            else animator.setObjectValues(new PointF(0, point.y - iv_src.getHeight()), new PointF(point.x - iv_src.getWidth(), 0));
            animator.setEvaluator(new PointFEvaluator());//PointFEvaluator为自定义的估值器,其实就是用来封装你需要的数据用的
            animator.addUpdateListener(new AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    PointF point = (PointF) animation.getAnimatedValue();
                    iv_src.setX(point.x);
                    iv_src.setY(point.y);
                }
            });
            animator.start();
        }
        return super.onTouchEvent(event);
    }

}


TypeEvaluator 估值器 抛物线的更多相关文章

  1. Android属性动画:插值器与估值器

    声明:本篇文章部分内容来自<Android开发艺术探索>. 我们都知道对于属性动画可以对某个属性做动画,而 插值器(TimeInterpolator)和 估值器(TypeEvaluator ...

  2. Android 动画:你真的会使用插值器与估值器吗?

    目录   目录 1. 插值器(Interpolator) 1.1 简介 定义:一个接口 作用:设置 属性值 从初始值过渡到结束值 的变化规律 如匀速.加速 & 减速 等等 即确定了 动画效果变 ...

  3. 属性动画详解 Interpolator TypeEvaluator

    概述 产生原因         3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:prope ...

  4. 深入分析Android动画(一)

    动画的分类: ①View动画 View动画顾名思义其作用对象为View,包含平移.缩放.旋转.透明,这四类变化分别对应着Animation的子类TranlateAnimation.ScaleAnima ...

  5. Android开发之漫漫长途 XVII——动画

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  6. 【属性动画总结】Property Animation

    属性动画概述 3.0以前,android仅支持两种动画模式,tweened animation 和 frame-by-frame animation,在android3.0中又引入了一个新的动画系统: ...

  7. Android系统编程入门系列之界面Activity响应多元的属性动画

    在响应丝滑动画一篇文章中,分别介绍了作用于普通视图.绘制视图的绘制对象.和界面这三种对象的动画效果,但是都有一些使用的局限性.比如这些动画都只是以屏幕上绘制更新的方式绘制动画,并没有真实改变作用对象的 ...

  8. 字节跳动Android春招,三轮面试,夺命连环问,心态崩了

    我是春招参加字节面试的,现在已经入职俩月啦,当时没有及时记录下来拖到现在...我尽量回忆当时的内容希望能帮到大家. 投的部门是深圳字节影像,不得不说这个部门的效率,上午投下午就接到hr的电话约面试时间 ...

  9. 2021年最新字节跳动Android面试真题解析

    概述 时间过得是真TM快,回想自己是16年从学校毕业,现在是出来工作的第五个年头啦.在不同的大小公司都待过,就在前段时间顺利的完成了一次跳槽涨薪,面试了几家公司,最终选择了字节跳动.今特此前来跟大家进 ...

随机推荐

  1. pyqt5最简单操作数据库

    要先安一个包才能使用QtSql通过新立得安装 import PyQt5.QtSql as sql db=sql.QSqlDatabase.addDatabase('QMYSQL') db.setDat ...

  2. loadrunner中Run-time-Setting设置

  3. Codeforces Round #361 (Div. 2) D - Friends and Subsequences

    题目大意:给你两个长度为n的数组a, b,问你有多少个问你有多少个区间满足 a中最大值等于b中最小值. 思路:我本来的想法是用单调栈求出每个点的管辖区间,然后问题就变成了巨麻烦的线段覆盖问题,就爆炸写 ...

  4. Redis Cluster集群的搭建与实践

    Redis Cluster集群 一.redis-cluster设计 Redis集群搭建的方式有多种,例如使用zookeeper等,但从redis 3.0之后版本支持redis-cluster集群,Re ...

  5. elementUI 学习入门之 checkbox 复选框

    CheckBox 复选框 与单选框基本类似.如:按钮样式.带边框.复选框按钮大小. eg: <template> <el-checkbox-group v-model="s ...

  6. redis环境搭建和java应用

    安装 连接 Java连接redis 下载 wget http://download.redis.io/releases/redis-4.0.9.tar.gz 解压移动 tar -xvf redis-4 ...

  7. BZOJ1018 堵塞的交通(线段树)

    题目很好明白,然后实现很神奇.首先如果考虑并查集的话,对于删边和加边操作我们无法同时进行.然后暴力分块的话,复杂度是O(n sqrt n) ,不是很优.于是看了题解,发现了线段树的神奇用途. 我们维护 ...

  8. BZOJ 3676: [Apio2014]回文串 回文树 回文自动机

    http://www.lydsy.com/JudgeOnline/problem.php?id=3676 另一种更简单更快常数更小的写法,很神奇……背板子. #include<iostream& ...

  9. POJ3744 Scout YYF I 概率DP+矩阵快速幂

    http://poj.org/problem?id=3744 题意:一条路,起点为1,有概率p走一步,概率1-p跳过一格(不走中间格的走两步),有n个点不能走,问到达终点(即最后一个坏点后)不踩坏点的 ...

  10. [CSAcademy]Squared Ends

    [CSAcademy]Squared Ends 题目大意: 给你一个长度为\(n(n\le10^4)\)的数列\(\{A_i\}(A_i\le10^6)\).定义区间\(A_{[l,r]}\)的代价为 ...