【问题标题】:Android: Press both ImageViews at the same timeAndroid:同时按下两个 ImageView
【发布时间】:2018-01-23 12:37:08
【问题描述】:

我目前正在尝试让您必须同时按下两个 ImageView 才能将它们移动到屏幕上的随机位置。因为我不能用OnClickListener 来做,所以我用下面的代码示例进行了尝试。经过数小时的搜索,我找到了这个代码:Link

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);
}
}

我又用谷歌搜索了一个小时左右,开始理解OnTouchEventMotionEvent。 我目前的主要问题是我不明白这是什么意思。

//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)

我是否需要通过某种方式获取 ImageView 的大小来定义我的 ImageView 的一个区域?

我自己的代码是这样的。

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

<?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>

代码中的错误在这里

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

【问题讨论】:

    标签: java android xml ontouchlistener motionevent


    【解决方案1】:

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

    【讨论】:

    • 我按照你说的做了,我得到了它的轻微工作。我创建了两个 Rect 对象,并从我的 ImagewViews 中传递了宽度和高度。我现在的主要问题是如何将我的 Rect 对象与我的 ImageViews 连接起来?我的 ImageView 大小只有两个 Rect 对象。我想把它们放在我的 ImageViews 上,当我在屏幕上触摸我的 ImageView 时,我同时触摸两者。我该怎么做?
    【解决方案2】:

    您可以同时调用两个视图的onClickListener。只需单击第一个视图,您必须以编程方式调用onClick() 以获得第二个视图。您可以使用view.performClick() 来完成此操作

    例如,如果您有两个视图 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 两个视图都会调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多