2012-11-27 60 views
1

我已經在imageview中設置了位圖,並且在觸摸多次的同時進行操作旋轉和拖動位圖。我想要在imageview中設置的位圖不應該從imageview中的特定區域出去。限制位圖不應該跨越圖像視圖中的特定區域

有什麼辦法可以達到這個目的。

請幫我這個..

@Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     canvas.save(); 
     scaleCount=scaleCount+scale; 
     angleCount = addAngle(angleCount, Math.toDegrees(angle));  

     if (!isInitialized) { 
      int w = getWidth(); 
      int h = getHeight(); 
      position.set(w/2, h/2); 
      isInitialized = true; 
     } 

     Paint paint = new Paint(); 
     paint.setFilterBitmap(true); 
     //Log.v("Height and Width", "Height: "+ getHeight() + "Width: "+ getWidth()); 

     transform.reset(); 
     transform.postTranslate(-width/2.0f, -height/2.0f); 
     transform.postRotate((float) Math.toDegrees(angle)); 
     transform.postScale(scale, scale); 
     transform.postTranslate(position.getX(), position.getY()); 

     rectangleF = new RectF(); 
     transform.mapRect(rectangleF); 
     //Log.v("VM", "Rect " + rectangleF.left + " " + rectangleF.top + " " + rectangleF.right + " " + rectangleF.bottom); 

     canvas.drawBitmap(bitmap, transform, paint); 
     canvas.restore(); 
     BitmapWidth=BitmapWidth+bitmap.getScaledWidth(canvas); 
     BitmapHeight=BitmapHeight+bitmap.getScaledHeight(canvas); 

    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     vca = null; 
     vcb = null; 
     vpa = null; 
     vpb = null; 

     x = event.getX(); 
     y = event.getY(); 

     try { 
      touchManager.update(event); 

      if (touchManager.getPressCount() == 1) { 
       vca = touchManager.getPoint(0); 
       vpa = touchManager.getPreviousPoint(0); 

       position.add(touchManager.moveDelta(0)); 
      } 
      else { 
       if (touchManager.getPressCount() == 2) { 
        vca = touchManager.getPoint(0); 
        vpa = touchManager.getPreviousPoint(0); 
        vcb = touchManager.getPoint(1); 
        vpb = touchManager.getPreviousPoint(1); 

        VMVector2D current = touchManager.getVector(0, 1); 
        VMVector2D previous = touchManager.getPreviousVector(0, 1); 
        float currentDistance = current.getLength(); 
        float previousDistance = previous.getLength(); 

        if (currentDistance-previousDistance != 0) { 
         scale *= currentDistance/previousDistance; 
        } 
        angle -= VMVector2D.getSignedAngleBetween(current, previous); 
        /*angleCount=angleCount+angle;*/ 
       } 
      } 
      invalidate(); 
     } 
     catch(Exception exception) { 
     // Log.d("VM", exception.getMessage()); 
     } 
     return true; 
    } 

回答