2016-02-05 74 views
2

我有一個活動,共包含4個圖像。它們都與1080x1920設備的分辨率相匹配。當我使用這些圖像(這些圖像通過XML直接加載到我的活動中)運行該活動時,它會在我的Genymotion模擬器中運行極其緩慢的,並且在真實的Android設備上運行滯後

這裏是設置:活動運行緩慢,幾個ImageView-s

<android.support.design.widget.AppBarLayout 
...> 
<android.support.design.widget.CollapsingToolbarLayout 
...> 
<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="200dp" 
      android:orientation="vertical" 
      app:layout_collapseMode="parallax"> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/imageView" 
       android:src="@drawable/shot_header" 
       android:scaleType="centerCrop" /> 

</LinearLayout> 
<android.support.v7.widget.Toolbar 
      .../> 
</android.support.design.widget.CollapsingToolbarLayout> 
</android.support.design.widget.AppBarLayout> 

第一圖像處於CollapsingToolbarlayout。該圖像的分辨率爲1080x649 PNG。

的content_activity:
此圖片填充母width.It的分辨率爲1080x772 PNG。

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="250dp" 
    android:id="@+id/main_image" 
    android:layout_below="@+id/shot_error_field" 
    android:src="@drawable/forehand_midpng" 
    android:adjustViewBounds="true" 
    android:scaleType="centerCrop" 
    android:layout_marginTop="15dp"/> 

其他2個圖像是在一個的LinearLayout,其分辨率爲500x399

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/main_image"> 

    <ImageView 
     android:layout_width="150dp" 
     android:layout_height="150dp" 
     android:id="@+id/imageView3" 
     android:src="@drawable/forehand_mid_wrong" 
     android:layout_weight="1"/> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="1dp" 
     android:layout_weight="1" > 
    </View> 

    <ImageView 
     android:layout_width="150dp" 
     android:layout_height="150dp" 
     android:id="@+id/imageView4" 
     android:src="@drawable/forehand_mid_wrong" 
     android:layout_weight="1"/> 
</LinearLayout> 

總之,我有4個ImageViews的活動,填入適當大小的圖像,這應該現代Android設備沒有問題。問題是由於高內存消耗,此活動運行非常緩慢且滯後。

我做錯了什麼?我怎樣才能進一步優化這些圖像?

我看着其他線程-out of memory issue,但似乎沒有提出解決這樣的問題。

+0

ImageView的一般不喜歡大的圖像。 –

+1

你用'android:largeHeap =「true」'嘗試嗎?這是不值得推薦的,但如果沒有任何作用,它可能是有幫助的。 – Stanojkovic

+2

問題是圖像分辨率..善意調整大小 –

回答

5

問題是圖像的resolution,如果你可以將圖像的reduce resolution然後工作罰款這裏是減少image resolution ans大小的一些例子。

如果您傳遞位圖寬度和高度,則使用下面的函數。

public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth, 
      int bitmapHeight) { 
     return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight, 
       true); 
    } 

如果您想要位圖比例相同並減少位圖大小。然後傳遞最大尺寸的位圖。您可以使用此功能

public Bitmap getResizedBitmap(Bitmap image, int maxSize) { 
    int width = image.getWidth(); 
    int height = image.getHeight(); 

    float bitmapRatio = (float)width/(float) height; 
    if (bitmapRatio > 0) { 
     width = maxSize; 
     height = (int) (width/bitmapRatio); 
    } else { 
     height = maxSize; 
     width = (int) (height * bitmapRatio); 
    } 
    return Bitmap.createScaledBitmap(image, width, height, true); 
} 

,或者如果您使用的是繪圖資源,然後用這個方法

public Drawable resizeImage(int imageResource) {// R.drawable.icon 
    // Get device dimensions 
    Display display = getWindowManager().getDefaultDisplay(); 
    double deviceWidth = display.getWidth(); 

    BitmapDrawable bd = (BitmapDrawable) this.getResources().getDrawable(
      imageResource); 
    double imageHeight = bd.getBitmap().getHeight(); 
    double imageWidth = bd.getBitmap().getWidth(); 

    double ratio = deviceWidth/imageWidth; 
    int newImageHeight = (int) (imageHeight * ratio); 

    Bitmap bMap = BitmapFactory.decodeResource(getResources(), imageResource); 
    Drawable drawable = new BitmapDrawable(this.getResources(), 
      getResizedBitmap(bMap, newImageHeight, (int) deviceWidth)); 

    return drawable; 
} 

/************************ Resize Bitmap *********************************/ 
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 

    int width = bm.getWidth(); 
    int height = bm.getHeight(); 

    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 

    // create a matrix for the manipulation 
    Matrix matrix = new Matrix(); 

    // resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 

    // recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, 
      matrix, false); 

    return resizedBitmap; 
} 
+1

優秀! 'resizeImage()'方法可行地工作於 – BabbevDan

+1

@DannyBabbev。感謝buddy接受我的答案。 –

0

TRY

  1. 絕對降低圖像的大小。
  2. 如果可以,緩存圖像。
  3. 將圖像下載到其他線程上。存儲一個HashMap將 很容易讓你

當你得到圖像的URL列表,遍歷它們:

pictures.put(id,view); 
     try{ 
      FileInputStream in = openFileInput(id); 
      Bitmap bitmap = null; 
      bitmap = BitmapFactory.decodeStream(in, null, null); 
     view.setImageBitmap(bitmap); 
     }catch(Exception e){ 
      new Thread(new PictureGetter(this,mHandler,id)).start(); 
     } 

代碼來更新圖像視圖:

if(id!=null){ 
     ImageView iv = pictures.get(id); 
     if(iv!=null){ 
      try{ 
       FileInputStream in = openFileInput(id); 
       Bitmap bitmap = null; 
       bitmap = BitmapFactory.decodeStream(in, null, null); 
       iv.setImageBitmap(bitmap); 
      }catch(Exception e){ 
     } 
    } 
+0

我的圖片尺寸很小。最大的是** 351 KB ** – BabbevDan

+0

這就是原因。它應該是平穩的工作,而不是那個 –

0

嘗試Facebook的壁畫庫。它可以處理大型圖像和我的項目之一,減少大約50%的內存消耗。

0

其實這不應該是一個問題,這些圖像是不是很大,讓你手機laggy。查看應用程序中的其他內容,可能會在UI線程中出現一些繁重的操作(如數據庫寫入/讀入,API請求)。

如果沒有這樣的操作,並且您看到了性能問題,請嘗試通過一些庫(如Picasso)設置這些圖像。

搖籃依賴性:

compile 'com.squareup.picasso:picasso:2.4.0' 

和代碼如下所示:

Picasso.with(this).load(R.drawable.my_image).into(toolbar);

+0

兄弟!你說1080x1920不是問題!真?在任何標準清晰度設備中檢查分辨率爲1080x1920的任何圖像,然後讓我知道會發生什麼 –

+0

首先,我不是你的兄弟,夥伴。其次 - 你讀過這個問題了嗎?他有一個分辨率爲1080x1920的現代Android設備,而不是圖像。優化總是需要的,但總是有可能在UI線程中有更重要的問題,隊友。 –