Scale and center matrix on arbitrary point

跟風遠走 提交于 2019-12-23 21:34:52

问题


I'm using a custom ImageView to display a rather large bitmap. The bitmap display is being handled by a matrix that transforms it to what the user sees. I'm trying to implement a "double-tap-to-zoom" but I can't quite get it right. I'm trying to have the image zoom on the point where the user touched with this point ending up in the center of the screen at the end.

I worked through a lot of the matrix math and transformations and basically the following transformation is what I need to do

float dx = centerX - focusX;
float dy = centerY - focusY;

Matrix m = new Matrix( baseMatrix );
m.postTranslate( -focusX, -focusY );
m.postScale( scale, scale );
m.postTranslate( focusX + dx, focusY + dy );

Which if I was just swapping the matrices would be fine but I need to animate from the baseMatrix to this new one. Is there a way I can interpolate between these two matrices?

I tried to interpolate the scale and translation separately but that didn't work out well for me (quite possible that I did it wrong and it is the correct way to go). The way I am currently interpolating just for the scale is below. I've tried adding a translation interpolation in the handler as well and it just didn't work out

mHandler.post( new Runnable() {

  @Override
  public void run() {
    mZooming = true;

    long now = System.currentTimeMillis();
    float currentMs = Math.min( durationMs, now - startTime );
    float newScale = (float) mEasing.easeInOut( currentMs, 0, deltaScale, durationMs );

    zoomTo( oldScale + newScale, destX, destY );

    if ( currentMs < durationMs ) {
      mHandler.post( this );
    } else {
      onZoomAnimationCompleted( getScale() );
      scrollBy( dx, dy, durationMs )
    }
  }
});

Has anyone done something like this before? Am I approaching it completely wrong?

Thanks in advance

来源:https://stackoverflow.com/questions/21539949/scale-and-center-matrix-on-arbitrary-point

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