Android:检测OnClick,以及其他元素是否位于顶部

时间:2015-10-11 05:46:54

标签: android onclicklistener android-event

奇怪的是,但我想做以下事情:

  • 在屏幕右下角有一个与指尖大小相关的圆形元素。
  • 让一些图像从屏幕左侧进入,位于同一Y位置,从左向右移动,最终与上述元素重叠。
  • 检测何时触摸第一个元素,以及图像元素是否与其重叠。

它类似于Dance Dance Revolution或Guitar Hero的作品。他们排队,你在正确的时间点击,然后发生了一些事情。

我知道如何设置onClickListener,但有人知道如何实现上述目标吗?

干杯, 李。

1 个答案:

答案 0 :(得分:0)

所以尽管获得了投票(我很高兴让我知道为什么,所以我可以避免将来的任何事情!),我将发布我最终自己制定的解决方案。 。希望有一天这会帮助别人。

注意:我稍微改了一下。我现在有来自屏幕右侧的ki爆炸,向左移动,当点击悟空的图像时,代码检查是否有任何ki爆炸在悟空的X位置,正负100 (所以距离悟空中心100px半径)。

    // Set the touch listener for the image I want as the target
imgCurrentForm.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    // Ki Blast image
                    ImageView kiBlast1 = (ImageView) findViewById(R.id.kiBlast1);

                    // Current X positions at any point in time
                    float xposForm = imgCurrentForm.getX();
                    float xposKi1 = kiBlast1.getX();

                    // Handle the screen touch event
                    int action = event.getAction();
                    switch (action) {

                        case MotionEvent.ACTION_DOWN:

                            if (currentPowerLevel >= 1 && currentPowerLevel <= 299999) {

                                if (Math.abs(xposForm - xposKi1) <= 100) {
                                    // Do whatever needs to be done if true
                                } 
                            }
                            break;
                    }
                    return true;
                }
            });