ImageView的问题,放大和缩小,拖动最高和最低水平拖动、大和、最低、水平

2023-09-05 10:19:30 作者:你是我向往的以后あ

我已经执行的缩放和拖动functionalities.I现在用矩阵的缩放和拖动functionality.But我的问题是图像显示的是,我不能把我的最小和最大缩放级别,并拖累空间limit.can任何的Ë告诉我,我怎么能做到这一点。

I had implemented an image view with the zoom and drag functionalities.I am using matrix for the zoom and drag functionality.But my problem is that I cant set my minimum and maximum zoom level and the drag space limit.can any on e tell me how I can do that.

    private float spacing(MotionEvent event) {
       float x = event.getX(0) - event.getX(1);
       float y = event.getY(0) - event.getY(1);
       return FloatMath.sqrt(x * x + y * y);
    }

    private void midPoint(PointF point, MotionEvent event) {
       float x = event.getX(0) + event.getX(1);
       float y = event.getY(0) + event.getY(1);
       point.set(x / 2, y / 2);
    }

    private float spacing(PointF start,PointF end)
    {
           float x = end.x- start.x;
           float y = end.y -start.y;

           return FloatMath.sqrt(x * x + y * y);
    }

@Override
public boolean onTouch(View v, MotionEvent event) 
{
       ImageView view = (ImageView) v;
       view.setScaleType(ImageView.ScaleType.MATRIX);

       float scale;


       switch (event.getAction() & MotionEvent.ACTION_MASK) {

       case MotionEvent.ACTION_DOWN:

            savedMatrix.set(matrix);
            start.set(event.getX(), event.getY());
            Log.d(TAG, "mode=DRAG" );
            mode = DRAG;

          break;
       case MotionEvent.ACTION_UP: 

           if(mode==DRAG)
           {
               PointF end=new PointF();
               end.set(event.getX(), event.getY());

               Log.d("Fling", "Inside the Action Key UP"+spacing(start, end));

               float []x = new float[9],org=new float[9];

               matrix.getValues(x);
               orgMatrix.getValues(org);

               Log.d("Fling", "matrixValue"+matrix);
               Log.d("Fling", "OrgmatrixValue"+orgMatrix);

               float matrixSizex=x[Matrix.MSCALE_X];
               float matrixSizey=x[Matrix.MSCALE_Y];


               float matrixOrgSizex=org[Matrix.MSCALE_X];
               float matrixOrgSizey=org[Matrix.MSCALE_Y];

               if(Math.abs(matrixOrgSizex-matrixSizex)<0.17f&&Math.abs(matrixOrgSizey-matrixSizey)<0.17f)
               {
                   Log.d("Fling", "Current Size is equal");
                   if(spacing(start, end)>30.f)
                   {
                       if((start.x>end.x+30)&&(Math.abs(start.y-end.y)<50.0f))
                       {
                           Log.d("Fling", "Is From Right To left");
                           loadedimage.setImageMatrix(orgMatrix);
                           leftSwipe();
                           view.setScaleType(ImageView.ScaleType.FIT_XY);
                       }
                       else if((end.x>start.x+30)&&(Math.abs(end.y-start.y)<50.0f))
                       {
                           Log.d("Fling", "Is From Left To Right");                        
                           loadedimage.setImageMatrix(orgMatrix);
                           rightSwipe();
                           view.setScaleType(ImageView.ScaleType.FIT_XY);
                       }
                   }
               }
           }
       case MotionEvent.ACTION_POINTER_UP: //second finger lifted
          mode = NONE;
          Log.d(TAG, "mode=NONE" );
          break;
       case MotionEvent.ACTION_POINTER_DOWN: //second finger down
          oldDist = spacing(event);
          Log.d(TAG, "oldDist=" + oldDist);
          if (oldDist > 5f) {
             savedMatrix.set(matrix);
             midPoint(mid, event);
             mode = ZOOM;
             Log.d(TAG, "mode=ZOOM" );
          }
          break;

       case MotionEvent.ACTION_MOVE: 
          if (mode == DRAG) { 

              //movement of first finger
               PointF end=new PointF();
               end.set(event.getX(), event.getY());

               Log.d("Fling", "Inside the Action Key UP"+spacing(start, end));

               float []x = new float[9],org=new float[9];

               matrix.getValues(x);
               orgMatrix.getValues(org);

               Log.d("Fling", "matrixValue"+matrix);
               Log.d("Fling", "OrgmatrixValue"+orgMatrix);

               float matrixSizex=x[Matrix.MSCALE_X];
               float matrixSizey=x[Matrix.MSCALE_Y];


               float matrixOrgSizex=org[Matrix.MSCALE_X];
               float matrixOrgSizey=org[Matrix.MSCALE_Y];

               if(Math.abs(matrixOrgSizex-matrixSizex)>=0.17f&&Math.abs(matrixOrgSizey-matrixSizey)>=0.17f)
               {

                   matrix.set(savedMatrix);

                   if (view.getLeft() >= 0)
                   {
                       matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
                   }
               }
          }
          else if (mode == ZOOM) { //pinch zooming
             float newDist = spacing(event);
             Log.d(TAG, "newDist=" + newDist);
             if (newDist > 5f) {
                matrix.set(savedMatrix);
                scale = newDist / oldDist; //thinking i need to play around with this value to limit it**
                matrix.postScale(scale, scale, mid.x, mid.y);
             }
          }
          break;
       }

       // Perform the transformation
       view.setImageMatrix(matrix);

       return true; // indicate event was handled
}

我的样品code是这里任何一个可以帮助我在设置最小和最大缩放和拖动level.I也有问题,当我在图像中触摸视图,它会自动获得在转换到矩阵我不能让它放大保持健康的设备screen.I我完全被困在这儿了........

My Sample code is here can any one help me in setting minimum and maximum zoom and drag level.I also have problem when I touch in the image view it automatically get zoomed when converted to the matrix I cant make it stay fit in the device screen.I am totally stuck here........

推荐答案

要限制缩放我比较缩放矩阵与单位矩阵,它,如果它比矩阵小不分配给我的ImageView,在这种情况下,我重置缩放矩阵回单位矩阵。我使用的是单声道为Android,但我想它会在Java中几乎是一样的:

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);
      }

在哪里SmallerThanIdentity实现:

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));
}

和这里的ResetView。我有Java的code为:

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);
}

对于滚动,我翻译矩阵的地方,我要限制的滚动区域之前使用下面的方法ShouldScroll

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
}