2012-11-11 42 views
1

我正在準備somecompass活動。 我有我的玫瑰風.png,我想在活動開始時縮放它,並在方向改變後旋轉。縮放和旋轉imageview

問題是,當我縮放我的圖像(僅在onCreate),第一次(也是唯一的)使用旋轉方法後,我的視圖再次調整大小,我不知道爲什麼。

讓我發佈我的旋轉和縮放方法。玫瑰是我的ImageView實例(ImageView玫瑰)

請看看並張貼任何建議。我應該嘗試不同的方式來縮放我的ImageView嗎?

Thx任何幫助!

旋轉(貌似工作正常)

public void rotateRose(float angle){ 

      angle = 360 - angle; 
      Matrix matrix=new Matrix(); 
      rose.setScaleType(ScaleType.MATRIX); 
      pivX = rose.getDrawable().getBounds().width()/2; 
      pivY = rose.getDrawable().getBounds().height()/2; 
      matrix.preRotate((float) angle, pivX, pivY); 
      rose.setImageMatrix(matrix); 
} 

量表(發現source link

private void scaleImage(ImageView view, int boundBoxInDp) 
    { 
     // Get the ImageView and its bitmap 
     Drawable drawing = view.getDrawable(); 
     Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap(); 

     // Get current dimensions 
     int width = bitmap.getWidth(); 
     int height = bitmap.getHeight(); 

     // Determine how much to scale: the dimension requiring less scaling is 
     // closer to the its side. This way the image always stays inside your 
     // bounding box AND either x/y axis touches it. 
     float xScale = ((float) boundBoxInDp)/width; 
     float yScale = ((float) boundBoxInDp)/height; 
     float scale = (xScale <= yScale) ? xScale : yScale; 

     // Create a matrix for the scaling and add the scaling data 
     Matrix matrix = new Matrix(); 
     matrix.postScale(scale, scale); 

     // Create a new bitmap and convert it to a format understood by the ImageView 
     Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); 
     BitmapDrawable result = new BitmapDrawable(scaledBitmap); 
     width = scaledBitmap.getWidth(); 
     height = scaledBitmap.getHeight(); 

     // Apply the scaled bitmap 
     view.setImageDrawable(result); 

     // Now change ImageView's dimensions to match the scaled image 
     RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
     params.width = width; 
     params.height = height; 
     view.setLayoutParams(params); 

     //pivX = width/2; 
     // pivY = height/2; 
    } 

回答

1

好吧,我知道了!

更換

view.setImageDrawable(result); 

在Scale方法與

view.setImageBitmap(scaledBitmap); 

如果您正在尋找縮放和旋轉圖像視圖的方法:上述這些工作正常(與此變化不大)