Imageview issue with zoom in and out,drag with maximum and minimum levels

纵饮孤独 提交于 2019-11-30 16:56:40

To restrict zooming I compare the zoomed matrix with the identity matrix and don't assign it to my ImageView if it's smaller than the identity matrix, in which case I reset the scaled matrix back to the identity matrix. I'm using Mono for Android but I guess it will be almost the same in Java:

      //check that zoom is not too small
      if (Utils.SmallerThanIdentity(matrix))
      {
        ResetView(v);
      }

Where SmallerThanIdentity is implemented:

public static bool SmallerThanIdentity(Android.Graphics.Matrix m)
{
  float[] values = new float[9];
  m.GetValues(values);

  return ((values[0] < 1.0) || (values[4] < 1.0) || (values[8] < 1.0));
}

And here's ResetView. I have Java code for that:

public void resetView(View v)
{
  ImageView view = (ImageView)v;
  matrix = new Matrix();

  view.setScaleType(ImageView.ScaleType.MATRIX);
  view.setImageMatrix(matrix);
}

Regarding scroll, I use the ShouldScroll method below before translating the matrix to the area where I want to restrict the scrolling.

private bool ShouldScroll(Android.Graphics.Matrix matrix)
{
  float[] values = new float[9];
  matrix.GetValues(values);

  float[] oldValues = new float[9];
  oldMatrix.GetValues(oldValues);

  float zoomPercentX = values[0] / oldValues[0];
  float zoomPercentY = values[4] / oldValues[4];

  float tmpW = -(this.Drawable.IntrinsicWidth / 2) * zoomPercentX;
  float tmpH = -(this.Drawable.IntrinsicHeight / 2) * zoomPercentY;

  return (values[2] < 0.0f) && (values[2] > tmpW) && //horizontal coordinates
          (values[5] < 0.0f) && (values[5] > tmpH); //vertical coordinates
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!