开发者

Can't avoid GCs in simple draw loops

I'm trying to test out some different methods of drawing to a Canvas, without triggering garbage collection. Even the most basic examples cause frequent gc开发者_如何学Cs. Example:

class Panel extends View {
    private int mX = 0;
    private Paint mPaint = new Paint();

    public Panel(Context context) {
        super(context);
        mPaint.setColor(0xFFFF0000);
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
        canvas.drawRect(mX, 0, mX+40, 40, mPaint);
        mX++;

        postInvalidate();
    }
}

I get the same result with SurfaceView (lunar lander example). GCs about every 10 seconds or so, pretty jarring in a realtime game. I'm not making any allocations in the draw loop above, so something must be allocated in the canvas etc classes (unfortunately).

I had success with an opengl test, no gcs, but I was hoping to avoid getting into opengl. I'm pretty familiar with it, but it's going to be tough drawing some effects I wanted to achieve using opengl.

Thanks


postInvalidate() may have to create an object. Use invalidate() instead, there is no reason to use postInvalidate() here.


Instead of guessing you should take a look at what is allocated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