由于写项目时是在Fragment中code的,故在采用的是fragment:
第一种使用SimpleAdapter
新建一个主activity_dzzy.xml如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<GridView
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"<!--定义每行显示的列数,auto_fit自定义-->
/>
</LinearLayout>
建立activity_dzzy_item2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_gravity="center"
android:scaleType="fitXY" />
</LinearLayout>
在对应的fragment中
public class DzzyFragment extends Fragment {
private GridView gridView;
private int[] imgs = { };//图片资源
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_dzzy, container, false);
setHasOptionsMenu(true);
gridView = (GridView) view.findViewById(R.id.gridView);
ArrayList<HashMap<String, Object>> imagelist = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < imgs.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("img", imgs[i]);
imagelist.add(map);
}
SimpleAdapter sa = new SimpleAdapter(getActivity()
.getApplicationContext(), imagelist,
R.layout.activity_dzzy_item2, new String[] { "img" },
new int[] { R.id.img });
gridView.setAdapter(sa);
return view;
}
}
但是由于该方法对drawable没有采用任何的处理,如果图片数量多或者比较大时占用内存比较严重,严重时可能oom。
第二种方法采用自定义的adapter,并对引用的bitmap进行加工,并及时回收
引用上面的两个xml
修改Fragment
private GridView gridView ;
<pre name="code" class="java"><span style="white-space:pre"> </span>private int[] imgs = { };//图片资源
public List<Bitmap> bitmaps=new ArrayList<Bitmap>();public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.activity_dzzy, container, false);setHasOptionsMenu(true);gridView = (GridView) view.findViewById(R.id.gridView);for(int i:imgs){bitmaps.add(GetBitmap.getBitmap(getActivity().getApplicationContext(), i));}MySimpleAdapter msa=new MySimpleAdapter(bitmaps);gridView.setAdapter(msa);return view;}@Overridepublic void onDestroy() {/* * 回收本地图片资源 * */for(Bitmap bitmap:bitmaps){if(bitmap != null && !bitmap.isRecycled()){bitmap.recycle();bitmap=null;}} System.gc();super.onDestroy();}//适配器public class MySimpleAdapter extends BaseAdapter{private LayoutInflater mInflater;private List<Bitmap> bitmaps;public MySimpleAdapter(List<Bitmap> bitmaps) {this.mInflater = LayoutInflater.from(getActivity().getApplicationContext());this.bitmaps=bitmaps;}@Overridepublic int getCount() {return bitmaps.size();}@Overridepublic Object getItem(int arg0) {return bitmaps.get(arg0);}@Overridepublic long getItemId(int arg0) {return arg0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder = null;if (convertView == null) {holder = new ViewHolder();convertView = mInflater.inflate(R.layout.activity_dzzy_item2, null); holder.image = (ImageView) convertView.findViewById(R.id.img); convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}holder.image.setImageBitmap(bitmaps.get(position));return convertView;}}class ViewHolder {public ImageView image;}
新建类GetBitmap:通过图片id获取图片的bitmap
public class GetBitmap {
public static Bitmap getBitmap(Context context,int resId){
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// 获取资源图片
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
}
}
这样能有效的减少图片内存的占用,能灵活的对图片进行调整、回收,建议采用第二种方法。