美文网首页
2.工具类解析-ViewPortHandler(MPAndroi

2.工具类解析-ViewPortHandler(MPAndroi

作者: 世界是一个圆_ | 来源:发表于2018-06-03 16:38 被阅读0次

*1.ViewPortHandler 主要包含当前chart的value的视图信息,比如offsets,缩放的比例,平移的累计量,每一个ChartView都会持有一个ViewPortHandler,它作为一个中间件,连接chartView,Transformer,Axis,ChartTouchListener等。
2.它最重要的一个成员变量是mMatrixTouch,缩放的比例,平移的累计量都放在其中。

1.简单描述其作用

举个简单的例子:在进行了缩放之后是如何刷新界面的,代码如下
首先在ChartTouchListener处理手势:

public class BarLineChartTouchListener extends ChartTouchListener{

        private Matrix mMatrix = new Matrix();
    
 @Override
    public boolean onTouch(View view, MotionEvent event) {
    
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        
        case MotionEvent.ACTION_MOVE:
            //1.计算缩放比例,操作mMatrix,t代表缩放中心点
        mMatrix.postScale(scaleX, scaleY, t.x, t.y);
           break;     
        
        }
        //更新ViewPortHandler的matrix,refresh方法会刷新界面
        mMatrix = mChart.getViewPortHandler().refresh(mMatrix, mChart, true);
        return true;
    }
}
  public Matrix refresh(Matrix newMatrix, View chart, boolean invalidate) {
        mMatrixTouch.set(newMatrix);

        // make sure scale and translation are within their bounds
        limitTransAndScale(mMatrixTouch, mContentRect);

        if (invalidate)
            chart.invalidate();

        newMatrix.set(mMatrixTouch);
        return newMatrix;
    }

最后在chartView的ondraw方法中会获取ViewPortHandler中Matrix的信息重新渲染,这个流程后面会详细的讲解。

2.主要成员变量

protected final Matrix mMatrixTouch = new Matrix();//处理touch event的矩阵
protected RectF mContentRect = new RectF(); //value的绘制区域,去掉offset,坐标轴等

3.主要成员方法

 //每次进行手势操作后,调用此方法,刷新view
public Matrix refresh(Matrix newMatrix, View chart, boolean invalidate){
 mMatrixTouch.set(newMatrix);
        //边界检测
        limitTransAndScale(mMatrixTouch, mContentRect);
        if (invalidate)
            chart.invalidate();
        newMatrix.set(mMatrixTouch);
        return newMatrix;
}
//检测是否超出了最大
public void limitTransAndScale(Matrix matrix, RectF content) {

        matrix.getValues(matrixBuffer);
        //获取累计的transX,scaleX
        float curTransX = matrixBuffer[Matrix.MTRANS_X];
        float curScaleX = matrixBuffer[Matrix.MSCALE_X];
    
        //获取累计的transY,scaleY
        float curTransY = matrixBuffer[Matrix.MTRANS_Y];
        float curScaleY = matrixBuffer[Matrix.MSCALE_Y];

        // min scale-x is 1f
        mScaleX = Math.min(Math.max(mMinScaleX, curScaleX), mMaxScaleX);

        // min scale-y is 1f
        mScaleY = Math.min(Math.max(mMinScaleY, curScaleY), mMaxScaleY);

        float width = 0f;
        float height = 0f;

        if (content != null) {
            width = content.width();
            height = content.height();
        }
        //计算最大的transX,比如没有缩放的情况下,mScaleX = 1,最大transX为0,缩放2倍,最大transX为一个chartWidth
        float maxTransX = -width * (mScaleX - 1f);
        mTransX = Math.min(Math.max(curTransX, maxTransX - mTransOffsetX), mTransOffsetX);

       //计算最大的transY
        float maxTransY = height * (mScaleY - 1f);
        mTransY = Math.max(Math.min(curTransY, maxTransY + mTransOffsetY), -mTransOffsetY);

        matrixBuffer[Matrix.MTRANS_X] = mTransX;
        matrixBuffer[Matrix.MSCALE_X] = mScaleX;

        matrixBuffer[Matrix.MTRANS_Y] = mTransY;
        matrixBuffer[Matrix.MSCALE_Y] = mScaleY;

        matrix.setValues(matrixBuffer);
    }
//计算好offset之后重新设置mContentRect
public void restrainViewPort(float offsetLeft, float offsetTop, float offsetRight,
                                 float offsetBottom) {
        mContentRect.set(offsetLeft, offsetTop, mChartWidth - offsetRight, mChartHeight
                - offsetBottom);
    }
//缩放
public void zoom(float scaleX, float scaleY, float x, float y, Matrix outputMatrix) {
        outputMatrix.reset();
        outputMatrix.set(mMatrixTouch);
        outputMatrix.postScale(scaleX, scaleY, x, y);
    }
//平移 
public void translate(final float[] transformedPts, Matrix outputMatrix) {
        outputMatrix.reset();
        outputMatrix.set(mMatrixTouch);
        final float x = transformedPts[0] - offsetLeft();
        final float y = transformedPts[1] - offsetTop();
        outputMatrix.postTranslate(-x, -y);
    }

4.ViewPortHandler的初始化

在chartView中的onSizeChange方法中得到view的宽高后设置ViewPortHandler的mContent

@Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        

        if (w > 0 && h > 0 && w < 10000 && h < 10000) {
            
            mViewPortHandler.setChartDimens(w, h);
           
        } else {
            if (mLogEnabled)
                Log.w(LOG_TAG, "*Avoiding* setting chart dimens! width: " + w + ", height: " + h);
        }

        // This may cause the chart view to mutate properties affecting the view port --
        //   lets do this before we try to run any pending jobs on the view port itself
        notifyDataSetChanged();

        ...

        super.onSizeChanged(w, h, oldw, oldh);
    }

