2012-11-08 176 views
0

的我有一個的ImageView到我所設置OnTouchListener使拖動並用手指手勢旋轉它。我的問題自帶旋轉,當我與我的實際代碼這樣做,結果是位圖,但旋轉如預期覆蓋之前的版本,如圖附圖片。有沒有人有一個想法,爲什麼發生這種情況?謝謝。旋轉ImageView的

//Touch event related variables 
int touchState; 
final int IDLE = 0; 
final int TOUCH = 1; 
final int PINCH = 2; 
float dist0, distCurrent; 
float angle; 

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

      // use angle to rotate view 
      angle = getDegreesFromRadians((float) (Math.atan(disty/distx))); 
      Log.i("Angle of rotation", angle + ""); 

      drawMatrix((ImageView) view); 

     } else { 
      handledHere = startDrag (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.5){ 
     curScale = 0.5f;  
    } else if (curScale > 1.5){ 
     curScale = 1.2f; 
    } 

    Bitmap originalBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); //view.getDrawingCache(); 
    Canvas canvas = new Canvas(originalBitmap); 
    view.draw(canvas); 
    int[] originalSize = (int[]) view.getTag(); 
    int newHeight = (int) (originalSize[1] * curScale); //(view.getHeight() * curScale); 
    int newWidth = (int) (originalSize[0] * curScale); //(view.getWidth() * curScale); 

    view.setImageBitmap(getResizedBitmap(originalBitmap, newHeight, newWidth)); 

} 

private Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 
    int width = bm.getWidth(); 
    int height = bm.getHeight(); 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 
    // create a matrix for the manipulation 
    Matrix matrix = new Matrix(); 
    // rotate the bipmap 
    matrix.postRotate(angle); 
    // recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 
    return resizedBitmap; 
} 

image rotation

回答

0

我想你畫一個旋轉的圖像前,應清除畫布。

我將實例在constructorcanvas,將剛剛爲什麼不使用ImageView.setScaleType(MATRIX)繪製圖像

Paint paint = new Paint(); 
paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR)); 
canvas.drawPaint(paint); 
0

前對PorterDuff併發揮與矩陣的方法。 您甚至可以使用矩陣進行縮放,平移和旋轉。