1

首先我为我糟糕的英语道歉。

我有一个适用于 Android 的应用程序,其中有一个产品目录活动。产品列表和图片中的数据在单独的例程中下载。Activity 的唯一职责就是加载数据产品及其各自的照片。

为了演示数据,我们创建了一个列表视图,并在代码后面添加了一个继承自 BaseAdapter 的自定义适配器类;

一些考虑。

  • 源图像的最大尺寸为 500 像素高度或宽度。

  • 平均最大文件大小为 45 kb。

  • 记录数在1200-2000之间

布局/catalogo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/gv_catalogo_gridprods"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:horizontalSpacing="5dp"
        android:numColumns="3"
        android:verticalSpacing="5dp"
        tools:listitem="@layout/catalogo_item" >
    </GridView>

</LinearLayout>

布局/catalogo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/fundo_imagem_catalogo"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:baselineAligned="false" >

        <LinearLayout
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/label_codigo"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/lb_catalogoitem_codigo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/label_000"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/label_descricao"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/lb_catalogoitem_descricao"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/label_descricao"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:id="@+id/iv_catalogoitem_imagem"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:baselineAligned="false"
        android:contentDescription="Imagem Produto"
        android:src="@drawable/icone_android" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:baselineAligned="false" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/label_codbarras"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/lb_catalogoitem_codbarras"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/label_789000000"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/label_embalagem"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/lb_catalogoitem_emb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/label_cx012"
                android:textStyle="bold" />

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

Adater 到 GridView

public class AdapterImagensCatalogo extends BaseAdapter {

    Context contexto;
    List<ItemCatalogo> catalogo;
    BitmapFactory.Options optBitMap;

    public AdapterImagensCatalogo(Context contexto, List<ItemCatalogo> catalogo) {
        this.contexto = contexto;
        this.catalogo = catalogo;
        this.optBitMap = new BitmapFactory.Options();
        this.optBitMap.inSampleSize = 10;

    }

    @Override
    public int getCount() {
        return catalogo.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(contexto).inflate(R.layout.catalogo_item, null);
        TextView tv = (TextView) convertView.findViewById(R.id.lb_catalogoitem_codigo);
        tv.setText(catalogo.get(position).getProduto());

        tv = (TextView) convertView.findViewById(R.id.lb_catalogoitem_descricao);
        tv.setText(catalogo.get(position).getDescricao());

        tv = (TextView) convertView.findViewById(R.id.lb_catalogoitem_codbarras);
        tv.setText(catalogo.get(position).getCodigoBarras());

        tv = (TextView) convertView.findViewById(R.id.lb_catalogoitem_emb);
        tv.setText(catalogo.get(position).getEmbalagem());

        ImageView imgV = (ImageView) convertView.findViewById(R.id.iv_catalogoitem_imagem);
        imgV.setImageBitmap(getMiniaturaImagem(catalogo.get(position).getPathArquivo()));

        return convertView;
    }


    private Bitmap getMiniaturaImagem(String path){
        return BitmapFactory.decodeFile(path, optBitMap);
    }


}

当用户浏览图像列表时会出现问题。尤其是当他如此快速地导航时。

