如何更新SurfaceView?

时间:2013-09-04 06:59:41

标签: java android surfaceview

我扩展了SurfaceView并设法在Activity中绘制它。 Activity应该能够在我的SurfaceView上调用一个方法来改变一些参数并重绘它。如何实现更新功能?

这是我的班级:

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?
    }
}

update应该包含哪些内容来重绘所有内容?这是否与surfaceChanged有关?

1 个答案:

答案 0 :(得分:6)

  • update函数应调用invalidate()postInvalidate(),以便重新构建视图。
  • 此外,SurfaceViews默认禁用重绘。通过调用例如setWillNotDraw(false)启用它surfaceCreated