Android drawing button to canvas with custom view?

白昼怎懂夜的黑 提交于 2019-11-28 08:34:26
Lumis

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

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

Try this

public onDraw(Canvas canvas) {
       super.onDraw(canvas);
       canvas.save();
       pauseButton.draw(canvas);
       canvas.restore();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!