尝试缩放时间位图的下降bitmap.Most是一个主要的原因,我们得到内存问题。 同时了解如何回收位图。 以下代码将帮助您。
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filename, options);
options.inJustDecodeBounds = false;
options.inSampleSize = 2;
bitmap = BitmapFactory.decodeFile(filename, options);
if (bitmap != null && exact) {
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
}
另外请确保您确实覆盖了以下方法。
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((TextView) view);
}
或者你可以创建一个函数来缩小位图
private byte[] resizeImage(byte[] input) {
if (input == null) {
return null;
}
Bitmap bitmapOrg = BitmapFactory.decodeByteArray(input, 0, input.length);
if (bitmapOrg == null) {
return null;
}
int height = bitmapOrg.getHeight();
int width = bitmapOrg.getWidth();
int newHeight = 250;
float scaleHeight = ((float) newHeight)/height;
// creates matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleHeight, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
bitmapOrg.recycle();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
resizedBitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
resizedBitmap.recycle();
return bos.toByteArray();
}
感谢您的soln,但上面的问题是为webview! –
@Vipul是的,我已覆盖destroyItem,仍然没有运气。 – Kiran