2017-06-02 74 views
0

請我有我的應用程序的麻煩。outofmemoryerror當滾動scrollview與imageview

我的問題是outofmemoryerror錯誤。

我試着解釋一下我的方法

滾動滾動視圖在它是ImageView的,項的ImageView的問題大小(它的的文本項0-X線圖像)時,有23個<項目,滾動時Thise錯誤出現應用大大增加了內存使用量。但我不知道如何防止它?

我粘貼我的類的一部分插入位圖(畫布)到imageview。

try { 
     Bitmap b = Bitmap.createBitmap(srk, vsk, Bitmap.Config.ARGB_8888); 

     c = new Canvas(b); 
     imageview.setImageBitmap(b); 

    } catch (IllegalArgumentException e) { 
    } 

    paint = new Paint(); 
    paint.setAntiAlias(true); 
    paint.setStyle(Paint.Style.FILL); 


    for (int i=1;i<(prvy.getNasledujuciitemindex(pacage));i++) { 
     if (prvy.citajItem(pacage,i)!=null) { 

      if (prvy.CiZbalene(pacage, i)) { 
       paint.setColor(Color.argb(255, 130, 130, 130)); 
       c.drawRect(zarovnanieXZACDelLine, zarovnanieYDelLine + (i - 1) * vskconst, zarovnanieXKONDelLine, zarovnanieYDelLine + 2 + (i - 1) * vskconst, paint); 

       c.drawBitmap(cross, zarovnanieXX, (float) (context.getResources().getDimensionPixelSize(R.dimen.KorekciaLine) + (int) (i - 1) * vskconst), paint); 
       paint.setColor(Color.argb(255, 130, 130, 130)); 
       DalsiPrvok(paint, c, prvy.citajItem(pacage, i), vsk2 + (i - 1) * vskconst, sizetext, font); 
      } else { 
       paint.setColor(Color.BLACK); 
       DalsiPrvok(paint, c, prvy.citajItem(pacage, i), vsk2 + (i - 1) * vskconst, sizetext, font); 
      } 
     } else {break;} 

    } 
    imageview.invalidate(); 

只有這個代碼寫入imageview。

這裏是我的xml當有imageview。

  <ScrollView 
         android:id="@+id/scrollView2" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:scrollIndicators="none" 
         android:scrollbars="none"> 

         <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:orientation="vertical"> 

          <FrameLayout 
           android:id="@+id/Pack1" 
           android:layout_width="fill_parent" 
           android:layout_height="fill_parent" 
           android:background="#e1e1e1" 
           android:paddingBottom="@dimen/Pack_Bottom" 
           android:visibility="visible"> 

           <LinearLayout 
            android:id="@+id/linear1" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:orientation="vertical"> 



            <ImageView 
             android:id="@+id/listPrvy" 
             android:layout_width="match_parent" 
             android:layout_height="0dp" 
             android:clickable="true" 
             android:longClickable="true" 
             android:scaleType="fitXY"/> 
           </LinearLayout> 

          </FrameLayout> 
          </LinearLayout> 
          </ScrollView> 

我調用方法↑(其寫位圖到imageview的)

itemimg =新ItemsInPacagesImageView(imglist1,tentocontext,nazovtripu,0,prvy); - 這個類是我的,它將位圖放入imglist1(imageview)。

當我嘗試把b.recycle();位圖b = Bitmap.createBitmap(srk,vsk,Bitmap.Config.ARGB_8888)之後;

 c = new Canvas(b); 
     imageview.setImageBitmap(b); 

應用程序崩潰像使用回收位圖的東西。

謝謝你的任何想法。

Image

樓梯它開始滾動後(25項寬1080,高5040px)

回答

0

位圖需要大量的內存。一些提示是:

  1. 避免Bitmap.Config.ARGB_8888。對於不透明圖像,請使用Bitmap.Config.RGB_565
  2. 設置爲BitmapFactory.Options.inPurgeable = true爲您的圖像。它棄用去年的Android版本,但在舊的正常工作
+0

我沒有看到變化。應用程序也崩潰了。 – trip07

+0

這些更改只是減少內存使用量。還有很多其他的提示,例如:不要保留對未使用圖像的引用,不要同時保存很多圖像等。使用內存分析器 – rupashka

0

在Android上一個常見的問題來了,當你開始處理圖像時,它會導致很多OutOfMemoryError並且有專門用高清晰度的圖片。

如果你想避免在Android上的圖像造成任何OutOfMemoryError異常的問題,我建議你用畢加索: Link

+0

我沒有加載因程序運行時生成的互聯網或手機內存中的圖像。 – trip07