我编写了一个小型2D动画应用程序,其中用户在具有DPAD方向的布局中的ImageView周围移动。还有另一个ImageView作为目标。如果球进入球门,则应显示成功消息。以下代码是错误的,即只要有人移动DPAD,就会显示成功消息!

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    ObjectAnimator animator;
    switch(keyCode) {
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            animator = ObjectAnimator.ofFloat(ball, "translationX", ball.getX() + STEP_X);
                    animator.start();
            break;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            animator = ObjectAnimator.ofFloat(ball, "translationX", ball.getX() - STEP_X);
            animator.start();
            break;
        case KeyEvent.KEYCODE_DPAD_UP:
            animator = ObjectAnimator.ofFloat(ball, "translationY", ball.getY() - STEP_Y);
            animator.start();
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            animator = ObjectAnimator.ofFloat(ball, "translationY",   ball.getY() + STEP_Y);
        animator.start();
        break;
    }
    Rect ballRect = new Rect(ball.getLeft(), ball.getTop(), ball.getRight(), ball.getBottom());
    Rect goalRect = new Rect(goal.getLeft(), goal.getTop(), goal.getRight(), ball.getBottom());

    if(goalRect.intersect(ballRect)) {
        Toast.makeText(MainActivity.this, "Goal", Toast.LENGTH_SHORT).show();
    }

    return super.onKeyDown(keyCode, event);
}


布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <ImageView
        android:id="@+id/goal"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/field" />
    <ImageView
        android:id="@+id/object"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:contentDescription="Object"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ball" />

</android.support.constraint.ConstraintLayout>


预习:

android - 无法检测到两个 View 叠加-LMLPHP

有任何想法吗?

最佳答案

以下代码将为您工作:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    ObjectAnimator animator;
    switch(keyCode) {
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            animator = ObjectAnimator.ofFloat(ball, "translationX", ball.getX() + STEP_X);
                    animator.start();
            break;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            animator = ObjectAnimator.ofFloat(ball, "translationX", ball.getX() - STEP_X);
            animator.start();
            break;
        case KeyEvent.KEYCODE_DPAD_UP:
            animator = ObjectAnimator.ofFloat(ball, "translationY", ball.getY() - STEP_Y);
            animator.start();
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            animator = ObjectAnimator.ofFloat(ball, "translationY",   ball.getY() + STEP_Y);
        animator.start();
        break;
    }
    Rect ballRect = new Rect(ball.getLeft(), ball.getTop(), ball.getRight(), ball.getBottom());
    Rect goalRect = new Rect(goal.getLeft(), goal.getTop(), goal.getRight(), goal.getBottom());

    if(goalRect.intersect(ballRect)) {
        Toast.makeText(MainActivity.this, "Goal Success", Toast.LENGTH_SHORT).show();
    }

    return super.onKeyDown(keyCode, event);
}


目标成功Toast将显示在相交处

10-04 20:14