【问题标题】:what does the super call for onDraw do in the SurfaceView callback method, example: super.onDraw(canvas)SurfaceView回调方法中对onDraw的super调用是做什么的,例如:super.onDraw(canvas)
【发布时间】:2013-10-03 08:24:07
【问题描述】:

super.onDraw() 在这个例子中究竟做了什么?

如果我忽略它,它会改变调用 onDraw 方法时发生的情况吗?

 class Preview extends SurfaceView implements SurfaceHolder.Callback {

     // onDraw is a callback method of the SurfaceView class
     @Override
     protected void onDraw(Canvas canvas) {

     super.onDraw(canvas);

     // more code to implement some action when this method is called

     } // end onDraw method

 } // end Preview class

编辑:我刚刚找到了一个很好的问题和答案,以帮助澄清 supercall 的目的,When NOT to call super() method when overriding?

【问题讨论】:

    标签: java android user-interface camera surfaceview


    【解决方案1】:

    如果您查看SurfaceView 的源代码,您会发现它扩展了View 并且它没有覆盖onDraw() 方法,因此调用的是ViewonDraw() 方法.

    现在看看ViewonDraw()方法:

    /**
     * Implement this to do your drawing.
     *
     * @param canvas the canvas on which the background will be drawn
     */
    protected void onDraw(Canvas canvas) {
    }
    

    它什么也没做。所以在你的课堂上调用super.onDraw() 将无济于事。

    【讨论】:

      【解决方案2】:

      它什么都不做)SurfaceView 的 onDraw 是 View 的 onDraw 并且它是空的。但不能保证它总是空的。所以如果你没有理由不打电话,你最好打电话给它。

      【讨论】:

      • 我认为这就是为什么在许多代码完成工具中,它们会在您添加回调方法时自动添加超级调用
      • 默认情况下,如果你覆盖它,你总是应该调用超类方法。因为如果你不调用它,你可能会破坏一些东西。但是在某些情况下,当你真的确定时,如果你不需要它,你可以完全删除 super 的实现(恕我直言)。
      【解决方案3】:

      它调用SurfaceView类中的方法onDraw()

      一般来说,不是您的特定情况

      因为它是超类实现中的基类,基本的东西,如borderscolors,或任何设置到您传递的画布对象的东西。

      如果你不想让他们自己在这里写。

      【讨论】:

      • 如果是调用父类SurfaceView中的构造函数或方法,那我怎么知道什么时候调用呢?还是在我需要该功能时始终调用它更安全?
      • 这是什么意思,天气构造函数或方法,你是说继承我父母的属性。实际上你不需要像这样显式调用,除非你传递的那个对象上有一些特殊的功能(比如设置一些属性)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 2021-10-22
      相关资源
      最近更新 更多