Imageview issue with zoom in and out,drag with max

2020-03-01 03:06发布

问题:

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
}

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........

回答1:

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
}