7

好的,我想我有一个真正的问题需要改变。我在 Android 中实现了一个 gridView,按照 Android 开发者页面http://developer.android.com/resources/tutorials/views/hello-gridview.html中的说明逐步进行了 更改,以便在单击视图时进行更改,将返回位图。这一切都适用于 Galxy Note 和 Galaxy S2 等高级手机,以及 Galaxy ace 等不太先进的手机,甚至是 2 年前的一些糟糕的 htc。但由于某种原因,由于 OutOfMemory 问题,它在带有 ICS 的 Galaxy 3 上崩溃了。当在 gridview 上使用大约一半的图像时,它确实有效(虽然有点慢)。这对任何人都有意义吗?

这是我对 ImageAdapter 的实现:

public class ImageAdapter extends BaseAdapter {
private Context mContext;
private int mWidth;
private int mHeight;

public ImageAdapter(Context c, int width, int height) {
    mContext = c;
    mWidth = width;
    mHeight = height;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return mThumbIds[position];
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(mWidth, mHeight));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.third_world , R.drawable.annoyinggirl,
        R.drawable.asianfather, R.drawable.awkawespeng,
        R.drawable.awkpeng, R.drawable.blankdad,
        R.drawable.collegesenior, R.drawable.courage,
        R.drawable.frog, R.drawable.frynotsure,
        R.drawable.goodguygreg, R.drawable.scumbag,
        R.drawable.raptor,R.drawable.keano,
        R.drawable.successkid, R.drawable.wonka,
        R.drawable.braceyourselves, R.drawable.onedoesnotsimply,
        R.drawable.firstworldproblem, R.drawable.amitheonlyone,
        R.drawable.badluckbrian, R.drawable.interestingman,
        R.drawable.toodamnhigh, R.drawable.def    
};

}

这是我主要的调用函数:

  private void changeToTemplateView(){
    setContentView(R.layout.templates);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(context, (int)(dstWidth * 1.4), (int)(dstHeight * 1.4)));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            Options opts = new Options();
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), (int) parent.getItemIdAtPosition(position), opts);

            //do stuff with bitmap          
        }
    });
}

这是gridview的xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"

/>

如果有人知道我在这里可能做错了什么,我将永远感激不尽

编辑:在崩溃之前,我在日志中收到一个错误:“FimgApiStretch:stretch failed”。此外,gridview 中的滚动不起作用。这与即使没有足够的缩略图导致滚动,应用程序仍然会崩溃的事实无关

4

3 回答 3

2

尝试用 match_parent 替换 fill_parent 因为根据我的信息,属性值 fill_parent 在 android 2.2 及更高版本上已弃用希望这会有所帮助

你可以看到这个链接http://developer.android.com/training/displaying-bitmaps/load-bitmap.html 我希望这会对你有所帮助

于 2012-06-12T15:07:44.263 回答
2

至于“为什么它会在 Galaxy 3 而不是在劣质手机上崩溃”的问题,答案是“我不知道”。但是,事实证明我一直在滥用gridview。对缩略图和整个图像使用相同的图像是一个坏主意。我所做的(这可能只不过是一种解决方法,但它有效)是使用 2 组图像,小的和全尺寸的 ines。这样做是这样的

设置缩略图:

private Integer[] mThumbIds = {
        R.drawable.third_world__small , R.drawable.annoyinggirl__small,
        R.drawable.asianfather__small, R.drawable.awkawespeng__small,
        R.drawable.awkpeng__small, R.drawable.blankdad__small,
        R.drawable.collegesenior__small, R.drawable.courage__small,
        R.drawable.frog__small, R.drawable.frynotsure__small,
        R.drawable.goodguygreg__small, R.drawable.scumbag__small,
        R.drawable.raptor__small,R.drawable.keano__small,
        R.drawable.successkid__small, R.drawable.wonka__small,
        R.drawable.braceyourselves__small, R.drawable.onedoesnotsimply__small,
        R.drawable.firstworldproblem__small, R.drawable.amitheonlyone__small,
        R.drawable.badluckbrian__small, R.drawable.interestingman__small,
        R.drawable.toodamnhigh__small, R.drawable.def__small    
};

public View getView(int position, View convertView, ViewGroup parent) {
    //clearAllResources();
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(mWidth, mHeight));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

然后,创建另一个数组:

private Map<Integer, Integer> mThumbIdToFullSizeId = new HashMap<Integer,Integer>();
public ImageAdapter(Context c, int width, int height) {
    mContext = c;
    mWidth = width;
    mHeight = height;
    mThumbIdToFullSizeId.put(R.drawable.third_world__small, R.drawable.third_world);
    mThumbIdToFullSizeId.put(R.drawable.annoyinggirl__small,R.drawable.annoyinggirl);
    mThumbIdToFullSizeId.put(R.drawable.asianfather__small, R.drawable.asianfather);
    mThumbIdToFullSizeId.put(R.drawable.awkawespeng__small, R.drawable.awkawespeng);
    mThumbIdToFullSizeId.put(R.drawable.awkpeng__small, R.drawable.awkpeng);
    mThumbIdToFullSizeId.put(R.drawable.blankdad__small, R.drawable.blankdad);
    mThumbIdToFullSizeId.put(R.drawable.collegesenior__small, R.drawable.collegesenior);
    mThumbIdToFullSizeId.put(R.drawable.courage__small, R.drawable.courage);
    mThumbIdToFullSizeId.put(R.drawable.frog__small, R.drawable.frog);
    mThumbIdToFullSizeId.put(R.drawable.frynotsure__small, R.drawable.frynotsure);
    mThumbIdToFullSizeId.put(R.drawable.goodguygreg__small, R.drawable.goodguygreg);
    mThumbIdToFullSizeId.put(R.drawable.scumbag__small, R.drawable.scumbag);
    mThumbIdToFullSizeId.put(R.drawable.raptor__small, R.drawable.raptor);
    mThumbIdToFullSizeId.put(R.drawable.keano__small, R.drawable.keano);
    mThumbIdToFullSizeId.put(R.drawable.successkid__small, R.drawable.successkid);
    mThumbIdToFullSizeId.put(R.drawable.wonka__small, R.drawable.wonka);
    mThumbIdToFullSizeId.put(R.drawable.braceyourselves__small, R.drawable.braceyourselves);
    mThumbIdToFullSizeId.put(R.drawable.onedoesnotsimply__small, R.drawable.onedoesnotsimply);
    mThumbIdToFullSizeId.put(R.drawable.firstworldproblem__small, R.drawable.firstworldproblem);
    mThumbIdToFullSizeId.put(R.drawable.amitheonlyone__small, R.drawable.amitheonlyone);
    mThumbIdToFullSizeId.put(R.drawable.badluckbrian__small, R.drawable.badluckbrian);
    mThumbIdToFullSizeId.put(R.drawable.interestingman__small, R.drawable.interestingman);
    mThumbIdToFullSizeId.put(R.drawable.toodamnhigh__small, R.drawable.toodamnhigh);
    mThumbIdToFullSizeId.put(R.drawable.def__small, R.drawable.def);
}

然后,对于 getview 函数:

public long getItemId(int position) {

    return mThumbIdToFullSizeId.get(mThumbIds[position]);
}

一切都会对记忆更加友好。我给 Muhannad 打勾,因为它给了我指向 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html的链接,这对我有很大帮助

于 2012-06-16T16:41:28.603 回答
1

我有一个类似的问题。我的应用程序始终运行良好,没有任何问题,但在运行 Android 4 的设备上开始因OutOfMemory错误而崩溃。特别是,它在加载 GalleryView 适配器时崩溃。在阅读了许多官方文档(http://android.developer.com)的页面后,我了解到内存的使用与旧版本的 SO 不同,但最终解决了清单文件的问题。我将 Android 3.0 设置为项目构建目标,添加 android:targetSdkVersion="15",将 android:largeHeap="true" 添加到应用程序标签,最后添加支持屏幕 android:anyDensity="false"

希望这也可以帮助你!

于 2012-08-11T22:41:58.870 回答