在图像视图上的DoubleTap手势

时间:2016-01-30 00:43:00

标签: android android-imageview

我是Android的初学者,并试图制作一个简单的井字游戏。在游戏中的任何时候,当用户双击ImageView时,我希望图像改变(即)X将变为O而O将变为X.我该如何实现?

这是我的代码

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.view.GestureDetectorCompat;
public class MainActivity extends AppCompatActivity implements OnGestureListener ,GestureDetector.OnDoubleTapListener {
private GestureDetectorCompat gestureDetector;
int activePlayer =0; // if its 0 -> Cross , if its 1 -> circle
boolean gameActive = true;
int[] gameState ={2,2,2,2,2,2,2,2,2};
int[][] winningPosition = {{0,1,2},{3,4,5},{6,7,8},{0,4,8},{2,4,6},{0,3,6},{1,4,7},{2,5,8}};
ImageView counter;
public void dropIn (View view) {
    counter =  (ImageView) view;
    System.out.println(counter.getTag().toString());
    int tapCounter = Integer.parseInt(counter.getTag().toString());
    if (gameState[tapCounter] == 2 && gameActive) {
        gameState[tapCounter] = activePlayer;
        counter.setTranslationY(-1000f);
        if (activePlayer == 0) {
            counter.setImageResource(R.drawable.o);
            activePlayer = 1;
        } else {
            counter.setImageResource(R.drawable.a);
            activePlayer = 0;
        }
        counter.animate().translationYBy(1000f).setDuration(600);
        for(int[] winningPositions : winningPosition)
        {
            if(gameState[winningPositions[0]] == gameState[winningPositions[1]] && gameState[winningPositions[1]] == gameState[winningPositions[2]]
                    && gameState[winningPositions[1]] != 2)
            {
                String winner = "Player 1";
                gameActive = false;
                if(gameState[winningPositions[0]] == 1)
                {
                    winner = "Player 2";
                }
                TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
                winnerMessage.setText(winner + " has won !");
                RelativeLayout hiddenLayout = (RelativeLayout)findViewById(R.id.playAgainLayout);
                hiddenLayout.setVisibility(View.VISIBLE);}
            else
            {
                boolean gameOver = true;
                for(int counterState : gameState)
                {

                    if(counterState == 2)
                        gameOver = false;

                }
                if(gameOver)
                {
                    TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
                    winnerMessage.setText("Its a draw");
                    RelativeLayout hiddenLayout = (RelativeLayout)findViewById(R.id.playAgainLayout);
                    hiddenLayout.setVisibility(View.VISIBLE);

                }
            }
        }
    }
}
@Override
public boolean onDoubleTap(MotionEvent e) {
  if(gameState[0] ==0 || gameState[1] ==0 || gameState[2] ==0)
    {
        for (int i = 0;i<9;i++)
        {
            if(gameState[i] == 0)
            {
                counter.setImageResource(0);
                counter.setImageResource(R.drawable.a);
                gameState[i]=1;
            }
            else {
                counter.setImageResource(R.drawable.a);
                gameState[i]=0;
            }
        }
    }
   return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
    Toast.makeText(getApplicationContext(),"Double Tap Event",Toast.LENGTH_LONG).show();
    return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    return false;
}
public void exit()
{
    Intent intent = new Intent(this, IntroActivity.class);
    startActivity(intent);
}
public void playAgain(View view)
{
    RelativeLayout hiddenLayout = (RelativeLayout)findViewById(R.id.playAgainLayout);
    hiddenLayout.setVisibility(View.INVISIBLE);
    activePlayer =0;
    gameActive=true;
    for(int i=0;i<gameState.length;i++)
    {
        gameState[i]=2;
    }
    GridLayout tableGrid = (GridLayout) findViewById(R.id.tableBorder);
    for(int i=0;i<tableGrid.getChildCount();i++)
    {
        ((ImageView) tableGrid.getChildAt(i)).setImageResource(0);
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.gestureDetector = new GestureDetectorCompat(this,this);
    gestureDetector.setOnDoubleTapListener(this);
}
@Override
public boolean onDown(MotionEvent e) {
    return false;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    this.gestureDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}
}

我的XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.adarshjayakumar.mygame.MainActivity"
android:background="#f2eddc">

<GridLayout
    android:layout_width="302dp"
    android:layout_height="302dp"
    android:columnCount="3"
    android:rowCount="3"
    android:layout_alignParentLeft="false"
    android:layout_alignParentTop="false"
    android:layout_centerInParent="true"
    android:layout_alignParentRight="false"
    android:background="@drawable/able"
    android:id="@+id/tableBorder">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView"
        android:layout_row="0"
        android:layout_column="0"
        android:tag="0"
        android:onClick="dropIn"
        />
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView1"
        android:layout_row="0"
        android:layout_column="1"
        android:tag="1"
        android:onClick="dropIn" />
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView2"
        android:layout_row="0"
        android:layout_column="2"
        android:tag="2"
        android:onClick="dropIn" />
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView3"
        android:layout_row="1"
        android:layout_column="0"
        android:tag="3"
        android:onClick="dropIn" />
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView4"
        android:layout_row="1"
        android:layout_column="1"
        android:tag="4"
        android:onClick="dropIn" />
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView5"
        android:layout_row="1"
        android:layout_column="2"
        android:tag="5"
        android:onClick="dropIn" />
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView6"
        android:layout_row="2"
        android:layout_column="0"
        android:tag="6"
        android:onClick="dropIn" />
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView7"
        android:layout_row="2"
        android:layout_column="1"
        android:tag="7"
        android:onClick="dropIn" />
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView8"
        android:layout_row="2"
        android:layout_column="2"
        android:tag="8"
        android:onClick="dropIn" />

</GridLayout>
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="0dp"
    android:layout_alignParentBottom="false"
    android:layout_centerVertical="true"
    android:background="#f96262"
    android:id="@+id/playAgainLayout"
    android:visibility="invisible">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Game Over"
        android:id="@+id/textView"
        android:textSize="20dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textStyle="bold" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play Again"
        android:id="@+id/button"
        android:layout_marginTop="60dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true"
        android:layout_marginStart="48dp"
        android:onClick="playAgain" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Exit"
        android:id="@+id/button2"
        android:layout_alignTop="@+id/button"
        android:layout_toEndOf="@+id/textView"
        android:onClick="exit" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play 1 has won"
        android:id="@+id/winnerMessage"
        android:layout_above="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_margin="20dp"
        android:textSize="18dp" />
  </RelativeLayout>
  </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

首次启动时阅读原始Google文档总是很好。 http://developer.android.com/reference/android/support/v4/view/GestureDetectorCompat.html手势检测器让您检测到双击。