2014-11-10 3 views
3

이미지 용 ViewPager를 만들려고합니다. 하지만 OutOfMemory 오류가 발생합니다. ") (재구성;" ViewPager Out of Memory + ImageView

나는 내가 비트 맵을 사용하거나 같은 뭔가를해야만 할 sholud 읽어 보시기 바랍니다 ... 내가 그것을 이해하지 VUT ...

내 코드 :

어댑터 :

public class app_info_Adapter extends PagerAdapter { 
private Activity act; 
int[] pictures = { 
       R.drawable.1, 
       R.drawable.2, 
       R.drawable.3, 
       R.drawable.4 
      }; 

public app_info_Adapter(Activity act) { 
    this.act = act; 
} 

@Override 
public void destroyItem(View collection, int position, Object o) { 
    View view = (View)o; 
    ((ViewPager) collection).removeView(view); 
    view = null; 
} 

@Override 
public void finishUpdate(View arg0) { 
    // TODO Auto-generated method stub 

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

@Override 
public Object instantiateItem(View context, int position) { 
    ImageView imageView = new ImageView(act); 
    imageView.setImageResource(pictures[position]); 
    LayoutParams imageParams = new LayoutParams(); 
    imageParams.height = LayoutParams.WRAP_CONTENT; 
    imageParams.width = LayoutParams.WRAP_CONTENT; 
    imageView.setLayoutParams(imageParams); 

    ((ViewPager) context).addView(imageView); 

    return imageView; 
} 

@Override 
public boolean isViewFromObject(View view, Object object) { 
    return view==((ImageView)object); 
} 

@Override 
public void restoreState(Parcelable arg0, ClassLoader arg1) { 
    // TODO Auto-generated method stub 
} 
@Override 
public Parcelable saveState() { 
    // TODO Auto-generated method stub 
    return null; 
} 
@Override 
public void startUpdate(View arg0) { 
    // TODO Auto-generated method stub 
} 
} 

활동 더 나은 성능과 더 OurOfMemory-오류를 얻을 수있는 방법

@Override 
protected void onStart() { 
    super.onStart(); 
    setContentView(R.layout.app_info); 


    ViewPager pager = (ViewPager)findViewById(R.id.viewpager); 
    app_info_Adapter myPagerAdapter = new app_info_Adapter(this); 
    pager.setAdapter(myPagerAdapter); 
} 

정말 고마워요!

+0

을 단 4 개 이미지? – Rob

+0

@Rob 예 ... 3 번째 이미지에서 오류가 발생했습니다 ... – Flo

+0

확인 : http://developer.android.com/training/displaying-bitmaps/index.html –

답변

3

읽고해야합니다

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

후 당김에서 읽을 내가 샘플을 변경 내 아래 함수를 사용 메모리가 부족

public static Bitmap decodeSampledBitmapFromDrawable(Resources res,int resId,int reqWidth, int reqHeight) { 

      // First decode with inJustDecodeBounds=true to check dimensions 
      final BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inJustDecodeBounds = true; 
      BitmapFactory.decodeResource(res, resId, options); 

      // Calculate inSampleSize 
      options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

      // Decode bitmap with inSampleSize set 
      options.inJustDecodeBounds = false; 
      return BitmapFactory.decodeResource(res, resId, options); 
     } 


public static int calculateInSampleSize(
      BitmapFactory.Options options, int reqWidth, int reqHeight) { 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 

      final int halfHeight = height/2; 
      final int halfWidth = width/2; 

      // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
      // height and width larger than the requested height and width. 
      while ((halfHeight/inSampleSize) > reqHeight 
        && (halfWidth/inSampleSize) > reqWidth) { 
       inSampleSize *= 2; 
      } 
     } 

     return inSampleSize; 
    } 
+0

도 참조하십시오 [ImageView 고해상도 이미지를 표시 할 수 없음] (https://stackoverflow.com/questions/26196479/imageview-not-able-to-show-high-resolution-images/26196757#26196757) – mmlooloo

+0

알았어. 그래서 ...하지만 아무것도 바뀌지 않습니다 ... 저는 3 번째 그림과 "메모리 부족"으로 넘어갑니다 ... – Flo

+0

4 가지 해결책을 제안 할 수 있습니다 : 1 - 'int inSampleSize = 1;'을 2 또는 4로 변경하십시오 ... 2- 비트 맵 재사용 3- 매니페스트에서'android : largeHeap = "true"'를 사용하십시오. 4- 조각 FragmentStatePagerAdapter를 사용하고 내용을 조각에 넣으십시오. – mmlooloo