2012-04-07 107 views
0

在我的活動中,我必須展示一些圖片。我在我的活動中使用了gridview,每行應該顯示4個圖像(每個圖像的寬度是屏幕的25%)。我需要減小寬度相同的圖像的高度。Android,如何降低圖像適配器中圖像的高度?

所以,在我的UI中我有一個gridview和我的imageAdapter將圖片排列到屏幕上。這是適配器的代碼:

public class ContestantsPhotoAdapter extends BaseAdapter { 

    private LayoutInflater myInflater; 
    private Bitmap[] bitmapList; 


    public ContestantsPhotoAdapter(Context c) { 
     myInflater = LayoutInflater.from(c); 
    } 

    public void setData(Bitmap[] bmImages){ 
     bitmapList = bmImages; 

     Log.i("ContestantsPhotoAdapter", "Images passed to the adapter."); 
    } 

    @Override 
    public int getCount() { 
     return bitmapList.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent){ 
     ViewHolder holder; 

     if (convertView == null) { 
      convertView = myInflater.inflate(R.layout.grid_contestantsphoto, null); 
      holder = new ViewHolder(); 
      holder.ivIcon = (ImageView) convertView.findViewById(R.id.imvContestantsPhoto_icon); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.ivIcon.setImageBitmap(bitmapList[position]); 

     return convertView; 
    } 

    static class ViewHolder { 
     ImageView ivIcon; 
    } 
} 

grid_contestantsphoto.xml是:

<?xml version="1.0" encoding="utf-8"?> 

<ImageView 
    xmlns:android     = "http://schemas.android.com/apk/res/android" 
    android:id      = "@+id/imvContestantsPhoto_icon" 
    android:layout_width   = "wrap_content" 
    android:layout_height   = "100dip" 
    android:contentDescription  = "@string/menu_contentdescription" /> 

我的問題是,當我改變android:layout_height = "100dip"它不生效,始終高度和以前一樣。

請告訴我如何縮小尺寸。謝謝。

回答

0

以供將來參考。我改變了適配器代碼這樣:

public void setData(Bitmap[] bmImages){ 
     bitmapList = bmImages; 

     for(int i=0; i < bitmapList.length; i++){ 
      bitmap = bitmapList[i]; 
      bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getWidth(), false); 
      bitmapList[i] = bitmap; 
     } 
     Log.i("ContestantsPhotoAdapter", "Images passed to the adapter."); 
    } 

現在,圖像的高度是寬度爲相同。

0

這真的適合我!請試試這個!

<ImageView 
     android:id="@+id/productImage" 
     android:layout_width="100px" 
     android:layout_height="100px" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:scaleType="fitCenter" 
     android:layout_marginRight="10dip" 
     android:adjustViewBounds="true" 
     android:src="@drawable/loading" /> 
+0

謝謝,但不幸沒有奏效。仍然高度與原來一樣。 – Hesam 2012-04-07 18:35:10

+0

再次感謝,但我認爲這不是正確的方法,因爲沒有任何影響。 – Hesam 2012-04-07 18:53:27