好的,我想我有一个真正的问题需要改变。我在 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 中的滚动不起作用。这与即使没有足够的缩略图导致滚动,应用程序仍然会崩溃的事实无关