gridview通过小图显示大图内存溢出,哪位大神帮忙优化一下,新手啊~

骑着下毛驴 2013-02-12 12:56:05
XcActivity.java

package com.shi.sun.love;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class XcActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.xcxml);
GridView gridView=(GridView)findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
//单击GridView元素的响应
gridView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//弹出单击的GridView元素的位置
Intent intent =new Intent(XcActivity.this,IvActivity.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
}
private class ImageAdapter extends BaseAdapter{
private Context mContext;

public ImageAdapter(Context context) {
this.mContext=context;
}

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

@Override
public Object getItem(int position) {
return mThumbIds[position];
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
//定义一个ImageView,显示在GridView里
ImageView imageView;
if(convertView==null){
imageView=new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}else{
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}



}
//展示图片
private Integer[] mThumbIds = {
R.drawable.a1, R.drawable.a2,
R.drawable.a3, R.drawable.a4,
R.drawable.a5, R.drawable.a6,
R.drawable.a7, R.drawable.a8,
R.drawable.a9, R.drawable.a10,
R.drawable.background, R.drawable.a12,
R.drawable.a13, R.drawable.a14,
R.drawable.a15, R.drawable.a18,
R.drawable.a17,R.drawable.a19,
R.drawable.a11,R.drawable.a20,
};
}


IvActivity.java

package com.shi.sun.love;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

public class IvActivity extends Activity {
private ImageView imageView;
private Integer[] mThumbIds = {
R.drawable.a1, R.drawable.a2,
R.drawable.a3, R.drawable.a4,
R.drawable.a5, R.drawable.a6,
R.drawable.a7, R.drawable.a8,
R.drawable.a9, R.drawable.a10,
R.drawable.background, R.drawable.a12,
R.drawable.a13, R.drawable.a14,
R.drawable.a15, R.drawable.a18,
R.drawable.a17,R.drawable.a19,
R.drawable.a11,R.drawable.a20,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview);
imageView = (ImageView)findViewById(R.id.imageView);
Intent intent = getIntent();
Bundle bundle=intent.getExtras();
int positon=bundle.getInt("position");
int imageId = intent.getIntExtra("id",mThumbIds[positon]);
imageView.setImageResource(imageId);
}
}
...全文
51 3 点赞 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zqfddqr 2013-02-15
引用 2 楼 tcking_com 的回复:
引用 1 楼 zqfddqr 的回复:内存调大点实验没有 不管用呀,虚拟机内存调大试过了, 而且真机不可以调啊~
你虚拟机调大内存过了没有啊,现在虚拟机上过了在说。
  • 打赏
  • 举报
回复
zqfddqr 2013-02-12
内存调大点实验没有
  • 打赏
  • 举报
回复
骑着下毛驴 2013-02-12
引用 1 楼 zqfddqr 的回复:
内存调大点实验没有
不管用呀,虚拟机内存调大试过了, 而且真机不可以调啊~
  • 打赏
  • 举报
回复
相关推荐
发帖
Java
加入

4.8w+

社区成员

Java相关技术讨论
申请成为版主
帖子事件
创建了帖子
2013-02-12 12:56
社区公告
暂无公告