6

我已经扩展了 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吗?

4

1 回答 1

8
  • update函数应该调用invalidate()orpostInvalidate()以便视图重绘。
  • 此外,SurfaceViews 默认禁用重绘。setWillNotDraw(false)通过调用例如启用它surfaceCreated
于 2013-09-12T08:46:51.317 回答