Using an ImageView transform matrix instead of canvas.scale(float, float, float, float)

泪湿孤枕 提交于 2020-01-06 13:36:36

问题


I need to change the way I am scaling and moving my imageview to using a matrix instead of calling canvas.scale(,,,,), but I cannot figure out the proper calls to do this.

I had this in my ScaleListener listener class's onScale method

public boolean onScale(ScaleGestureDetector detector) {

    ...//determining zoom level and pivot points offset

    child.zoom(scaleFactor, zoomCenter[0], zoomCenter[1]);
}

I had in the child ImageView's onDraw method:

protected void onDraw(Canvas canvas){

    super.onDraw(canvas);
    canvas.scale(mScaleFactor, mScaleFactor, mPosX, mPosY);
    ...
    //do all the drawing here
}

public void zoom(float scaleFactor, float zoomCenterX, float zoomCenterY) {
    mScaleFactor = scaleFactor;
    mPosX = zoomCenterX;
    mPosY = zoomCenterY;

    //redraw and remeasure the mapview after zooming in
    invalidate();

}

I would like something like this in the onScale method:

public boolean onScale(ScaleGestureDetector detector) {
        ...//calculating scale and offsets

        Matrix m = child.getImageMatrix();
        m.reset();
        m.postScale(scaleFactor, scaleFactor);
        m.postTranslate(zoomCenter[0], zoomCenter[1]);
        child.setScaleType(ScaleType.MATRIX);
        child.setImageMatrix(m);

        child.invalidate();


        return true;
    }

But this does not appear to be doing anything...am I missing something here? I tried using child.getMatrix() instead but that through an IllegalStateException saying the matrix cannot be modified. Any help would be great!


回答1:


Okay I figured out what the problem was. I was using setImageMatrix(m) to apply to the entire ImageView child class, instead of setting the matrix for the canvas, like this:

@Override
protected void onDraw(Canvas canvas){

    ...
    canvas.concat(transform);

}

I'm not sure what the difference is between the two though, if anyone knows see difference bewteen imageMatrix and matrix

EDIT: using canvas.setMatrix(transform) messed up the scrolling; canvas.concat(transform) was recommended instead and that does this trick




回答2:


What if you try?

m.setScale(scaleFactor, scaleFactor);
m.postTranslate(zoomCenter[0], zoomCenter[1]);

I also believe child.setScaleType(ScaleType.MATRIX) is not needed. At least my coding works without it.



来源:https://stackoverflow.com/questions/18408122/using-an-imageview-transform-matrix-instead-of-canvas-scalefloat-float-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!