2017-04-19 73 views
1

在我的Android應用程序中,我使用了ZoomImageView Class。其中所有的作品完美,除了我的ImageView.it停止縮放,拖動和旋轉達到後MAX縮放級別。我的圖像停止在最大程度縮放後移動

下面是我的的onTouchEvent()

@Override 
    public boolean onTouch(View v, MotionEvent event) { 

     mScaleDetector.onTouchEvent(event); 
     ImageView view = (ImageView) v; 
     float scale; 
     dumpEvent(event); 
     // Handle touch events here... 

     switch (event.getAction() & MotionEvent.ACTION_MASK) { 
      case MotionEvent.ACTION_CANCEL: 
       touch = false; 
       invalidate(); 
       break; 
      case MotionEvent.ACTION_DOWN: 
       if (isInBitmap(event)) { 
        touch = true; 
        savedMatrix.set(matrix); 
        if (MainActivity.mCurrentView != null) { 
         MainActivity.mCurrentView.setInEdit(false); 
         MainActivity.mCurrentView.setInEdit(false); 
        } 
        if (bottom.getVisibility() == INVISIBLE) { 
         rel.bringToFront(); 
         setAlpha(0.6f); 
        } 
        bottom.setVisibility(INVISIBLE); 
        start.set(event.getX(), event.getY()); 
        mode = DRAG; 
        left.setVisibility(VISIBLE); 
        right.setVisibility(VISIBLE); 
        bottom.setVisibility(INVISIBLE); 
        invalidate(); 
        postInvalidate(); 
        requestLayout(); 
       } 
       break; 
      case MotionEvent.ACTION_UP: 
       touch = false; 
       rel_frame.bringToFront(); 
       left.bringToFront(); 
       right.bringToFront(); 
       bottom.bringToFront(); 
       // MainActivity.drawabletext.bringToFront(); 
       setAlpha(1.0f);// first finger lifted 
       invalidate(); 

       break; 
      case MotionEvent.ACTION_POINTER_UP: // second finger lifted 
       mode = NONE; 
       //lastEvent=null; 
       invalidate(); 
       break; 

      case MotionEvent.ACTION_POINTER_DOWN: // first and second finger down 
       if (isInBitmap(event)) { 
        touch = true; 
        if (bottom.getVisibility() == INVISIBLE) { 
         rel.bringToFront(); 
         setAlpha(0.6f); 
        } 
        oldDist = spacing(event); 
        if (oldDist > 5f) { 
         savedMatrix.set(matrix); 
         midPoint(mid, event); 
         mode = ZOOM; 
        } 
        if (oldDist > 10f) { 
         savedMatrix.set(matrix); 
         midPoint(mid, event); 
        } 
        lastEvent = new float[4]; 
        lastEvent[0] = event.getX(0); 
        lastEvent[1] = event.getX(1); 
        lastEvent[2] = event.getY(0); 
        lastEvent[3] = event.getY(1); 
        d = rotation(event); 
        invalidate(); 
        postInvalidate(); 
        requestLayout(); 
       } 

       break; 
      case MotionEvent.ACTION_MOVE: 

       if (isInBitmap(event)) { 

        if (mode == DRAG) { 
         matrix.set(savedMatrix); 
         matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); // create the transformation in the matrix of points 
        } else if (mode == ZOOM) { 
         // pinch zooming 
         float newDist = spacing(event); 
         Log.d(TAG, "newDist=" + newDist); 
         if (newDist > 5f) { 
          matrix.set(savedMatrix); 
          scale = newDist/oldDist; 
          matrix.postScale(scale, scale, mid.x, mid.y); 

         } 
         if (lastEvent != null) { 
          newRot = rotation(event); 
          float r = newRot - d; 
          matrix.postRotate(r, getMeasuredWidth()/2, 
            getMeasuredHeight()/2); 
         } 
        } 
        invalidate(); 
        postInvalidate(); 
       } 
       break; 
     } 
     invalidate(); 
     postInvalidate(); 
     requestLayout(); 
     view.setImageMatrix(matrix); 
     invalidate(); 
     postInvalidate(); 
     requestLayout();// display the transformation on screen 
     return true; 
    } 

請人幫我出從這個Problem.Thanks提前

+0

'invalidate(); postInvalidate(); requestLayout();'重複兩次?你爲什麼這樣做? – pskink

+0

嘿@Alis試試我的代碼,讓我知道...希望這會幫助你。 –

+0

@pskink刷新的ImageView以防萬一它停止堅持並開始工作,因爲我想要的。 – Alis

回答

1

更換你onTouch與你,讓我知道..

@Override 
public boolean onTouch(View v, MotionEvent event) { 

    view1 = (ImageView) v; 
    switch (event.getAction() & MotionEvent.ACTION_MASK) { 
     case MotionEvent.ACTION_DOWN: 
      savedMatrix.set(matrix); 
      start.set(event.getX(), event.getY()); 
      mode = DRAG; 
      lastEvent = null; 
      break; 
     case MotionEvent.ACTION_POINTER_DOWN: 
      oldDist = spacing(event); 
      if (oldDist > 10f) { 
       savedMatrix.set(matrix); 
       midPoint(mid, event); 
       mode = ZOOM; 
      } 
      lastEvent = new float[4]; 
      lastEvent[0] = event.getX(0); 
      lastEvent[1] = event.getX(1); 
      lastEvent[2] = event.getY(0); 
      lastEvent[3] = event.getY(1); 
      d = rotation(event); 
      break; 
     case MotionEvent.ACTION_UP: 
     case MotionEvent.ACTION_POINTER_UP: 
      mode = NONE; 
      lastEvent = null; 
      break; 
     case MotionEvent.ACTION_MOVE: 
      if (mode == DRAG) { 
       matrix.set(savedMatrix); 
       float dx = event.getX() - start.x; 
       float dy = event.getY() - start.y; 
       matrix.postTranslate(dx, dy); 
      } else if (mode == ZOOM) { 
       float newDist = spacing(event); 
       if (newDist > 10f) { 
        matrix.set(savedMatrix); 
        float scale = (newDist/oldDist); 
        matrix.postScale(scale, scale, mid.x, mid.y); 
       } 

       if (lastEvent != null) { 
        newRot = rotation(event); 
        float r = newRot - d; 
        matrix.postRotate(r, view1.getWidth()/2, 
          view1.getHeight()/2); 
       } 
      } 
      break; 
    } 
    view1.setImageMatrix(matrix); 

    return true; 
} 

並確保使用android默認ImageView而不是庫ZoomImageView

+0

你的代碼工作得很好謝謝。 – Alis

+0

對不起薩加爾我不能+1投票你我很少有聲望的code.because。 – Alis

+0

確定,但24小時後您將可以接受我的答案。 –