无法检测两个视图叠加

时间:2018-04-08 04:16:27

标签: android animation

我编写了一个小型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>

预览:

optimized away

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

以下代码将为您服务:

int fd[2];              // CREATING PIPE
pipe(fd);
int status;

int pid=fork();
if(pid==0)
{
     // WRITER PROCESS

    srand(123);
    int arr[3]={1,2,3};

    close(fd[0]);                   // CLOSE UNUSED(READING END)
    for(int i=0;i<3;i++)
      write(fd[1],&arr[i],sizeof(int));
    close(fd[1]);                   // CLOSE WRITING END AFTER WRITING SO AS READ GETS THE EOF
}
else
{
    // READER PROCESS

    int arr[10];

    int  i=0;
    int n_bytes;
    //close(fd[1]);                   // CLOSE UNUSED(WRITING END)
    while((n_bytes=read(fd[0],&arr[i],sizeof(int)))>0)        // READIN IN A LOOP UNTIL END
        i++;
    close(fd[0]);                   // CLOSE READING END after reading
    for(int j=0;j<i;j++)
        cout<<arr[j]<<endl;
    while(wait(&status)>0)
       ;
}

目标成功@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); } 将显示在交叉点

答案 1 :(得分:0)

您已为goalRect错误地设置尺寸,请更改

Rect goalRect = new Rect(goal.getLeft(), goal.getTop(), goal.getRight(), ball.getBottom());

Rect goalRect = new Rect(goal.getLeft(), goal.getTop(), goal.getRight(), goal.getBottom());

此外,这不会影响您的代码,但如果您使用intersect(),那么如果两个Rect相交,则goalRect()将设置为ballRect()。将来,如果您不想无意中更改Rect,请使用intersects()。有关详细信息,请参阅branding guidelines