繁体   English   中英

具有图像和文本的Listview的内存不足异常

[英]Out of memory Exception on Listview with images and text

我的应用程序内存不足,原因是其中包含一个ImageView和2个TextView的ListView。 图片很大,我将它们缩小到ListView的56x56dp中的single_row.xml ,但在其他屏幕上也使用较大的图片。

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

这是主要的activity.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>`

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

正如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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM