gpt4 book ai didi

android - 内存不足 + 位图 + ImageView + ViewPager

转载 作者:行者123 更新时间:2023-11-29 00:10:48 25 4
gpt4 key购买 nike

我正在为我的应用开发一个简单的照片库。但是我遇到了一些非常烦人的内存泄漏,而且我不知道如何处理它们。

你们能帮帮我吗?我搜索了其他问题,但根据答案,我似乎做对了。当然,我不是。

我的代码:

我正在使用 TouchImageView显示图像。

另外,我扩展了ViewPager来支持缩放功能,像这样:

public class ExtendedViewPager extends ViewPager {

public ExtendedViewPager(Context context) {
super(context);
}

public ExtendedViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof TouchImageView) {
/*
canScrollHorizontally is not supported for Api < 14. To get around this issue,
ViewPager is extended and canScrollHorizontallyFroyo, a wrapper around
canScrollHorizontally supporting Api >= 8, is called.
*/
return ((TouchImageView) v).canScrollHorizontallyFroyo(-dx);
} else {
return super.canScroll(v, checkV, dx, x, y);
}
}
}

有些照片可能很大(例如 2000x2000 像素)。因此,在将它们添加到 View 之前,我使用此重新采样函数对它们进行了重新采样:

public static Bitmap resample(int resourceID, boolean toFullScreen, Context c){
//set the width and height we want to use as maximum display
WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int targetWidth = display.getWidth();
int targetHeight = display.getHeight();

//create bitmap options to calculate and use sample size
BitmapFactory.Options bmpOptions = new BitmapFactory.Options();

//first decode image dimensions only - not the image bitmap itself
bmpOptions.inJustDecodeBounds = true;
BitmapFactory.decodeResource(c.getResources(), resourceID, bmpOptions);

//image width and height before sampling
int currHeight = bmpOptions.outHeight;
int currWidth = bmpOptions.outWidth;

//variable to store new sample size
int sampleSize = 1;

//calculate the sample size if the existing size is larger than target size
if (toFullScreen || currHeight > targetHeight || currWidth > targetWidth){
//use either width or height
if (toFullScreen || currWidth > currHeight)
sampleSize = Math.round((float)currHeight / (float)targetHeight);
else
sampleSize = Math.round((float)currWidth / (float)targetWidth);
}

//use the new sample size
bmpOptions.inSampleSize = sampleSize;
//now decode the bitmap using sample options
bmpOptions.inJustDecodeBounds = false;

//get the file as a bitmap and return it
Bitmap pic = BitmapFactory.decodeResource(c.getResources(), resourceID, bmpOptions);
return pic;
}

并在我的 PagerAdapter 实现中调用它,其中我还尝试 recycle() 位图:

private class TouchImagePagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return galleryImages.length;
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = PhotoGalleryActivity.this;
TouchImageView touchImageView = new TouchImageView(context);
touchImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

touchImageView.setImageBitmap(Helper.resample(galleryImages[position].getImgResource(), false, getApplicationContext()));
container.addView(touchImageView, 0);

return touchImageView;
}

@Override
public void destroyItem(ViewGroup collection, int position, Object object) {
TouchImageView ti = (TouchImageView) object;
BitmapDrawable bmpDrawable = (BitmapDrawable) ti.getDrawable();
if (bmpDrawable != null) {
Bitmap bmp = bmpDrawable.getBitmap();
if(bmp != null && !bmp.isRecycled())
bmp.recycle();
}
collection.removeView(ti);
ti = null;
}

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

有什么线索吗?提前致谢。

最佳答案

如果原始 bmp 大小大于 imageview,我建议更改 inSampleSize 2 或 4。你说有时图像大于 2000x2000。这可能会导致内存问题。

关于android - 内存不足 + 位图 + ImageView + ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30294067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com