0

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!

kburbach
  • 761
  • 10
  • 26

2 Answers2

2

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

Community
  • 1
  • 1
kburbach
  • 761
  • 10
  • 26
0

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.

sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
  • what is the difference between postScale() and setScale()? Also, I noticed there is a setScale(float, float, float, float) that i can use that does basically the same thing. But with the code I have above, nothing is updating – kburbach Aug 23 '13 at 17:41
  • As I understand it, "post" is the last operation applied to the matrix. It can be only one "post" operation. – sergej shafarenka Aug 23 '13 at 17:43
  • 1
    This questions has nice answer that cleared up the difference for me: http://stackoverflow.com/questions/3855578/android-matrix-what-is-the-different-between-preconcat-and-postconcat – kburbach Aug 23 '13 at 22:02