Android-如何让移动的屏幕对象扩展Button

发布于 2024-12-28 06:48:36 字数 282 浏览 3 评论 0原文

在我正在开发的一个应用程序中,我有一个类扩展了表面视图,另一个类处理所有计算以找出应该在屏幕上绘制对象的位置和时间。现在,我有一种注册屏幕点击的黑客方法,其中每次点击都会解析对象列表,并且最接近的对象会点击。当屏幕上只有 3 或 4 个对象时,此方法有效,但当我有更多对象时,点击会注册到错误的对象。

我想做的是让屏幕上的对象扩展按钮类,这样我就可以在单击它们时将 MotionEvent 传递给它们。但由于我所开设的课程的方式,目前我对如何做到这一点感到有点困惑。

有谁知道有关扩展按钮类以用于移动屏幕对象的教程吗?

In an app Im working on I have a class that extends a surface view and another that handles all calculations to figure out where and when objects should be drawn onscreen. Right now I have a sort of hacky way of registering screen clicks where a list of objects is parsed through on each click and the closest object takes the click. This works when there is only 3 or 4 objects onscreen but when I have more the clicks register for the wrong objects.

What Im trying to do is to make the onscreen objects extend the button class so I can just pass a MotionEvent to them when they are clicked. But because of the way that the classes I have are set up its a bit confusing to me at the moment on how this could be done.

Does anybody possible know of any tutorials that deal with extending the button class to use with moving onscreen objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风轻花落早 2025-01-04 06:48:36

如果我理解正确的话,您正在尝试确定绘制到屏幕上的哪个对象已被单击?

我知道这并不完全是您所要求的,但是当我过去实现自定义视图时,我通常会像这样处理点击检测:

我获取您想要可点击的对象的 x 和 y 坐标。在 SurfaceView 类中的 onTouchEvent(MotionEvent event) 方法中,有一些 if 语句来检查单击的 x 和 y 是否在对象的边界内。

这里有一些代码可以更好地说明我的意思:

@Override
public boolean onTouchEvent(MotionEvent event) 
{
    // If the user touches the space occupied by object1
    if(event.getAction() == MotionEvent.ACTION_DOWN 
            && event.getX() <= object1.xPosition + object1.width 
            && event.getX() >= object1.xPosition
            && event.getY() >= object1.yPosition 
            && event.getY() < object1.yPosition + object1.height)
    {
            // The click was in the bounds of object1's current location
        object1.doSomething();
    }
    // ......
}

希望这是有道理的并且有一定帮助。

If I understand correctly, you're trying to determine which object drawn to the screen has been clicked?

I know this isn't exactly what you asked for but when I've implemented a custom view the past, I've typically handled click detection like this:

I get the x and y coordinates of the objects that you want to be clickable. In your onTouchEvent(MotionEvent event) method within your SurfaceView class, have some if statement that checks whether the x and y of the click is within the bounds of your object.

Here's some code to better illustrate what I'm saying:

@Override
public boolean onTouchEvent(MotionEvent event) 
{
    // If the user touches the space occupied by object1
    if(event.getAction() == MotionEvent.ACTION_DOWN 
            && event.getX() <= object1.xPosition + object1.width 
            && event.getX() >= object1.xPosition
            && event.getY() >= object1.yPosition 
            && event.getY() < object1.yPosition + object1.height)
    {
            // The click was in the bounds of object1's current location
        object1.doSomething();
    }
    // ......
}

Hopefully this makes sense and is somewhat helpful.

热情消退 2025-01-04 06:48:36

我认为你有点混合东西。按钮通常用于布局(xml 布局的东西)。当您使用表面视图时,您(很可能)需要实现自己的“按钮”。
但是,可能可以将框架视图覆盖在表面视图之上,并将按钮放置在该视图上。

I think you kind of mixing stuff. Button is usually used in layout (the xml layout stuff). When you use surface view you (most likely) need to implement your own "button".
However, it is probably possible to just overlay a frameview on top of your surface view and place the button on that view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文