这是 LogCat 错误。

    11-10 21:17:06.781: E/MessageQueue-JNI(2526): java.lang.OutOfMemoryError
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:651)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:407)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at com.cleber.myapp.adapter.AdapterImagensCatalogo.getMiniaturaImagem(AdapterImagensCatalogo.java:69)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at com.cleber.myapp.adapter.AdapterImagensCatalogo.getView(AdapterImagensCatalogo.java:62)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.widget.AbsListView.obtainView(AbsListView.java:2461)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.widget.GridView.makeAndAddView(GridView.java:1331)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.widget.GridView.makeRow(GridView.java:331)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.widget.GridView.fillDown(GridView.java:283)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.widget.GridView.fillGap(GridView.java:243)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5549)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3429)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.widget.AbsListView.onTouchEvent(AbsListView.java:3921)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.View.dispatchTouchEvent(View.java:7392)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2229)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.app.Activity.dispatchTouchEvent(Activity.java:2483)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.View.dispatchPointerEvent(View.java:7577)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4421)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4399)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4505)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:178)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.os.MessageQueue.nativePollOnce(Native Method)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.os.MessageQueue.next(MessageQueue.java:125)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.os.Looper.loop(Looper.java:124)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at android.app.ActivityThread.main(ActivityThread.java:4949)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at java.lang.reflect.Method.invokeNative(Native Method)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at java.lang.reflect.Method.invoke(Method.java:511)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
11-10 21:17:06.781: E/MessageQueue-JNI(2526):   at dalvik.system.NativeStart.main(Native Method)
11-10 21:17:06.781: D/AndroidRuntime(2526): Shutting down VM
11-10 21:17:06.781: W/dalvikvm(2526): threadid=1: thread exiting with uncaught exception (group=0x41a892a0)
11-10 21:17:06.781: E/AndroidRuntime(2526): FATAL EXCEPTION: main
11-10 21:17:06.781: E/AndroidRuntime(2526): java.lang.OutOfMemoryError
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:651)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:407)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at com.cleber.myapp.adapter.AdapterImagensCatalogo.getMiniaturaImagem(AdapterImagensCatalogo.java:69)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at com.cleber.myapp.adapter.AdapterImagensCatalogo.getView(AdapterImagensCatalogo.java:62)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.widget.AbsListView.obtainView(AbsListView.java:2461)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.widget.GridView.makeAndAddView(GridView.java:1331)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.widget.GridView.makeRow(GridView.java:331)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.widget.GridView.fillDown(GridView.java:283)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.widget.GridView.fillGap(GridView.java:243)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5549)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3429)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3921)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.View.dispatchTouchEvent(View.java:7392)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2229)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1978)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.app.Activity.dispatchTouchEvent(Activity.java:2483)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.View.dispatchPointerEvent(View.java:7577)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4421)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4399)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4505)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:178)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.os.MessageQueue.nativePollOnce(Native Method)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.os.MessageQueue.next(MessageQueue.java:125)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.os.Looper.loop(Looper.java:124)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at android.app.ActivityThread.main(ActivityThread.java:4949)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at java.lang.reflect.Method.invokeNative(Native Method)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at java.lang.reflect.Method.invoke(Method.java:511)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
11-10 21:17:06.781: E/AndroidRuntime(2526):     at dalvik.system.NativeStart.main(Native Method)

我试图用stackoverflow的以下discuçoes来解决这个问题,但是没有成功。

线性布局的延迟加载(图像+文本)

懒惰的图像加载器到 ListView

ListView 中图像的延迟加载

自定义无尽 ListView Android

懒惰的图像加载器到 ListView

https://github.com/thest1/LazyList

https://github.com/nostra13/Android-Universal-Image-Loader

缩放和加载位图导致 OOM (OutOfMemoryError) (Android)

4

2 回答 2

1

首先,您没有重复使用convertView适配器中的现有内容。您应该重复使用此视图,而不是每次都夸大一个新视图:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(contexto).inflate(R.layout.catalogo_item, parent, false);
    }
    ...
}

(注意:你知道这里的项目父项将是什么(ViewGroup parent参数)所以你应该通过使用inflate(id, parent, false);而不是告诉充气机inflate(id, null);- 你几乎不应该打电话inflate(id, null)

这应该可以解决您的问题,因为您不再创建数百个视图,而是回收现有视图。如果没有,您可以在替换位图时显式回收它们,如下所示:

ImageView imgV = (ImageView) convertView.findViewById(R.id.iv_catalogoitem_imagem);
// Obtain a reference to the old image
Drawable oldImage = imgV.getDrawable();
// Set the new image
imgV.setImageBitmap(getMiniaturaImagem(catalogo.get(position).getPathArquivo()));
// Recycle the old image
if (oldImage != null) {
    ((BitmapDrawable)oldImage).getBitmap().recycle();
}
于 2013-11-10T22:21:37.067 回答
1

1) 使用 ActivityManager.getMemoryClass() 和 ActivityManager.getLargeMemoryClass() 来验证分配给您的应用程序的近似值。

2) 你可以在清单中声明大堆大小 android:largeHeap-"true"

3) 来自 Google I/O 的演示文稿包含一些很好的答案 http://static.googleusercontent.com/external_content/untrusted_dlcp/www.google.com/en//events/io/2011/static/presofiles/memory_management_for_android_apps.pdf

4) 如果图像要以小于其原始形式的尺寸显示(如缩略图等,请在加载之前对其进行缩放。

Google Developers 的本教程应该会有所帮助: https ://www.youtube.com/watch?v=12cB7gnL6po&list=PLWz5rJ2EKKc_XOgcRukSoKKjewFJZrKV0&index=61

来自描述:如果您的目标位图大小要小得多,子采样可以加快加载时间并减少对内存中大位图的需求,尽管很高兴了解您无法获得特定的位图大小,而是权力-两次缩小尺寸。

于 2013-11-10T22:30:33.840 回答