2012-04-07 113 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