Android:检测屏幕左边减去100dp

时间:2015-10-13 02:43:56

标签: android android-screen

我有3张图片以300dp,400dp和500dp的translationX开头。因此,当活动开始时只有一个可见,而另外两个在屏幕外(右边缘)。他们在一个处理程序中运行,每50ms将它们向左移动30dp。我需要做的是检测任何图像何时到达屏幕左边缘减去 100dp,所以100dp离屏,然后再将它们一直设置到右边缘, 100dp。

我一直在研究几个小时,我似乎找不到任何方法来告诉代码屏幕边缘的位置。我发现了类似的问题,但它们似乎已经将屏幕边缘存储在一个变量中,并试图用它做一些事情(没有显示它们是如何获得该变量的。)

这就是我一直在使用的,它已经使用了一段时间,但经过几次传递后,ki爆炸消失了(突然只有2个会显示,然后是1,最后没有)......

public void kiBlastHandler() {
    kiHandler = new Handler();
    kiHandler.postDelayed(kiRunnable, 50);
}

    private Runnable kiRunnable = new Runnable() {
        @Override
        public void run() {

            if (gameRunning) {
                // Ki Blasts
                ImageView kiBlast1 = (ImageView) findViewById(R.id.kiBlast1);
                ImageView kiBlast2 = (ImageView) findViewById(R.id.kiBlast2);
                ImageView kiBlast3 = (ImageView) findViewById(R.id.kiBlast3);

                // Ki blast movement
                kiBlast1.setX(kiBlast1.getX() - currentSpeed);
                kiBlast2.setX(kiBlast2.getX() - currentSpeed);
                kiBlast3.setX(kiBlast3.getX() - currentSpeed);

                // Images for lives
                ImageView imgLives = (ImageView) findViewById(R.id.imgLives);
                if (currentLives == 3) {
                    imgLives.setImageResource(R.drawable.lives_03);
                } else if (currentLives == 2) {
                    imgLives.setImageResource(R.drawable.lives_02);
                } else if (currentLives == 1) {
                    imgLives.setImageResource(R.drawable.lives_01);
                }

                // Ready the vibration for losing life
                Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

                // Handle losing lives
                if (kiBlast1.getTranslationX() == -1100) {
                    currentLives = (currentLives - 1);
                    v.vibrate(vibrateLives);
                    kiBlast1.setTranslationX(600);
                }

                if (kiBlast2.getTranslationX() == -1100) {
                    currentLives = (currentLives - 1);
                    v.vibrate(vibrateLives);
                    kiBlast2.setTranslationX(600);
                }

                if (kiBlast3.getTranslationX() == -1100) {
                    currentLives = (currentLives - 1);
                    v.vibrate(vibrateLives);
                    kiBlast3.setTranslationX(600);
                }
            }

            // No lives left
            if (currentLives == 0) {
                if (gameRunning) {
                    gameRunning = false;
                    gameOver();
                }
            }

            kiHandler.postDelayed(kiRunnable, 50);
        }
    };

有人可以帮忙吗?我真的很感激!

1 个答案:

答案 0 :(得分:0)

感谢@MuhammadBabar的帮助,我能够解决问题。我正在使用:

if (kiBlast1.getTranslationX() == -1100) {// etc }

当我应该使用getX()函数时:

if (kiBlast1.getX() <= (viewMain.getX() - 100)) {// etc }

现在它完美无缺!我希望这有助于其他人理解对象的坐标是如何工作的,因为它让我非常困惑!

相关问题