简体   繁体   English

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

[英]Android-How to make moving onscreen objects extend Button

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. 现在我有一种hacky方式来注册屏幕点击,每次点击都会解析对象列表,最近的对象会点击。 This works when there is only 3 or 4 objects onscreen but when I have more the clicks register for the wrong objects. 当屏幕上只有3个或4个对象但是我有更多点击注册错误的对象时,这种方法有效。

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. 我想做的是让屏幕上的对象扩展按钮类,这样我就可以在单击时将MotionEvent传递给它们。 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? 有没有人知道任何有关扩展按钮类以用于移动屏幕对象的教程?

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. 我得到了你想要点击的对象的x和y坐标。 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. 在SurfaceView类中的onTouchEvent(MotionEvent事件)方法中,使用一些if语句检查单击的x和y是否在对象的边界内。

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. 希望这是有道理的,有点帮助。

I think you kind of mixing stuff. 我认为你有点混合。 Button is usually used in layout (the xml layout stuff). Button通常用于布局(xml布局的东西)。 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. 但是,可能只是在表面视图顶部覆盖一个框架视图并将按钮放在该视图上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM