【问题标题】:Trying to load bitmaps efficiently results in no image being displayed尝试有效地加载位图会导致不显示图像
【发布时间】:2016-02-26 22:08:15
【问题描述】:

所以我希望我的应用能够在列表视图中高效地显示图像,以防止出现 OOM 错误。我尝试通过将图像转换为位图来实现这一点,然后将其设置为框架布局中的背景,然后将此 xml 文件用作我的列表视图的布局。但是,它不显示任何内容。

这是适配器类。

public class CustomAdapter extends BaseAdapter {

String [] result;
Context context;
int [] imageId;
String [] peopleId;
private static LayoutInflater inflater=null;
public CustomAdapter(MyMainActivity mainActivity, String[] prgmNameList, int[] prgmImages, String[] peopleImages) {
    // TODO Auto-generated constructor stub
    result=prgmNameList;
    context= mainActivity;
    imageId=prgmImages;
    peopleId=peopleImages;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    FrameLayout frame;
    ImageView pic;
    TextView tv;
    TextView imgPeople;
  }
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.program_list_upcoming, null);
    rowView.setRotation(5);
    holder.frame=(FrameLayout) rowView.findViewById(R.id.frame);
    holder.pic = (ImageView) rowView.findViewById(R.id.frameBackPic);
    holder.imgPeople=(TextView) rowView.findViewById(R.id.peopleImage);
    holder.tv=(TextView) rowView.findViewById(R.id.textView1);
    holder.imgPeople.setText(peopleId[position]);
    holder.frame =(FrameLayout) rowView.findViewById(R.id.frame);
    holder.tv.setText(result[position]);

    Bitmap bitmap = decodeSampledBitmapFromResource(Resources.getSystem(), imageId[position],400, 130);
    BitmapDrawable myDrawable = new BitmapDrawable(this.context.getResources(), bitmap);
    holder.pic.setImageBitmap(bitmap);
    holder.pic.requestLayout();

    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();
        }
    });
    return rowView;
}

//This is to convert the imageIDs into a array of small bitmaps
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                     int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

//this adjusts the size of the bitmap as its needed accordign to the layout dimentsions
public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        final int halfHeight = (int)(height / 1.5);
        final int halfWidth = (int)(width / 1.5);
        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }
    return inSampleSize;
}

}

这是与之关联的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:rotation="5">
<!-- First frame layout is to have the picture underneath everything-->
<FrameLayout
    android:id="@+id/frame"
    android:layout_width="fill_parent"
    android:layout_height="130dp"
    android:background="#a8000000">

    <ImageView
        android:id="@+id/frameBackPic"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY" />

    <!-- First frame layout is to have the bar-->


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_gravity="fill"
        android:layout_marginTop="83dp"
        android:background="#b8000000">

        <LinearLayout
            android:id="@+id/buttonPanel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:orientation="horizontal"
            android:weightSum="14">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_centerHorizontal="true"
                android:layout_gravity="left"
                android:layout_weight="10"
                android:paddingLeft="10dp"
                android:textColor="#d1d1d1"
                android:textSize="18dp" />

            <TextView
                android:id="@+id/distance"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_gravity="right"
                android:layout_weight="1"
                android:paddingLeft="10dp" />

            <TextView
                android:id="@+id/peopleImage"
                android:layout_width="fill_parent"
                android:layout_height="30dp"
                android:layout_gravity="right"
                android:layout_weight="1"
                android:paddingLeft="10dp" />

        </LinearLayout>
    </FrameLayout>


    <!-- Second frame layout is to have the translucent bar acr0ss-->
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#a8000000">

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="17dp"
            android:layout_below="@+id/buttonPanel"
            android:layout_gravity="center_vertical"
            android:layout_marginBottom="5dp"
            android:background="#7e000000"
            android:paddingLeft="15dp" />
    </FrameLayout>
</FrameLayout>

任何关于如何解决它的见解将不胜感激!

【问题讨论】:

  • 这似乎是过早的优化。你做了什么导致OOM?
  • 这不是过早的优化,这是一种常见的OOM形式。他只是做得不对。或者做正确的适配器——他总是创建新的视图,从不重用 convertView
  • 如果你想要我的链接,我删除了我的答案并将链接放在这里stackoverflow.com/questions/35664298/…

标签: java android listview bitmap


【解决方案1】:

这里有多个问题。

1)您没有正确使用列表视图。如果不是 null,您的 getView 应该重用 convertView 并且只有在它为 null 时才创建一个新视图。你不是。这会使您的代码完全低效,根本无法回收。

2)您的解码功能没有解码位图。它仅解码位图的大小-inJustDecodeBounds 设置为 true。所以你总是会返回 null。

3) 您没有将位图保存在任何形式的缓存中。这意味着您将在每次调用 getView 时解码位图,这将在每个滚动事件上每行一次。实际上,你做的比根本不努力更糟糕。

4)您正在使用资源进行此操作。资源在启动时已经膨胀成完整的位图。如果正确完成,这种优化仅适用于图像文件。

5)您将位图粘贴在资源的可绘制对象中。这是在启动时为您完成的。

如果您使用的是从网上下载的位图文件或位图,则需要进行优化。您正在做的事情实际上是在降低性能并损害您的应用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多