2012-09-27 94 views
1

我有一個圖像視圖,我想在其上設置圖像。圖像的大小是7,707,446字節。每當我嘗試設置佈局,應用程序崩潰和錯誤是內存不足的錯誤的。可有人建議我如何解決後援XML是: -如何在圖像視圖中設置大圖像

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars="none"> 

    <RelativeLayout 

     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/image" > 
    </RelativeLayout> 
</ScrollView> 

回答

0

嗨,我建議ü嘗試此操作並根據設備顯示圖像。

Bitmap bmp = BitmapFactory.decodeResource(myContext.getResources(),drawableId); 
      int w = bmp.getWidth(); 
      int h = bmp.getHeight(); 

      System.out.println("OrgWidth : " + w); 
      System.out.println("orgHeight : " + h); 

      int targetWidth = (screenWidth * w)/iPadWidth; 
      int targetHeight = (screenHeight * h)/iPadHeight; 

      System.out.println("TargetWidth : " + targetWidth); 
      System.out.println("TargetHeight : " + targetHeight); 

      int[] arrWidthHeight = {targetWidth,targetHeight}; 

      // This for Slove the out of Memory problem 

      int newWidth = 110; 
      int newHeight = 110; 
      float scaleWidth = ((float) newWidth)/w; 
      float scaleHeight = ((float) newHeight)/h; 

      Matrix bMatrix = new Matrix(); 
      bMatrix.postScale(scaleWidth, scaleHeight); 
      Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, w, h , bMatrix, true); 

首先取從資源的圖像,並找到根據裝置和比例後此圖像作爲目的在這裏解決了內存新的高度和圖像的寬度。

這對於發現的高度和寬度根據設備的高度和寬度..

WindowManager winManager = (WindowManager) myContext.getSystemService(Context.WINDOW_SERVICE); 
      screenWidth = winManager.getDefaultDisplay().getWidth(); 
      screenHeight = winManager.getDefaultDisplay().getHeight(); 

      System.out.println("Screen Width : " + screenWidth); 
      System.out.println("Screen Height : " + screenHeight); 

,並使用位圖來獲得圖像的高度和寬度女巫像ipathwidth和ipathHeight OK現在 快樂

+0

可以請你分不清什麼是屏幕寬度和iPadwidth – swati

+0

屏幕寬度是根據您使用您的活動,以顯示。設備和ipathwidth女巫圖像設備寬度\ –

+0

通常我創建一個方法女巫還給我新的高度和寬度與imagedrawable爲設置圖像背景.. –

0

Loading Large Bitmaps Effectively

你可以創建一個函數來計算的縮減因子:

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) { 
     if (width > height) { 
      inSampleSize = Math.round((float)height/(float)reqHeight); 
     } else { 
      inSampleSize = Math.round((float)width/(float)reqWidth); 
     } 
    } 
    return inSampleSize; 
} 

然後用它來裝載縮放位圖:

public static Bitmap decodeSampledBitmapFromResource(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); 
} 

因此,如果你想要一個100×100像素的縮略圖加載到您的ImageView,你只需使用:

mImageView.setImageBitmap(
    decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100) 
); 
1
Bitmap picture=BitmapFactory.decodeFile("/sdcard..."); 
    int width = picture.getWidth(); 
    int height = picture.getWidth(); 
    float aspectRatio = (float) width/(float) height; 
    int newWidth = 70; 
    int newHeight = (int) (70/aspectRatio);  
    picture= Bitmap.createScaledBitmap(picture, newWidth, newHeight, true); 


public static Bitmap decodeImage(String arrayList_image) { 

     URL aURL; 

     try { 

      aURL = new URL(arrayList_image); 

      URLConnection conn = aURL.openConnection(); 

      conn.connect(); 

      InputStream is = conn.getInputStream(); 

      BufferedInputStream bis = new BufferedInputStream(is); 

      bm = BitmapFactory.decodeStream(bis); 

      bis.close(); 

      is.close(); 

      return bm; 

     } catch (MalformedURLException e) { 

      e.printStackTrace(); 

     } catch (IOException e) { 

      e.printStackTrace(); 
     } 
     return null; 
    } 
相關問題