Android:在ViewPager中使用GridView显示156张图片

3

我希望使用Viewpager展示156个缩略图,分为18页,每页有9个缩略图。我已经按照以下方式实现:

Index_gridview类:

public void init() 
{  
    PageCount = (int) Math.ceil(mThumbIds.length / PAGE_SIZE);  
    mLists = new ArrayList<GridView>();  

    for (int i = 0; i < PageCount; i++) 
    {  
        GridView gv = new GridView(this);  
        gv.setAdapter(new MyGridViewAdapter(this, mStrs, mThumbIds, i, icon_dimension));  
        gv.setGravity(Gravity.CENTER);  
        gv.setClickable(true);  
        gv.setFocusable(true);  
        gv.setNumColumns(NUMOFCOLUMN);  
        gv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        gv.setHorizontalSpacing(gridview_horizontal_padding);
        gv.setVerticalSpacing(gridview_vertical_padding);
        mLists.add(gv);  
    }         
    page_number.setText("1/"+PageCount);
}  

GridViewAdapter 类:

public class MyGridViewAdapter extends BaseAdapter 
{   
    private Animation zoom     = null;
    // view
    int icon_width, icon_height;
    Typeface tf;
    private Context mContext;  
    private List<String> mLists;  
    public static final int PAGE_SIZE = 9; // showing how many items in 1 page  

    public MyGridViewAdapter(Context pContext, String[] pStrs, Integer[] image_ref, int page, int width) 
    {  
        this.mContext = pContext;  
        mLists = new ArrayList<String>();  
        int i = page * PAGE_SIZE;  
        int end = i + PAGE_SIZE;  
        while ((i < image_ref.length) && (i < end)) 
        {  
            mLists.add(pStrs[i]); 
            i++;  
        }  

        icon_width = width;
        icon_height = icon_width;

        tf = Typeface.createFromAsset(mContext.getAssets(),"fonts/HomegirlKiddo.ttf");   
    }  

    @Override  
    public int getCount() 
    {           
        return mLists.size();  
    }  
    @Override  
    public Object getItem(int position) 
    {           
        return mLists.get(position);  
    }  
    @Override  
    public long getItemId(int position) 
    {     
        return position;  
    }  

    @Override  
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ViewHolder holder;
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.menugrid, parent, false);
            holder = new ViewHolder();

            holder.img = (ImageView) convertView.findViewById(R.id.imageicon);
            convertView.setTag(holder);
        } 
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }


        final int myNum = Integer.parseInt(mLists.get(position)) -1;
        ImageView imageView = holder.img;

        try
        {
            imageView.setImageBitmap(decodeSampledBitmapFromResource(mContext.getResources(), mThumbIds[myNum], icon_width, icon_width));   
        }
        catch (OutOfMemoryError e)
        {
            ((Index_Gridview)mContext).custom_toast("System Out-of-Error! Restarting...");
            ((Index_Gridview)mContext).restart_app();  
        }


        imageView.setOnClickListener(new OnImageClickListener(myNum));       
        return convertView;
    }

private 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.inPreferredConfig = Bitmap.Config.ARGB_8888;
    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);
}

private int calculateInSampleSize (BitmapFactory.Options options, int reqWidth, int reqHeight) 
{
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) 
    {   
        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }   
    return inSampleSize;
} 

class ViewHolder 
{
    ImageView img;
}

// References to our images in res > drawable
public Integer[] mThumbIds =  new Integer[] 
    {
        R.drawable.t_aaa, 
        R.drawable.t_bbb, 
                    ....listing of 156 image references here
    }

问题:

我已经尝试使用“decodeSampledBitmapFromResource”导入缩略图。原始缩略图大约为75KB。

但这很容易导致OOM错误。如何优化代码以减轻OOM错误?

谢谢!


你在 imageView.setImageBitmap(decodeSampledBitmapFromResource(mContext.getResources(), mThumbIds[myNum], icon_width, icon_width)) 中传递了Image Height参数在哪里? - M D
好的,现在请发布您的decodeSampledBitmapFromResource(...)方法。 - M D
添加了,谢谢~~确实是标准解码方法。 - pearmak
每个文件大小约为75KB,尺寸为400*400。 - pearmak
请问您能告诉我如何做吗? - pearmak
显示剩余7条评论
3个回答

0

0

你必须自己进行一些内存管理 - 毫无疑问。你可以回收屏幕外的页面,并在需要时重新加载它们。LRUCache非常好,但它会使用所有图像的内存。

你的画廊并不是很大,你可以考虑将其保存在本地内存中。对于hdpi 800x480屏幕,你需要30MB到HD屏幕的150MB。使用JNI和C调用,你可以请求安卓硬件中安装的整个内存(512-1GB)。这种内存不是Java堆的一部分,因此你不必担心应用程序中其他事物缺乏内存。

当然,这不是完美的解决方案,因为占用超过系统提供的内存将导致分配失败。你必须注意这一点。

链接:http://code.google.com/p/native-buffer/

NativeBuffer是一种缓存,它使用C分配的内存来存储图像数据。它比重新加载图像或将它们缓存到SD卡上要快得多。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接