2012-08-28 38 views
0

我正確地獲取了我下載的可繪製圖形,並且顯示正確。我只想在我的ImageView上顯示圖像的頂部144x284dip。相反,當圖像下載時,ImageView將自身調整爲可繪製的寬度和高度。我試着修改layoutparams和minimumwidth/minimumheight,都無濟於事。獲取下載的可繪製圖像時ImageView自動調整

有什麼我可以做的強制ImageView留在144x284?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="284dip" 
    android:layout_height="144dip" > 

    <ImageView 
     android:id="@+id/imageViewLogo" 
     android:layout_width="284dip" 
     android:layout_height="144dip" /> 

</LinearLayout> 
+0

什麼是ImageView的佈局管理器?你可以發佈佈局XML文件嗎? – RajV

+0

你在那裏;-) – razielsarafan

回答

1

如果我理解正確的話,你想要做的兩件事情:

  1. 修復的ImageView的大小284dip X 144dip。
  2. 僅顯示未縮放圖像的左上角部分。

做第二部分將需要使用「矩陣」刻度類型,並將刻度設置爲1.0,將變換因子設置爲0.0。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="284dip" 
     android:layout_height="144dip" 
     android:scaleType="matrix" 
     android:src="@drawable/ic_launcher" /> 
</LinearLayout> 

默認情況下,縮放因子爲1,翻譯爲0.因此,您不必做任何其他操作。如果你希望有不同的縮放和翻譯,你可以編寫這樣的代碼。

void showImage(Bitmap bm) { 
    float scaleFactor = ...; 
    float transform = ...; 

    imageView.setImageBitmap(bm); 
    final Matrix matrix = new Matrix(); 
    matrix.setScale(scaleFactor, scaleFactor); 
    matrix.setTranslate(transform, transform); 
    imageView.setImageMatrix(matrix); 
} 
1

好吧,我解決了它。

我只是這樣做:

Bitmap newBitmap = Bitmap.createBitmap(bm, 0, 0, 144, 284); 

然後將其設置爲ImageView的。