Android drawing button to canvas with custom view?

后端 未结 3 775
醉梦人生
醉梦人生 2020-12-09 11:34

How can I draw a button on top of the canvas in a custom view? (Preferably on the mid-right side) Is there something I have to call before doing the button.draw(canvas)?

相关标签:
3条回答
  • 2020-12-09 12:03

    Try this

    public onDraw(Canvas canvas) {
           super.onDraw(canvas);
           canvas.save();
           pauseButton.draw(canvas);
           canvas.restore();
    }
    
    0 讨论(0)
  • 2020-12-09 12:08

    Why do you need to draw the button yourself? Use a FrameLayout and simply have the button overlayed on your custom view.

    0 讨论(0)
  • 2020-12-09 12:13

    You cannot insert a button into canvas. Canvas is an interface for bitmap or a bitmap buffer for a view. You can only draw other bitmap or pixels in it, not insert an object or a widget.

    There are some solutions:

    1. as Nikolay suggested, use a FrameLayout and create two layers (views), first your custom view and the second LinerView or RelativeView, which will come on top, where you can have buttons etc

    2. draw an image of a buttun on Canvas then use onTouchEvent in your custom view and test for the coordinates of the touch, then do something... an example for onTouchEvent here: Make certain area of bitmap transparent on touch

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