android - ViewPager sin memoria+ImageView
out-of-memory android-pageradapter (1)
Intento hacer ViewPager para imágenes. Pero obtengo un OutOfMemory-Error.
Leí que debo usar Bitmaps o hacer algo así como "reorganizar ();" ... pero no lo entiendo ...
Mi código:
Adaptador:
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
}
}
Actividad
@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);
}
¿Cómo se puede obtener un mejor rendimiento y sin OurOfMemory-Error?
¡Muchas gracias!
debes leer:
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
luego use mi función siguiente que cambié la muestra para leer desde dibujable:
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;
}