2015-06-19 57 views
1

我有一個小程序,在那裏你可以點擊一個圖像,然後將其改變。但這個小應用程序有一個非常高的內存使用情況,我想不通爲什麼:(。或者只是正常的,我要實現對內存很多方法,節約?...爲什麼這麼高的內存使用率?

當我點擊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

這可能是因爲您加載所有的12個圖像到內存中,並沒有處置它們趕走正確 –

+0

要創建2個圖像適配器 – Eoin

+0

此行是極不尋常的:'最後ImageAdapter ImageAdapter =新的ImageAdapter(this);' – MaxZoom

回答

0

這就像@Rakshith拉維說,可能是因爲您在您的應用程序圖像上加載圖像。我假設您爲每個發佈的圖片設置onClickListeners,對吧?這意味着無論你如何修改一個圖像,都意味着你必須對其他圖像做同樣的事情。此外,您還可以將這些圖像中的幾個堆疊在一起。由於您沒有刪除以前的圖像或從數據庫訪問圖像,因此會使用很多手機的內存。如果您有任何問題,請隨時發表評論。

真誠,

阿尼

+0

那麼我怎麼才能合適地刪除以前的圖片呢?我用來構建這些代碼的大多數示例都與我的一樣。 – h0ppel

+0

您可以使用removeViewAt函數。我將添加另一篇文章,告訴你如何做到這一點。 –

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); 
    } 
}); 

請編輯它以便它適合您的情況。如果您還有其他問題,請告訴我。我會很樂意提供幫助。

真誠,

阿尼

相關問題