2013-02-14 54 views
-1

我正在研究一个应用程序,其中我使用了GridView,Gridview中的每个图像都有一个小的描述,当我们点击任何图像时,描述中,我测试过不同的设备,它的工作完美适用于MDPI和HDPI设备,但是当我在XHDPI设备(高分辨率设备)上测试时,出现内存不足错误。高分辨率设备上的GridView中的内存不足问题(XHDPI)

我无法理解有什么问题,这是我的活动: -

GridView gridView = (GridView) findViewById(R.id.grid_view); 

     // Instance of ImageAdapter Class 
     gridView.setAdapter(new ImageAdapter(getApplicationContext())); 

     /** 
     * On Click event for Single Gridview Item 
     * */ 
     gridView.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View v, 
        int position, long id) { 

       // Sending image id to FullScreenActivity 
       Intent i = new Intent(getApplicationContext(), TrafficDetails.class); 
       // passing array index 
       i.putExtra("id", position); 

       startActivity(i); 

      } 
     }); 

,这里是我的ImageAdapter在我使用图片来自我的绘制

public 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, 
      R.drawable.pic_7, R.drawable.pic_8, 
      R.drawable.pic_9, R.drawable.pic_10, 
      R.drawable.pic_11, R.drawable.pic_12, 
      R.drawable.pic_13, R.drawable.pic_14, 
      R.drawable.pic_15,R.drawable.pic_16, 
      R.drawable.pic_17,R.drawable.pic_18, 
      R.drawable.pic_19,R.drawable.pic_20, 
      R.drawable.pic_21,R.drawable.pic_22, 
      R.drawable.pic_23,R.drawable.pic_24, 
      R.drawable.pic_25,R.drawable.pic_26, 
      R.drawable.pic_27,R.drawable.pic_28, 
      R.drawable.pic_29,R.drawable.pic_30, 
      R.drawable.pic_31,R.drawable.pic_32, 
      R.drawable.pic_33,R.drawable.pic_34, 
      R.drawable.pic_35,R.drawable.pic_36, 
      R.drawable.pic_37,R.drawable.pic_38, 
      R.drawable.pic_39,R.drawable.pic_40, 
      R.drawable.pic_41,R.drawable.pic_42, 
      R.drawable.pic_43,R.drawable.pic_44, 
      R.drawable.pic_45,R.drawable.pic_46, 
      R.drawable.pic_47,R.drawable.pic_48, 
      R.drawable.pic_49,R.drawable.pic_50, 
      R.drawable.pic_51,R.drawable.pic_52, 
      R.drawable.pic_53, 
    }; 

    public String[] str_ary = { 

// Description of all images 
.................. 

    }; 

    // Constructor 
    public ImageAdapter(Context c){ 
     mContext = c; 
    } 

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

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

    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) {  

     ImageView imageView = new ImageView(mContext); 
     // TextView txt = new TextView(mContext); 
     imageView.setImageResource(mThumbIds[position]); 
     imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     imageView.setLayoutParams(new GridView.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); 
     // txt.setLayoutParams(new GridView.LayoutParams(500, 500)); 
     return imageView; 

请帮助我理清这一点,任何帮助将是明显的.....

谢谢

回答

4

尝试如下:

ImageView imageView = new ImageView(mContext); 
// TextView txt = new TextView(mContext); 
imageView.setImageResource(mThumbIds[position]); 
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
imageView.setLayoutParams(
     new GridView.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT)); 
Bitmap m_d = BitmapFactory.decodeResource(mContext.getResources(), 
            mThumbIds[p_position]); 
if (m_d != null) 
{ 
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(m_d, 100, 100, true); 
    imageView.setImageBitmap(resizedBitmap); 
} 
+0

这个工程。好人。但你为什么使用'matrix.postScale'? – sandalone 2013-07-03 16:06:31

+1

'matrix.postScale'用于缩小图像的比例尺寸。但它没有用在这个代码中,所以这里不需要.. :) – GrIsHu 2013-07-07 12:10:08

+0

哦,好的。谢谢! – sandalone 2013-07-08 16:51:52

0

这不是你的编码。好吧,但它没有语法错误,这是你没有想到和计划好的方式。 Android为您的应用程序分配一定量的内存以在运行时使用。假设有10MB的RAM(这取决于当前使用的内存量)假设您正在尝试加载10张图像,总共需要15MB RAM。 Android通常会尝试为您的应用提供额外的5MB,但并不总能得到保证。那时,Android只能说'Out of Memory',你的应用只会崩溃。

请参考this