2012-12-30 26 views
-2

我有一個imageview,它將隨後包含一些隨機大小的圖像 我想用圖像填充imageview,但保留比例爲 (所以不要使用縮放類型FITXY或centercrop因爲它修剪我的圖像的一部分)將隨機大小的圖像合成到ImageView中

我該怎麼做?

在這個測試代碼,我有左,我的圖片

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 


    > 

    <ImageView 
     android:id="@+id/imgTest" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:adjustViewBounds="true" 
     android:scaleType="fitCenter" 

     android:src="@drawable/test" 
     /> 

回答

0

的右側空白處用一個矩陣我的屏幕的高度/寬度解決

private Bitmap resize(Bitmap bm, int w, int h) 
    { 
     int width = bm.getWidth(); 
     int height = bm.getHeight(); 
     int newWidth = w; 
     int newHeight = h; 
     float scaleWidth = ((float) newWidth)/width; 
     float scaleHeight = ((float) newHeight)/height; 

     Matrix matrix = new Matrix(); 
     matrix.postScale(scaleWidth, scaleHeight); 
     Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true); 

     return resizedBitmap; 
    } 
0

試試這個:

<ImageView 
    android:id="@+id/imgTest" 
    android:layout_width="wrap_content" 
    android:layout_height="100dp" 
    android:layout_alignParentLeft="true" 
    android:paddingRight="20dp" 
    android:scaleType="fitStart" 
    android:src="@drawable/test"/> 
+0

有的話o澄清會很好 – Najzero

相關問題