2012-10-30 90 views
0

我有一個ImageView,我添加了一個OnTouchListener,以便我可以用兩個手指捏放手勢放大和縮小後者。我使用下面的代碼,但是我在調​​整實際位圖大小時遇到​​問題。後者變得模糊不清,保持相同的大小,並與圖像的大小調整版本疊加,就好像它只是將新圖像放在舊圖像的頂部而不是替換它。我想有一個與該drawMatrix方法的問題...捏縮放ImageView時的奇怪行爲

int touchState; 
    final int IDLE = 0; 
    final int TOUCH = 1; 
    final int PINCH = 2; 
    float dist0, distCurrent; 

    public boolean onTouch(View view, MotionEvent event) { 
     boolean handledHere = false; 

     float distx, disty; 

     final int action = event.getAction(); 

     switch(action & MotionEvent.ACTION_MASK){ 
     case MotionEvent.ACTION_DOWN: 
      //A pressed gesture has started, the motion contains the initial starting location. 
      touchState = TOUCH; 
      break; 
     case MotionEvent.ACTION_POINTER_DOWN: 
      //A non-primary pointer has gone down. 
      touchState = PINCH; 

      //Get the distance when the second pointer touch 
      distx = event.getX(0) - event.getX(1); 
      disty = event.getY(0) - event.getY(1); 
      dist0 = FloatMath.sqrt(distx * distx + disty * disty); 

      break; 
     case MotionEvent.ACTION_MOVE: 
      //A change has happened during a press gesture (between ACTION_DOWN and ACTION_UP). 
      if(touchState == PINCH){       
       //Get the current distance 
       distx = event.getX(0) - event.getX(1); 
       disty = event.getY(0) - event.getY(1); 
       distCurrent = FloatMath.sqrt(distx * distx + disty * disty); 

       drawMatrix((ImageView) view); 
      } 

      break; 
     case MotionEvent.ACTION_UP: 
      //A pressed gesture has finished. 
      touchState = IDLE; 
      break; 
     case MotionEvent.ACTION_POINTER_UP: 
      //A non-primary pointer has gone up. 
      touchState = TOUCH; 
      break; 

     } 

     return handledHere; 
    } 

    private void drawMatrix(ImageView view){ 
     float curScale = distCurrent/dist0; 
     if (curScale < 0.1){ 
      curScale = 0.1f;  
     } 

     view.buildDrawingCache(); 
     Bitmap originalBitmap = view.getDrawingCache(); 
     Bitmap resizedBitmap;  
     int newHeight = (int) (view.getHeight() * curScale); 
     int newWidth = (int) (view.getWidth() * curScale); 
     resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, false); 
     view.setImageBitmap(resizedBitmap); 

    } 

當我和數次放大,這裏是它是如何成爲... enter image description here

感謝所有幫助我能得到。

回答

1

實際上,您正在調用onmove中的繪圖函數,因此在您移動手指時它會一直調用它。因此,使用新的高度和寬度生成的圖像已設置爲imageview,這是alredy set der。嘗試刪除所有的意見之前設置調整大小的圖像imageview.Hope這將解決您的問題。

private void drawMatrix(ImageView view){ 
     float curScale = distCurrent/dist0; 
     if (curScale < 0.1){ 
      curScale = 0.1f;  
     } 

     view.buildDrawingCache(); 
     Bitmap originalBitmap = view.getDrawingCache(); 
     Bitmap resizedBitmap;  
     int newHeight = (int) (view.getHeight() * curScale); 
     int newWidth = (int) (view.getWidth() * curScale); 
     resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, false); 
     view.removeallviews(); 
     view.setImageBitmap(resizedBitmap); 

    } 
+0

我可以將removeAllViews()傳遞給ImageView嗎?它不工作.. – jpmastermind

+0

我很抱歉,它不會與imageview工作。我遲到檢查與它。實際上,我期待解決這個問題將很快打回我做這件事..我看到 –

+0

謝謝,我期待着您的解決方案 – jpmastermind

0

您可以使用TouchImageView類[https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/example/touch/TouchImageView.java。 這爲圖像視圖提供了很好的放大和縮小功能。

+0

我試過了,事情是我已經可以在特定區域內拖動它。使用TouchImageView,爲了使其僅在特定區域內可縮放,我必須將其高度和寬度設置爲該區域的高度和寬度,但如果這樣做,我將無法將其他ImageView拖放到該區域。 ..我需要的東西,將放大ImageView本身,而不是ImageView的區域內的圖像。你明白嗎? – jpmastermind