2011-10-01 3 views
0

을 초과와 이미지 갤러리에 앞뒤로 내 응용 프로그램 충돌을 시간의 무리를 스크롤 할 때 상단 하나는 고해상도/당김 폴더에있는 이미지의 미리 정의 된 그룹에서 선택 될 것입니다안드로이드 사용자 정의 갤러리 위젯 java.lang.OutOfMemoryError와 함께 충돌 :</p> <p>java.lang.OutOfMemoryError와 : 비트 맵 크기의 비트 맵 크기가 VM 예산

  • : VM 예산

    나는 수직으로 두 개의 이미지를 표시하는 갤러리를 필요 초과합니다.

  • 하단 이미지는이 특정 이미지에 올바른 답변 (게임의 점수 페이지 제출) 또는 잘못 표시되면 줄이 표시된 빨간색 원으로 표시되면 녹색 체크 표시가됩니다.

    public class ImageAdapter extends BaseAdapter { 
        int mGalleryItemBackground; 
        private Context mContext; 
    
        public ImageAdapter(Context c) { 
         mContext = c; 
         TypedArray attr = mContext.obtainStyledAttributes(R.styleable.SubmitScoreGallery); 
         mGalleryItemBackground = attr.getResourceId(
           R.styleable.SubmitScoreGallery_android_galleryItemBackground, 0); 
         attr.recycle(); 
        } 
        @Override 
        public int getCount() { 
    
         return numQuestions; 
        } 
    
        @Override 
        public Object getItem(int position) { 
    
         return position; 
        } 
    
        @Override 
        public long getItemId(int position) { 
    
         return position; 
        } 
    
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
    
    
         //Setup a LinearLayout to display the second image 
         LinearLayout lLayout = new LinearLayout(this.mContext); 
         lLayout.setOrientation(LinearLayout.VERTICAL); 
         //Create the ImageView 
         ImageView imageView = new ImageView(this.mContext); 
    
    
         imageView.setImageResource(imageList.get(randOrder.get(position))); 
         imageView.setLayoutParams(new Gallery.LayoutParams(gDispW, gDispH)); 
         imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
         imageView.setBackgroundResource(mGalleryItemBackground); 
         lLayout.addView(imageView); 
         //Create the right/wrong image 
         ImageView imageViewBottom = new ImageView(lLayout.getContext()); 
         if (score.getScoreAtIndex(position)== 1){ 
          imageViewBottom.setImageResource(R.drawable.green_checkmark); 
         } 
         else{ 
          imageViewBottom.setImageResource(R.drawable.incorrect_circle); 
         } 
         imageViewBottom.setLayoutParams(new Gallery.LayoutParams(gDispW, gDispH)); 
         imageViewBottom.setPadding(gDispW/3, 0, gDispW/3, gDispH/2); 
         imageViewBottom.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
         //imageViewBottom.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
         lLayout.addView(imageViewBottom); 
    
         return lLayout; 
    
        } 
    } 
    

    randOrder는 이미지

    갤러리 5, 10 또는 15의 이미지를 보유 순서를 보유하는 배열이다 : 여기

는 BaseAdapter을 연장 내 코드 사용자가 선택한 질문 수에 따라

15 개의 이미지와 일관되게 충돌이 발생할 수 있습니다.

더 좋은 방법이 있나요?

내가 뭘 잘못하고 있니?

는 이미지 뷰를 매번 작성의 getView 방법에

답변

2

을 주셔서 감사합니다. 다음과 같이 convertView를 사용할 수 있습니다.

 ImageView imageView; 
     // if it's not recycled, initialize some attributes 
     if (convertView == null) { 
      imageView = new ImageView(mContext); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

또한 비트 맵에서 리샘플링을 사용할 수 있습니다.

+0

imageView, lLayout 및 imageViewBottom에 대해이 작업을 시도했지만 여전히 충돌합니다. –

+0

리샘플링 비트 맵을 사용해 보셨습니까?이 질문을 확인하십시오 : http://stackoverflow.com/questions/3331527/android-resize-a-large-bitmap-file-to-scaled-output-file –

0

댓글이 게시물의 19 : code.google.com Issue 8488이 문제를 해결했습니다.

내가해야 할 일은 res/drawable-nodpi 폴더를 만들고 여기에 호출 할 이미지를 넣는 것입니다.

이미지를 res/drawable에 저장하면 attr.recycle()이 올바르게 작동하지 않습니다. 왜 그런지 알아?

나는 Tim의 제안을 구현했으며 갤러리에서 앞뒤로 스크롤을 빠르게 할 것 같다. 고맙습니다!

편집 :

은 위의 대답은 마이 터치 4g 위해 일하지만, 프로그램이 여전히 G2로 추락했다. 그래서 저는 팀의 조언을 듣고 이미지를 다시 샘플링하기 시작했습니다. 나는이 게시물에서 Fedor의 대답을 사용하여 끝냈다. Strange out of memory issue while loading an image to a Bitmap object 그리고 나는 G2에서 메모리 문제를 보지 못했다.

관련 문제