我创建了一个GridView
,如下所示
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="application.example.com.fragment_practice.MainActivity"
tools:showIn="@layout/app_bar_main">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="@dimen/grid_view_width"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="@dimen/dp_10" />
</RelativeLayout>
以下是我的MainActivity.java
课程
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
}
}
以下是ImageAdapter.java
类
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Constructor
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// 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) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(180, 180));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(15, 15, 15, 15);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6,
R.drawable.sample_7, R.drawable.sample_8,
R.drawable.sample_13, R.drawable.sample_9,
R.drawable.sample_10, R.drawable.sample_11,
R.drawable.sample_12, R.drawable.sample_13,
R.drawable.sample_14, R.drawable.sample_16,
R.drawable.sample_16, R.drawable.sample_17,
R.drawable.sample_18, R.drawable.sample_19,
R.drawable.sample_20, R.drawable.sample_21,
R.drawable.sample_22, R.drawable.sample_23
};
}
在滚动之前,每件事情都很完美。
以下是我在向上和向下滚动GridView
3-4次时的例外情况。
致命的例外:mai
处理:application.example.com.fragment_practice,PID:18917 java.lang.OutOfMemoryError:无法分配4915212字节 分配2666656个空闲字节和2MB直到OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) 在android.graphics.BitmapFactory.nativeDecodeAsset(本机方法) 在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856) 在 android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675) 在 android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2230) 在 android.content.res.Resources.loadDrawableForCookie(Resources.java:4284) 在android.content.res.Resources.loadDrawable(Resources.java:4158) 在android.content.res.Resources.getDrawable(Resources.java:2043) 在android.content.res.Resources.getDrawable(Resources.java:2025) 在android.content.Context.getDrawable(Context.java:464) 在android.widget.ImageView.resolveUri(ImageView.java:827) 在android.widget.ImageView.setImageResource(ImageView.java:434) 在 application.example.com.fragment_practice.ImageAdapter.getView(ImageAdapter.java:48) 在android.widget.AbsListView.obtainView(AbsListView.java:2937) 在android.widget.GridView.makeAndAddView(GridView.java:1463) 在android.widget.GridView.makeRow(GridView.java:366) 在android.widget.GridView.fillUp(GridView.java:407) 在android.widget.GridView.fillGap(GridView.java:282) 在android.widget.AbsListView.trackMotionScroll(AbsListView.java:7307) 在android.widget.AbsListView.scrollIfNeeded(AbsListView.java:4400) 在android.widget.AbsListView.onTouchMove(AbsListView.java:5796) 在android.widget.AbsListView.onTouchEvent(AbsListView.java:5624) 在android.view.View.dispatchTouchEvent(View.java:10011) 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2833) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2504) 在 android.widget.AbsListView.dispatchTouchEvent(AbsListView.java:5561) 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519) 在 com.android.internal.policy.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2840) 在 com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1853) 在android.app.Activity.dispatchTouchEvent(Activity.java:3061) 在 android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63) 在 android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63) 在 com.android.internal.policy.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2801) 在android.view.View.dispatchPointerEvent(View.java:10246) 在 android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5447) 在 android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:5283) 在 android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4721) 在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl。
编辑1:任何人都可以解释一下为什么我在滚动3-4次上下网格视图时没有加载这个问题?
答案 0 :(得分:2)
使用Glide加载图片: -
在您的gradle中添加依赖
compile 'com.github.bumptech.glide:glide:3.7.0'
加载图片: -
Glide.with(mContext).load(mThumbIds[position]).placeholder(R.drawable.user_placeholder).into(imageView);
如果这不起作用试试这个, 在Android块中添加应用程序的build.gradle
dexOptions {
incremental true
javaMaxHeapSize "4g"
jumboMode = true
}
答案 1 :(得分:0)
我建议您使用名为Glide的库,使用此库在gradle文件中添加以下内容
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
}
然后在res文件夹中创建一个名为drawable-nodpi
的文件夹。将所有图像放在那里,不要忘记从drawable-hdpi
和其他可绘制文件夹中删除图像。确保图像仅存在于drawable-nodpi
文件夹中。然后,而不是imageView.setImageResource(mThumbIds[position]);
,请执行以下操作
Glide.with(mContext)
.load(mThumbIds[position])
.diskCacheStrategy( DiskCacheStrategy.NONE )
.skipMemoryCache( true )
.into(imageView);
让我知道它是否有效。