Android 绘图按钮到带有自定义视图的画布? [英] Android drawing button to canvas with custom view?
问题描述
如何在自定义视图中在画布顶部绘制按钮?(最好在中间右侧)在执行 button.draw(canvas) 之前有什么我必须调用的吗?
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)?
public class MyClass extends View {
public Simulation(Context context) {
super(context);
pauseButton.setText("TestButton");
pauseButton.setClickable(true);
pauseButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.i(TAG, "Button Pressed!");
}
});
public onDraw(Canvas canvas) {
super.onDraw(canvas);
pauseButton.draw(canvas);
}
}
感谢您的时间
推荐答案
不能将按钮插入画布.Canvas 是位图的接口或视图的位图缓冲区.您只能在其中绘制其他位图或像素,不能插入对象或小部件.
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.
有一些解决方案:
按照 Nikolay 的建议,使用 FrameLayout 并创建两个图层(视图),第一个是您的自定义视图,第二个是 LinerView 或 RelativeView,它们将位于顶部,您可以在其中放置按钮等
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
在 Canvas 上绘制一个按钮的图像,然后在您的自定义视图中使用 onTouchEvent 并测试触摸的坐标,然后做一些事情......这里是 onTouchEvent 的示例:使位图的某些区域在触摸时透明
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
这篇关于Android 绘图按钮到带有自定义视图的画布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!