2012-02-29 63 views
1

如果我評論這個區塊,那麼GC垃圾郵件就會消失。什麼是垃圾郵件?如果我註釋掉「//清理」部分,那麼垃圾郵件仍然存在。爲什麼logcat被GC信息垃圾郵件?

public void updatePixels() 
{ 
    // Fill the bitmap with black. 
    mBitmap.eraseColor(Color.BLACK); 

    // Pass bitmap to be rendered by native function. 
    if(!mNativeHelper.render(mBitmap)) return; 


    // If FroyVisuals has text to display, then use a canvas and paint brush to display it. 
    String text = mActivity.mTextDisplay; 
    if(text != null) 
    { 
     // Give the bitmap a canvas so we can draw on it. 
     mCanvas.setBitmap(mBitmap); 

     Paint paint = new Paint(); 
     paint.setAntiAlias(true); 
     paint.setTextSize(10); 
     paint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.ITALIC)); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeWidth(1); 
     paint.setColor(Color.WHITE); 
     paint.setTextAlign(Paint.Align.CENTER); 

     float canvasWidth = mCanvas.getWidth(); 
     float textWidth = paint.measureText(text); 
     float startPositionX = (canvasWidth - textWidth/2)/2; 

     mCanvas.drawText(text, startPositionX, mTextureWidth-12, paint); 
     paint = null; 
    } 

    // Flip the texture vertically. 
    Matrix flip = new Matrix(); 

    flip.postScale(1f, -1f); 

    Bitmap temp = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), flip, true); 

    // Copy bitmap pixels into buffer. 
    mPixelBuffer.rewind(); 

    temp.copyPixelsToBuffer(mPixelBuffer); 

    // Cleanup 
    temp.recycle(); 
    temp = null; 
    flip = null; 
} 

回答

1

我不知道你在'這個塊'指的是哪一部分,所以我假設你的意思是整個方法。您正在創建的對象這種方法

這裏

Paint paint = new Paint(); 

這裏

Bitmap temp = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), flip, true); 

裏面,所以如果被頻繁調用這個方法,你會看到這些對象GC'd相當很多。您應該使用一種不需要您在每次通話中創建對象的方法。例如,您可以在第一次運行該方法時創建bitmap,然後操作該位圖的canvas以實現翻轉。

您可以抓取canvasin the constructor並在每個方法調用開始時使用rotate()進行操作。這樣,您可以在第一次方法調用時創建BitmapCanvas

此外,無論何時您創建一個位圖,都可以爲此分配視頻內存,所以它看起來效率更低!

+0

如何在不創建新的位圖對象的情況下翻轉位圖? – Scott 2012-02-29 10:17:46

+0

已在我的答案中添加了一些更多信息以幫助您解決此問題 – Dori 2012-02-29 10:47:55

相關問題