带有图像和文本的Listview上的内存不足

时间:2014-05-17 16:25:31

标签: java android listview android-listview bitmap

由于ListView内部有ImageView和2 TextView,我的应用内存不足。图像很大,我将它们缩小到56x56dpsingle_row.xml的ListView,但它们也在其他屏幕上以更大的尺寸使用。

MainActivity将single_row调用到listview中并将其膨胀。然后单行重复每行的单独内容。每行都有一个图像和2个textviews。我知道有一种方法可以使用位图,但我不确定如何在代码中实现它。

这是主要活动.java代码

@Override public View getView(int i, View convertView, ViewGroup viewGroup) {
    View row=convertView;   
    if(row==null){
        LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=inflater.inflate(R.layout.single_row, viewGroup,false); 
    }
    TextView title = (TextView) row.findViewById(R.id.textView);
    TextView description = (TextView)row.findViewById(R.id.textView2);
    ImageView image = (ImageView)row.findViewById(R.id.imageView);

    SingleRow temp=list.get(i);
    title.setText(temp.title);
    description.setText(temp.description);
    image.setImageResource(temp.image);

    return row;
}

这是 singlerow.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:src="@drawable/placeholder1" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/imageView"
        android:layout_toRightOf="@+id/imageView"
        android:text="Large Text"    
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignParentRight="true"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />
    </RelativeLayout>`

任何解决方案都会有所帮助。我不确定如何在这个特定的结构中实现它。

1 个答案:

答案 0 :(得分:1)

正如Karakuri所说,将ImageView大小限制为56dpx56dp并不意味着没有加载整个位图。首先从文件(或资源)加载位图,然后缩小它。使用

//For resource
image.setImageBitmap(decodeSampledBitmapFromResource("android.resource://com.my.package/drawable/image_name"));
 //For file
image.setImageBitmap(decodeSampledBitmapFromResource(filepath));


public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {

    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 2;
    if (height >= reqHeight || width >= reqWidth) {
        inSampleSize *= 2;
    }
    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(String file,
        int reqWidth, int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(file, options);
}