Android:同时按下两个ImageView

时间:2018-01-23 00:45:34

标签: java android xml ontouchlistener motionevent

我目前正在努力让你必须同时按下两个ImageView才能将它们移动到屏幕上的随机位置。由于我无法使用uri执行此操作,因此我尝试使用以下代码示例。经过几个小时的搜索,我找到了这段代码:Link

OnClickListener

我再次谷歌搜索了一个小时左右,开始了解public class MainClass extends Activity { // ... btn1 = (CustomButton)findViewById(R.id.button_one); btn2 = (CustomButton)findViewById(R.id.button_one); btn1.ignoreMotionEvent(true); btn2.ignoreMotionEvent(true); // ... @Override public boolean onTouchEvent(MotionEvent event) { if(r1 == null) { //r1 and r2 are Rect that define the absolute region of the button. //They are both defined here to ensure that the everything will be layed out (didn't always work so just for extra "sure-ness" did it here) } CustomButton btn = null; MotionEvent mevent = null; switch(event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_UP: int x = (int)event.getX(); int y = (int)event.getY(); if(r1.contains(x, y)) { btn = btn1; } else if(r2.contains(x, y)) { btn = btn2; } if(btn != null) { mevent = event; } break; } if(btn != null) { btn.ignoreMotionEvent(false); btn.onTouchEvent(mevent); btn.ignoreMotionEvent(true); } return super.onTouchEvent(event); } } OnTouchEvent。 我目前的主要问题是我不明白这意味着什么。

MotionEvent

我是否需要以某种方式通过某种方式定义我的ImageView区域?

我自己的代码看起来像这样。

//r1 and r2 are Rect that define the absolute region of the button.
        //They are both defined here to ensure that the everything will be layed out (didn't always work so just for extra "sure-ness" did it here)

XML

import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;

public class GameMode2 extends AppCompatActivity {

    public int startTime = 15000;
    public int interval = 1;
    public int counter;
    boolean neoncircle2Clicked = false;
    boolean neoncircle3Clicked = false;
    boolean restartGame2ButtonClicked = false;
    ImageView neoncircle2;
    ImageView neoncircle3;
    TextView startGame2;
    TextView scoreGM2;
    TextView timerGM2;
    Button restartGame2Button;


    CountDownTimer timer = new CountDownTimer(startTime, interval) {
        @Override
        public void onTick(long millisUntilFinished) {
            final int timeLeft = (int) millisUntilFinished;
            timerGM2.setText(Integer.toString(timeLeft / 1000));
        }

        @Override
        public void onFinish() {
            timerGM2.setText("Time is up");
            restartGame2Button.setVisibility(View.VISIBLE);
            neoncircle2Clicked = true;
            neoncircle3Clicked = true;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_mode2);

        neoncircle2 = (ImageView) findViewById(R.id.neoncircle2);

        neoncircle3 = (ImageView) findViewById(R.id.neoncircle3);

        startGame2 = (TextView) findViewById(R.id.scoreGM2);
        scoreGM2 = (TextView) findViewById(R.id.scoreGM2);
        timerGM2 = (TextView) findViewById(R.id.timerGM2);
        restartGame2Button = (Button) findViewById(R.id.restartGame2Button);

        restartGame2Button.setVisibility(View.INVISIBLE);

        restartGame2Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!restartGame2ButtonClicked) {
                    startActivity(getIntent());
                }
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        ImageView neocnircle2 = null;
        MotionEvent mEvent = null;
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                int x = (int) event.getX();
                int y = (int) event.getY();

                if (neoncircle2.contains(x, y)) {
                    //do that and this
                }
        }

        return super.onTouchEvent(event);
    }

    public static Point getDisplaySize (@Nullable GameMode2 context) {
        Point point = new Point();
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getSize(point);
        return point;
    }


    public void setCirclesRandomPosition (ImageView neoncircle2, ImageView neoncircle3) {
        int randomX2 = new Random().nextInt(getDisplaySize(this).x - neoncircle2.getWidth());
        int randomY2= new Random().nextInt(getDisplaySize(this).y - (3 * neoncircle2.getHeight()));

        int randomX3 = new Random().nextInt(getDisplaySize(this).x - neoncircle3.getWidth());
        int randomY3 = new Random().nextInt(getDisplaySize(this).y - (3 * neoncircle3.getHeight()));

        neoncircle2.setX(randomX2);
        neoncircle2.setY(randomY2);

        neoncircle3.setX(randomX3);
        neoncircle3.setY(randomY3);
    }

    public void backToMenuGM2Clicked(View view) {
        Intent intentBackToMenuGM2 = new Intent(this, MainActivity.class);
        startActivity(intentBackToMenuGM2);
        this.finish();
    }

    public void onBackPressed () {
        Intent obp2 = new Intent(this, MainActivity.class);
        startActivity(obp2);
        this.finish();
    }
}

代码中的错误在这里

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/botGameFragGM2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">


    <ImageView
        android:id="@+id/neoncircle2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@+id/restartGame2Button"
        android:layout_alignStart="@+id/restartGame2Button"
        android:layout_centerVertical="true"
        app:srcCompat="@drawable/neoncircle2" />

    <ImageView
        android:id="@+id/neoncircle3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignEnd="@+id/restartGame2Button"
        android:layout_alignRight="@+id/restartGame2Button"
        android:layout_alignTop="@+id/neoncircle2"
        app:srcCompat="@drawable/neoncircle3" />

    <TextView
        android:id="@+id/startGame2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:textColor="#39FF14"
        android:textStyle="bold"
        android:textSize="25dp"
        android:text="Tap the green circle to start!"
        android:gravity="center"/>

    <Button
        android:id="@+id/restartGame2Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="11dp"
        android:background="#000000"
        android:text="Restart Game"
        android:textColor="#39FF14"
        android:textSize="30dp"
        android:textStyle="bold"
        android:gravity="center"/>

    <TextView
        android:id="@+id/scoreGM2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_margin="10dp"
        android:gravity="center"
        android:textStyle="bold"
        android:textColor="#39FF14"
        android:textSize="200dp"
        android:text=""
        android:alpha=".1"/>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

r1r2可能是Rect对象(https://developer.android.com/reference/android/graphics/Rect.html),而您的neoncircle2ImageView对象,有contains(int, int)中没有名为ImageView的函数。

答案 1 :(得分:0)

您可以同时拨打两个视图的onClickListener。您只需要点击第一个视图,就必须以编程方式为第二个视图调用onClick()。您可以使用view.performClick()

进行操作

例如,如果您有twi视图view1和view2,那么您可以这样做

view1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                      view2.performClick();
                       }
                });
view2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                      view1.performClick();
                       }
                });

点击任意一个视图,onClickListener两个视图都会调用。