How to update SurfaceView?

I expanded SurfaceView and was able to draw it in Activity. The exercise should be able to call a method on my SurfaceView that changes some parameter and redraws it. How to implement this update function?

This is my class:

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback { private int circleRadius = 50; private SurfaceHolder sh; private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); public MySurfaceView(Context context, AttributeSet as) { super(context,as); sh = getHolder(); sh.addCallback(this); paint.setColor(Color.BLUE); paint.setStyle(Style.FILL); } public void surfaceCreated(SurfaceHolder holder) { Canvas canvas = sh.lockCanvas(); canvas.drawColor(Color.BLACK); canvas.drawCircle(100, 200, circleRadius, paint); sh.unlockCanvasAndPost(canvas); } public void update( newRadius ) { circleRadius = newRadius; // What more? } } 

What should update contain to redraw everything? Is it related to surfaceChanged ?

+4
source share
1 answer
  • The update function must call invalidate() or postInvalidate() for the view to be redrawn.
  • In addition, the default redraw for SurfaceViews is disabled. Turn it on by calling setWillNotDraw(false) , for example. surfaceCreated .
+6
source

Source: https://habr.com/ru/post/1500450/


All Articles