我有一个小程序,您可以点击图像然后更改。但这个小应用程序具有极高的内存使用率,我无法弄清楚原因:(。或者它是否正常,我必须实现更多的方法来节省内存?...
当我在手机上点击12次时,内存使用量超过120兆字节。 图像都在200kb左右。
的活动:
public class Activity_easy extends Activity {
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.easy_layout);
Button btn_back = (Button) findViewById(R.id.btn_back);
btn_back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),
MainActivity.class));
finish();
}
});
gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
final ImageAdapter ImageAdapter = new ImageAdapter(this);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Toast.makeText(gridView.getContext(), "Position: " + position, Toast.LENGTH_SHORT).show();
ImageAdapter.switchImage(position, view);
}
});
}
}
ImageAdapter:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Integer[] pictures;
public ImageAdapter(Context c) {
mContext = c;
List<Integer> pictureList = new ArrayList<Integer>();
for(int i=0; i<6; i++) {
pictureList.add(i);
pictureList.add(i);
}
Collections.shuffle(pictureList);
pictures = (Integer[]) pictureList.toArray(new Integer[0]);
}
public int getCount() {
return pictures.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 ImageView getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
int size = mContext.getResources().getDimensionPixelSize(R.dimen.gridview);
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new LinearLayout.LayoutParams(size, size));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(R.drawable.card_back);
return imageView;
}
public void switchImage(int position, View view){
ImageView img = (ImageView) view;
int piece = pictures[position];
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inSampleSize = 3;
Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(),mThumbIds[piece],options);
img.setImageBitmap(icon);
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.pic_1,
R.drawable.pic_2,
R.drawable.pic_3,
R.drawable.pic_4,
R.drawable.pic_5,
R.drawable.pic_6,
};
}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="3"
android:stretchMode="columnWidth"
android:padding="5dp"
android:verticalSpacing="5dp"
android:horizontalSpacing="10dp"
android:gravity="center_vertical"
android:layout_marginBottom="100dp"/>
<Button
android:id="@+id/btn_back"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="@string/back_button" />
</RelativeLayout>
答案 0 :(得分:0)
这与@Rakshith Ravi说的一样,可能是因为您在应用中将图像加载到图像顶部。我假设你为你发布的每个图片设置onClickListeners,对吗?这意味着您对一个图像所做的任何修改都意味着您必须对其他图像执行相同的操作。此外,您将这些图像中的一些堆叠在一起。由于您没有删除以前的图像或从数据库访问图像,因此这将占用您手机的大量内存。如果您有任何疑问,请随时发表评论。
此致
阿尼
答案 1 :(得分:0)
如果要删除视图,请按照以下步骤操作。我会严重压缩你的代码以显示这一点。
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout_id_here);
//I am assuming your imageview is the first item in the relative layout
layout.removeViewAt(0);
ImageView image = new ImageView(getActivity());
image.setImageResource(R.drawable.whateverImage);
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
layout.removeViewAt(0);
ImageView image = new ImageView(getActivity());
image.setImageResource(R.drawable.whateverImage);
}
});
请编辑此内容以使其适合您的情况。如果您有其他问题,请与我们联系。我很乐意帮忙。
此致
阿尼