上面的代码初步设置了宽高了之后,还要给X,Y轴等留出位置,所以要重新设置left,top,right,bottom,这是在ChartView中的这个方法处理的

@Override
    public void calculateOffsets() {
        float offsetLeft = 0f, offsetRight = 0f, offsetTop = 0f, offsetBottom = 0f;


        if (mXAxis.isEnabled() && mXAxis.isDrawLabelsEnabled()) {
            float xLabelHeight = mXAxis.mLabelRotatedHeight + mXAxis.getYOffset();
            offsetBottom += xLabelHeight;
        }

        if (mAxisLeft.isEnabled() && mAxisLeft.isDrawLabelsEnabled()) {
            float longestYLabelWidth = mAxisLeft.getRequiredWidthSpace(mAxisRendererLeft.getPaintAxisLabels());
            offsetLeft += longestYLabelWidth;
        }

        offsetTop += getExtraTopOffset();
        offsetRight += getExtraRightOffset();
        offsetBottom += getExtraBottomOffset();
        offsetLeft += getExtraLeftOffset();

        float minOffset = Utils.convertDpToPixel(mMinOffset);

        mViewPortHandler.restrainViewPort(
                Math.max(minOffset, offsetLeft),
                Math.max(minOffset, offsetTop),
                Math.max(minOffset, offsetRight),
                Math.max(minOffset, offsetBottom));

        prepareOffsetMatrix();
        prepareValuePxMatrix();

        Log.e(TAG, "1.3: notifyDataChanged :计算offsets,本质就是根据设置的offset重新设置mViewHandler的content区域");
        Log.e(TAG, "1.4:notifyDataChanged :调用prepareOffsetMatrix 和 prepareValuePxMatrix 初始化左右两个y轴的transformer的offsetMatrix和valueToPxMatrix,这一步很重要");
    }

相关文章

网友评论

      本文标题:2.工具类解析-ViewPortHandler(MPAndroi

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