我的圖像高度超過6000像素。每當我嘗試顯示這些圖像時,都會發現內存異常。如何在imageview android中顯示大尺寸的位圖?
我看過很多鏈接,但沒有符合我的需求。因爲大多數人建議圖像調整解決方案。但是,如果我將重新調整圖像的尺寸,而不是由於質量較差而無法讀取圖像中的文字。
我想在創建一些代碼時可以打開一個圖像,而無需通過縮放效果重新調整大小。
任何幫助將非常感激。
我的圖像高度超過6000像素。每當我嘗試顯示這些圖像時,都會發現內存異常。如何在imageview android中顯示大尺寸的位圖?
我看過很多鏈接,但沒有符合我的需求。因爲大多數人建議圖像調整解決方案。但是,如果我將重新調整圖像的尺寸,而不是由於質量較差而無法讀取圖像中的文字。
我想在創建一些代碼時可以打開一個圖像,而無需通過縮放效果重新調整大小。
任何幫助將非常感激。
嘗試使用的WebView來顯示圖像,而不是ImageView的
的我建議你在3個較小的圖像中「剪切」這個圖像,然後將它們作爲三個圖像加載:第一個圖像位於y:0,高度爲2000px,第二個圖像的長度爲2000px,高度爲2000px,第三個在y:4000px。 你認爲這可行嗎? 好運
我不認爲它會工作....因爲我也需要實現縮放效果以及....用戶將不得不點擊多次縮放每個圖像.... –
你可以插入三圖像放在容器中,然後縮小整個容器,這樣您就可以獲得與放大整個圖像相同的效果。 – Alexandre
嘗試使用google推薦了這個問題。
爲了避免OOM您需要執行的代碼塊:
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;
}
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;
}
試試這個:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_view);
imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageBitmap(decodeSampledBitmapFromResource(imgpath));
}
//Load a bitmap from a resource with a target size
Bitmap decodeSampledBitmapFromResource(String res) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(res, options);
//Calculate display dimention for maximum reqwidth and reqheigth
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int xDim = size.x;
int yDim = size.y;
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, xDim, yDim);
// Decode bitmap with inSampleSize se5t
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(res, options);
}
int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
int inSampleSize = 1; //Default subsampling size
// Calculate the largest inSampleSize value
while ((options.outHeight/inSampleSize) > reqHeight
|| (options.outWidth/inSampleSize) > reqWidth) {
inSampleSize += 1;
}
return inSampleSize;
}
在應用程序清單文件標記設置largeHeap真
<application
android:largeHeap="true"
而且使用Picasso或Glide在ImageView中加載圖像
感謝您的建議....我會試試這個.. :) –
感謝男人....你救了我的一天....它像一個魅力:) –
很高興它幫助。 – Rakhita