2012-01-23 55 views
2

我有一個imageView,我基本上試圖製作一個溫度計風格的測量儀。我在下面添加了一張用於測試的圖片,我基本上正在嘗試旋轉圖片。第一張照片的圖像標準佈局。嘗試旋轉圖像後,寬度仍會填滿屏幕,但會縮小圖像以爲角落騰出空間,使實際圖像顯得更小。我希望圖像保持完全相同的大小,但是會根據傳遞給它的矩陣進行旋轉。Android:ImageView旋轉比例尺

Normal Image

Image rotated incorrectly, for me anyways

這裏是我使用的旋轉代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);   

    circle = (ImageView) findViewById(R.id.imageView1); 
    Bitmap myImg = BitmapFactory.decodeResource(getResources(), R.drawable.rotated); 

    Matrix matrix = new Matrix(); 
    matrix.postRotate(45); 

    Bitmap rot = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(), 
      matrix, true); 

    circle.setImageBitmap(rot);   
} 

所以,基本上,我需要紅/綠圈邊緣到達屏幕的邊緣。這可能與一個imageView或我需要寫一個自定義視圖,以允許東西離開屏幕?順便說一句,我實際上並不會畫出整個圈子,這只是一個例子來解決我的問題。

回答

3

您必須計算實際的新寬度和高度。 嘗試使用一些三角

newHeight = height/sin(alpha),其中alpha是你的旋轉角度......

newWidth = width/cos(alpha) 

則:

Bitmap rot = Bitmap.createBitmap(myImg, 0, 0, newWidth, newHeight , 
      matrix, true); 

不知道計算,我沒有檢查,但我認爲它是正確的,無論如何,這是一個很好的提示;)

+0

這就是我最初的想法。唯一的問題是,當我傳遞給createBitmap()的大小比容器大時,它強制關閉。 – SemperGumbee

+0

然後裁剪後創建... – Rotemmiz

+0

它掛在createBitmap()或當它試圖把它放在一個imageView? – Rotemmiz

2

不知道它是最佳的,但它的工作:只是裁剪旋轉位圖爲初始大小。

circle = (ImageView) findViewById(R.id.imageView1); 
Bitmap myImg = BitmapFactory.decodeResource(getResources(), R.drawable.rotated); 

Matrix matrix = new Matrix(); 
matrix.postRotate(45); 

Bitmap rot = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(), matrix, true); 

int off = (rot.getWidth() - myImg.getWidth())/2; 
Bitmap result = Bitmap.createBitmap(rot, off, off, myImg.getWidth(), myImg.getHeight()); 

circle.setImageBitmap(result);