Extended SurfaceView's onDraw() method never called

后端 未结 4 1845
萌比男神i
萌比男神i 2020-11-29 22:22

I\'m trying to modify the SurfaceView I use for doing a camera preview in order to display an overlaying square. However, the onDraw method of the extended SurfaceView is ne

相关标签:
4条回答
  • 2020-11-29 22:49

    Minor correction:

    Adding setWillNotDraw(false) to the constructor will cause crashes, because the underlying Surface object is not created yet.

    Instead, put the setWillNotDraw(false) statement into the surfaceCreated() method. This delays the call until there's a real object to work with, and works properly.

    (and many thanks to the users who posted this solution, it solved a major problem for me)

    0 讨论(0)
  • 2020-11-29 22:51

    Romain Guy explained it:

    For efficiency, layouts do not get their onDraw() method called. To enable it, call setWillNotDrawEnabled(false) (or set the equivalent XML attribute to false.)

    0 讨论(0)
  • 2020-11-29 22:56

    Based on all above comments I used :

    Add setWillNotDraw(false) in the onAttachedToWindow() event.

    @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); setWillNotDraw(false); }

    and it worked.

    0 讨论(0)
  • 2020-11-29 23:00

    Found it on the android-developers Google group. You simply have to add :

    setWillNotDraw(false)
    

    To the constructor. Now if someone could explain me why, that would be greatly appreciated.

    0 讨论(0)
提交回复
热议问题