Yuuki Kuroyama
Yuuki Kuroyama

Reputation: 33

Is it safe to keep calling View.invalidate so that onDraw is called constantly?

So I have this Canvas View which draws stuff from Canvas class, the thing is that onDraw in my Canvas view class is only called on certain time like moving from app to another app or anything that affects the screen and the layout. So I found that View.invalidate will call onDraw so that i will repaint all of the layout. So I made it like this to make my onDraw is called constantly, but is it safe? I mean I do feel some FPS drops in a certain time, is there a better way to make sure onDraw is called constantly?

private class CanvasView extends View{
    public CanvasView(Context context){
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        native_onDraw(canvas, width, height, density);

        this.invalidate(); // refresh everything
    }
}

Upvotes: 0

Views: 160

Answers (1)

TheArmKing
TheArmKing

Reputation: 21

Overriding OnDraw() and calling invalidate is a good way to achieve a display that updates every frame, and this will be resource consuming. On the other hand you can check out the Choreographer class.

Upvotes: 1

Related Questions