在做判斷點擊方塊周邊是否存在缺口的時候出現(xiàn)問題一點擊就報錯程序閃退
package com.Pintu.pintu;
import com.example.pintu.R;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
/**利用二維數(shù)組創(chuàng)建若干個游戲小方塊*/
private ImageView[][] iv_game_arr = new ImageView[3][5];
// 游戲主界面
private GridLayout gl_main_game;
/**當前空方塊實例的保存*/
private ImageView iv_null_ImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*初始化游戲的若干個小方塊*/
Bitmap bigBm = ((BitmapDrawable)getResources().getDrawable(R.drawable.ic_game_tu)).getBitmap();//獲取一張大圖
int tuWandH = bigBm.getWidth()/5;//每個游戲小方塊的寬和高
for (int i = 0; i < iv_game_arr.length; i++) {
for (int j = 0; j < iv_game_arr[0].length; j++) {
Bitmap bm=Bitmap.createBitmap(bigBm,j*tuWandH,i*tuWandH,tuWandH,tuWandH);//根據(jù)航和列切成若干個游戲小圖片
iv_game_arr[i][j] = new ImageView(this);
iv_game_arr[i][j].setImageBitmap(bm);
//設置方塊之間的間距
iv_game_arr[i][j].setPadding(2, 2, 2, 2);
iv_game_arr[i][j].setTag(new GameData(i, j, bm));//綁定自定義的數(shù)據(jù);
iv_game_arr[i][j].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean flag = isHasByNullImageView((ImageView)v);
Toast.makeText(MainActivity.this, "當前位置關系是否存在"+flag, Toast.LENGTH_SHORT).show();
}
});
}
}
// 初始化游戲主界面,并添加若干個小方塊
gl_main_game = (GridLayout) findViewById(R.id.gl_main_game);
for (int i = 0; i < iv_game_arr.length; i++) {
for (int j = 0; j < iv_game_arr[0].length; j++) {
gl_main_game.addView(iv_game_arr[i][j]);
}
}
// 設置最后一個方塊是空的
setNullImageView(iv_game_arr[2][4]);
}
/*設置某個方塊為空方塊
@param mImageView 當前設置為空的方塊的實例
@return true :相鄰,false:不相鄰
*/
public void setNullImageView(ImageView mImageView){
mImageView.setImageBitmap(null);
}
//判斷當前點擊的方塊,是否與空方塊的位置關系是相關關系
public boolean isHasByNullImageView(ImageView mImageView){
//分別獲取當前空方塊的位置與點擊方塊的位置,通過x,y兩邊都差1的方式判斷
GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
GameData mGameData = (GameData)mImageView.getTag();
if(mNullGameData.y==mGameData.y&&mGameData.x+1==mNullGameData.x){//當前點擊的方塊在空方塊的上邊
return true;
}else if(mNullGameData.y==mGameData.y&&mGameData.x-1==mNullGameData.x){//當前點擊的方塊在空方塊的下邊
return true;
}else if(mNullGameData.y==mGameData.y+1&&mGameData.x==mNullGameData.x){//當前點擊的方塊在空方塊的左邊
return true;
}else if(mNullGameData.y==mGameData.y-1&&mGameData.x==mNullGameData.x){//當前點擊的方塊在空方塊的右邊
return true;
}
return false;
}
// 每個游戲小方塊需要綁定的數(shù)據(jù)
class GameData{
/**每個小方塊的實際位置x*/
public int x =0;?
/**每個小方塊的實際位置y*/
public int y =0;
/**每個小方塊的圖片*/
public Bitmap bm;
/**每個小方塊的圖片位置*/
public int p_x = 0;
/**每個小方塊的圖片位置*/
public int p_y =0;
public GameData(int x, int y, Bitmap bm) {
super();
this.x = x;
this.y = y;
this.bm = bm;
this.p_x = x;
this.p_y = y;
}
}
}
2017-09-06
public class MainActivity extends Activity {
? ?private boolean isAnimRun = false;
? ?//判斷游戲是否開始
? ?private boolean isGameStart = false;
? ?private ImageView [] [] iv_game_arr = new ImageView[3][5];
? ?private GridLayout gl_main_game;
? ?private ImageView iv_null_ImageView;
? ?private GestureDetector mDetector;
? ?@Override
? ?public boolean onTouchEvent(MotionEvent event) {
? ? ? ?return mDetector.onTouchEvent(event);
? ?}
? ?@Override
? ?public boolean dispatchTouchEvent(MotionEvent ev) {
? ? ? ?mDetector.onTouchEvent(ev);
? ? ? ?return super.dispatchTouchEvent(ev);
? ?}
? ?@Override
? ?protected void onCreate(Bundle savedInstanceState) {
? ? ? ?super.onCreate(savedInstanceState);
? ? ? ?setContentView(R.layout.activity_main);
? ? ? ?mDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public boolean onDown(MotionEvent motionEvent) {
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onShowPress(MotionEvent motionEvent) {
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public boolean onSingleTapUp(MotionEvent motionEvent) {
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onLongPress(MotionEvent motionEvent) {
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
? ? ? ? ? ? ? ?int type = getDirByGes(motionEvent.getX(),motionEvent.getY(),motionEvent1.getX(),motionEvent1.getY());
// ? ? ? ? ? ? ? ?Toast.makeText(MainActivity.this,""+type,Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ?changeByDir(type);
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ?}
? ? ? ?});
? ? ? ?Bitmap bigBm = ((BitmapDrawable) getResources().getDrawable(R.drawable.ic_game_tu)).getBitmap();
? ? ? ?int tuWandH = bigBm.getWidth()/5;
? ? ? ?int ivWandH = getWindowManager().getDefaultDisplay().getWidth()/5;
? ? ? ?for (int i=0;i<iv_game_arr.length;i++){
? ? ? ? ? ?for(int j=0;j<iv_game_arr[0].length;j++){
? ? ? ? ? ? ? ?Bitmap bm = Bitmap.createBitmap(bigBm,j*tuWandH,i*tuWandH,tuWandH,tuWandH);
? ? ? ? ? ? ? ?iv_game_arr[i][j]=new ImageView(this);
? ? ? ? ? ? ? ?iv_game_arr[i][j].setImageBitmap(bm);
? ? ? ? ? ? ? ?iv_game_arr[i][j].setLayoutParams(new RelativeLayout.LayoutParams(ivWandH,ivWandH));
? ? ? ? ? ? ? ?iv_game_arr[i][j].setPadding(2,2,2,2);
? ? ? ? ? ? ? ?iv_game_arr[i][j].setTag(new GameData(i,j,bm));
? ? ? ? ? ? ? ?iv_game_arr[i][j].setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? ? ? ? ?@Override
? ? ? ? ? ? ? ? ? ?public void onClick(View v) {
? ? ? ? ? ? ? ? ? ? ? ?boolean flag = isHasByNullImageView((ImageView)v);
? ? ? ? ? ? ? ? ? ? ? ?//Toast.makeText(MainActivity.this,"位置關系是否存在:"+flag,Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? ? ?if(flag){
? ? ? ? ? ? ? ? ? ? ? ? ? ?changeDataByImageView((ImageView) v);
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?});
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?gl_main_game = (GridLayout) findViewById(R.id.gl_main_game);
? ? ? ?for(int i=0;i<iv_game_arr.length;i++){
? ? ? ? ? ?for(int j=0;j<iv_game_arr[0].length;j++){
? ? ? ? ? ? ? ?gl_main_game.addView(iv_game_arr[i][j]);
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?//設置最后一個方塊為空
? ? ? ?setNullImageView(iv_game_arr[2][4]);
? ? ? ?//隨機打亂順序
? ? ? ?randomMove();
? ? ? ?isGameStart = true;
? ?}
? ?//隨機打亂順序
? ?public void randomMove(){
? ? ? ?for(int i=0;i<100;i++){
? ? ? ? ? ?int type = (int) ((Math.random()*4)+1);
? ? ? ? ? ?changeByDir(type,false);
? ? ? ?}
? ?}
? ?public void changeByDir(int type){
? ? ? ? changeByDir(type,true);
? ?}
? ?public void changeByDir(int type ,boolean isAnim){
? ? ? ?GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
? ? ? ?int new_x = mNullGameData.x;
? ? ? ?int new_y = mNullGameData.y;
? ? ? ?if(type == 1){
? ? ? ? ? ?new_x++;
? ? ? ?}else if(type == 2){
? ? ? ? ? ?new_x--;
? ? ? ?}else if(type == 3){
? ? ? ? ? ?new_y++;
? ? ? ?}else if(type == 4){
? ? ? ? ? ?new_y--;
? ? ? ?}
? ? ? ?if(new_x>=0&&new_x<iv_game_arr.length&&new_y>=0&&new_y<iv_game_arr[0].length){
? ? ? ? ? ?if(isAnim){
? ? ? ? ? ? ? ?changeDataByImageView(iv_game_arr[new_x][new_y]);
? ? ? ? ? ?}else {
? ? ? ? ? ? ? ?changeDataByImageView(iv_game_arr[new_x][new_y],isAnim);
? ? ? ? ? ?}
? ? ? ?}else {
? ? ? ?}
? ?}
? ?//判斷結束的方法
? ?public void isGameOver(){
? ? ? ?boolean isGameOver = true;
? ? ? ?for(int i=0;i<iv_game_arr.length;i++){
? ? ? ? ? ?for(int j=0;j<iv_game_arr[0].length;j++){
? ? ? ? ? ? ? ?if(iv_game_arr[i][j]==iv_null_ImageView){
? ? ? ? ? ? ? ? ? ?continue;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?GameData mGameData = (GameData) iv_game_arr[i][j].getTag();
? ? ? ? ? ? ? ?if(!mGameData.isTrue()){
? ? ? ? ? ? ? ? ? ?isGameOver = false;
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ?if(isGameOver){
? ? ? ? ? ?Toast.makeText(MainActivity.this,"游戲結束",Toast.LENGTH_SHORT).show();
? ? ? ?}
? ?}
? ?public int getDirByGes(float start_x, float start_y, float end_x, float end_y){
? ? ? ?boolean isLeftOrRight = (Math.abs(start_x-end_x)>Math.abs(start_y-end_y))?true:false;
? ? ? ?if(isLeftOrRight){
? ? ? ? ? ?boolean isLeft = start_x-end_x>0?true:false;
? ? ? ? ? ?if(isLeft){
? ? ? ? ? ? ? ?return 3;
? ? ? ? ? ?}else {
? ? ? ? ? ? ? ?return 4;
? ? ? ? ? ?}
? ? ? ?}else {
? ? ? ? ? ?boolean isUp = start_y-end_y>0?true:false;
? ? ? ? ? ?if(isUp){
? ? ? ? ? ? ? ?return 1;
? ? ? ? ? ?}else {
? ? ? ? ? ? ? ?return 2;
? ? ? ? ? ?}
? ? ? ?}
? ?}
? ?public void setNullImageView(ImageView mImageView){
? ? ? ?mImageView.setImageBitmap(null);
? ? ? ?iv_null_ImageView = mImageView;
? ?}
? ?public void changeDataByImageView(final ImageView mImageView){
? ? ? ?changeDataByImageView(mImageView,true);
? ?}
? ?public void changeDataByImageView(final ImageView mImageView, boolean isAnim){
? ? ? ?if(isAnimRun){
? ? ? ? ? ?return;
? ? ? ?}
? ? ? ?if(!isAnim){
? ? ? ? ? ?GameData mGameData = (GameData) mImageView.getTag();
? ? ? ? ? ?iv_null_ImageView.setImageBitmap(mGameData.bm);
? ? ? ? ? ?GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
? ? ? ? ? ?mNullGameData.bm = mGameData.bm;
? ? ? ? ? ?mNullGameData.p_x = mGameData.p_x;
? ? ? ? ? ?mNullGameData.p_y = mGameData.p_y;
? ? ? ? ? ?setNullImageView(mImageView);//設置當前點擊的是空方塊
? ? ? ? ? ?if(isGameStart){
? ? ? ? ? ? ? ?//成功時,彈出Toast
? ? ? ? ? ? ? ?isGameOver();
? ? ? ? ? ?}
? ? ? ? ? ?return;
? ? ? ?}
? ? ? ?TranslateAnimation translateAnimation = null;
? ? ? ?if(mImageView.getX()>iv_null_ImageView.getX()){
? ? ? ? ? ?translateAnimation = new TranslateAnimation(0.1f,-mImageView.getWidth(),0.1f,0.1f);
? ? ? ?}else if(mImageView.getX()<iv_null_ImageView.getX()){
? ? ? ? ? ?translateAnimation = new TranslateAnimation(0.1f,mImageView.getWidth(),0.1f,0.1f);
? ? ? ?}else if(mImageView.getY()>iv_null_ImageView.getY()){
? ? ? ? ? ?translateAnimation = new TranslateAnimation(0.1f,0.1f,0.1f,-mImageView.getWidth());
? ? ? ?}else if(mImageView.getY()<iv_null_ImageView.getY()){
? ? ? ? ? ?translateAnimation = new TranslateAnimation(0.1f,0.1f,0.1f,mImageView.getWidth());
? ? ? ?}
? ? ? ?//設置動畫時長
? ? ? ?translateAnimation.setDuration(70);
? ? ? ?//結束設置后是否停留
? ? ? ?translateAnimation.setFillAfter(true);
? ? ? ?translateAnimation.setAnimationListener(new Animation.AnimationListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onAnimationStart(Animation animation) {
? ? ? ? ? ? ? ?isAnimRun = true;
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onAnimationEnd(Animation animation) {
? ? ? ? ? ? ? ?isAnimRun = false;
? ? ? ? ? ? ? ?mImageView.clearAnimation();
? ? ? ? ? ? ? ?GameData mGameData = (GameData) mImageView.getTag();
? ? ? ? ? ? ? ?iv_null_ImageView.setImageBitmap(mGameData.bm);
? ? ? ? ? ? ? ?GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
? ? ? ? ? ? ? ?mNullGameData.bm = mGameData.bm;
? ? ? ? ? ? ? ?mNullGameData.p_x = mGameData.p_x;
? ? ? ? ? ? ? ?mNullGameData.p_y = mGameData.p_y;
? ? ? ? ? ? ? ?setNullImageView(mImageView);//設置當前點擊的是空方塊
? ? ? ? ? ? ? ?if(isGameStart){
? ? ? ? ? ? ? ? ? ?//成功時,彈出Toast
? ? ? ? ? ? ? ? ? ?isGameOver();
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onAnimationRepeat(Animation animation) {
? ? ? ? ? ?}
? ? ? ?});
? ? ? ?//執(zhí)行動畫
? ? ? ?mImageView.startAnimation(translateAnimation);
? ?}
? ?public boolean isHasByNullImageView(ImageView mImageView){
? ? ? ?GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
? ? ? ?GameData mGameData = (GameData) mImageView.getTag();
? ? ? ?if(mNullGameData.y==mGameData.y && mGameData.x+1==mNullGameData.x){
? ? ? ? ? ?return true;
? ? ? ?}else if(mNullGameData.y==mGameData.y && mGameData.x-1==mNullGameData.x){
? ? ? ? ? ?return true;
? ? ? ?}else if(mNullGameData.y==mGameData.y+1 && mGameData.x==mNullGameData.x){
? ? ? ? ? ?return true;
? ? ? ?}else if(mNullGameData.y==mGameData.y-1 && mGameData.x==mNullGameData.x){
? ? ? ? ? ?return true;
? ? ? ?}
? ? ? ?return false;
? ?}
? ?class GameData{
? ? ? ?public int x = 0;
? ? ? ?public int y = 0;
? ? ? ?public Bitmap bm;
? ? ? ?public int p_x = 0;
? ? ? ?public int p_y = 0;
? ? ? ?public GameData(int x, int y, Bitmap bm) {
? ? ? ? ? ?super();
? ? ? ? ? ?this.x = x;
? ? ? ? ? ?this.y = y;
? ? ? ? ? ?this.bm = bm;
? ? ? ? ? ?this.p_x = x;
? ? ? ? ? ?this.p_y = y;
? ? ? ?}
? ? ? ?//判斷游戲是否結束的方法
? ? ? ?public boolean isTrue() {
? ? ? ? ? ?if(x==p_x&&y==p_y){
? ? ? ? ? ? ? ?return true;
? ? ? ? ? ?}
? ? ? ? ? ?return false;
? ? ? ?}
? ?}
}
2017-01-19
GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
boolean flag = isHasByNullImageView((ImageView)v);
我看日志報錯就是說這兩句錯誤,我看了很多次都沒發(fā)現(xiàn)哪里錯了,老師可以幫幫我嗎,現(xiàn)在是進程序然后點擊圖片就閃退。