3.工具类解析-Transformer(MPAndroidChart源码解析)

995 阅读2分钟

1.作用

Transformer,顾名思义就是个转换类,是将传入的value转化为相应的屏幕px坐标值。

2.主要成员变量

protected Matrix mMatrixValueToPx = new Matrix();//matrix to map the values to the screen pixels
protected Matrix mMatrixOffset = new Matrix();// matrix for handling the different offsets of the chart
protected ViewPortHandler mViewPortHandler;//会在构造函数中传入这个ViewPortHandler

3.主要成员方法

/**
     * 将原始数据value 对应到 屏幕px的矩阵,原理很简单,
     * 比如xValue=[10,90],yValue = [20,100],屏幕的大小为[0,0,100,200]
     * 先将xValue 和 yValue的最小值作为起始点 matrix.postTranslate(-xChartMin, -yChartMin);
     * 然后计算出需要缩放的比例 scaleX= 100/(90-10)  scaleY = 200/(100 - 20)
     * postScale(scaleX, -scaleY)  注意-scaleY是为了将屏幕坐标系转为普通的坐标系
     *
     * @param xChartMin
     * @param deltaX   X轴的range
     * @param deltaY   Y轴的range
     * @param yChartMin
     */
    public void prepareMatrixValuePx(float xChartMin, float deltaX, float deltaY, float yChartMin) {

        float scaleX = (float) ((mViewPortHandler.contentWidth()) / deltaX);
        float scaleY = (float) ((mViewPortHandler.contentHeight()) / deltaY);

        if (Float.isInfinite(scaleX)) {
            scaleX = 0;
        }
        if (Float.isInfinite(scaleY)) {
            scaleY = 0;
        }

        // setup all matrices
        mMatrixValueToPx.reset();
        mMatrixValueToPx.postTranslate(-xChartMin, -yChartMin);
        mMatrixValueToPx.postScale(scaleX, -scaleY);
    }
/**
     * Prepares the matrix that contains all offsets.
     *
     * @param inverted
     */
    public void prepareMatrixOffset(boolean inverted) {

        mMatrixOffset.reset();

        // offset.postTranslate(mOffsetLeft, getHeight() - mOffsetBottom);

        if (!inverted)
            mMatrixOffset.postTranslate(mViewPortHandler.offsetLeft(),
                    mViewPortHandler.getChartHeight() - mViewPortHandler.offsetBottom());
        else {
            mMatrixOffset
                    .setTranslate(mViewPortHandler.offsetLeft(), -mViewPortHandler.offsetTop());
            mMatrixOffset.postScale(1.0f, -1.0f);
        }
    }
 /**
     * Transform an array of points with all matrices. VERY IMPORTANT: Keep
     * matrix order "value-touch-offset" when transforming.
     原作者给出了转换时的顺序value-touch-offset,分别对用Transforer的mMatrixValueToPx,ViewPortHandler的mMatrixTouch,Transfomer的        mMatrixOffset
     为什么要按照这种顺序呢?
     首先使用mMatrixValueToPx 这个没什么疑问,绘图要先将value对应成屏幕的px值,然后在调整是否需要缩放,平移,offset之类的
     mMatrixTouch这个可能矩阵包含了缩放,平移等操作
     mMatrixOffset只包含了offset的信息,大概的样子是[1,0,transX][0,1,transY][0,0,1]
     如果先调用了mMatrixOffset 在调用mMatrixTouch,那我们的平移操作肯定会被后面的mMatrixTouch缩放,这就错了
     *
     * @param pts
     */
    public void pointValuesToPixel(float[] pts) {

        mMatrixValueToPx.mapPoints(pts);
        mViewPortHandler.getMatrixTouch().mapPoints(pts);
        long s = System.currentTimeMillis();
        mMatrixOffset.mapPoints(pts);
        Log.e("mapPoints","count: "+ pts.length+",mapPoints time:" +(System.currentTimeMillis() - s));
    }

下面对顺序做一下验证:

//验证一下mMatrixTouch和mMatrixOffset翻过来的情况
void testMappoint(){
        //假设有三个点(1,1)(2,2)(3,3)
        float[] points = {1f,1f,2f,2f,3f,3f};

        Matrix matrixTouch = new Matrix();
        matrixTouch.postScale(2,2);

       matrixTouch.mapPoints(points);

       Log.e("test,matrixTouch:",printArray(points));

       Matrix matrixOffset = new Matrix();
       matrixOffset.postTranslate(10,10);
       matrixOffset.mapPoints(points);
       Log.e("test,matrixOffset:",printArray(points));
    
    //得到最终正确结果{12.0,12.0,14.0,14.0,16.0,16.0}
   }

//反向
void testMappoint2(){
        //假设有三个点(1,1)(2,2)(3,3)
        float[] points = {1f,1f,2f,2f,3f,3f};

        Matrix matrixOffset = new Matrix();
        matrixOffset.postTranslate(10,10);
        matrixOffset.mapPoints(points);
        Log.e("test,matrixOffset2:",printArray(points));

        Matrix matrixTouch = new Matrix();
        matrixTouch.postScale(2,2);
        matrixTouch.mapPoints(points);

        Log.e("test,matrixTouch2:",printArray(points));
    //offset被缩放了:{22.0,22.0,24.0,24.0,26.0,26.0}
    }