为了账号安全,请及时绑定邮箱和手机立即绑定

在判断图片是否与空图片相邻这,为什么我点击任何方块都显示false?

在判断图片是否与空图片相邻这,为什么我点击任何方块都显示false?
package com.micasa.test3;import android.graphics.Bitmap;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.View.OnClickListener;import android.widget.GridLayout;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.Toast;import static android.R.attr.x;import static android.R.attr.y;public class MainActivity extends AppCompatActivity {    private ImageView[] [] game_arr = new ImageView[5][5];    private GridLayout rl_gl_game;    private ImageView blankImageView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Bitmap bigBm = ((BitmapDrawable)getResources().getDrawable(R.drawable.zhi)).getBitmap();        int bmwandh = bigBm.getWidth()/5;        int arrwandh = getWindowManager().getDefaultDisplay().getWidth()/5;        for (int i = 0; i <game_arr.length ; i++) {            for (int j = 0; j <game_arr[i].length ; j++) {                Bitmap bm = Bitmap.createBitmap(bigBm,j*bmwandh,i*bmwandh,bmwandh,bmwandh);                game_arr[i] [j] = new ImageView(this);                game_arr[i] [j].setLayoutParams(new RelativeLayout.LayoutParams(arrwandh,arrwandh));                game_arr[i] [j].setImageBitmap(bm);                game_arr[i] [j].setPadding(1,1,1,1);                game_arr[i] [j].setTag(new GameData(x,y,bm));                game_arr[i] [j].setOnClickListener(new OnClickListener() {                    @Override                    public void onClick(View v) {                        boolean flag = isbyNullImageView((ImageView)v);                            Toast.makeText(MainActivity.this, ""+flag, Toast.LENGTH_SHORT).show();                    }                });            }        }        rl_gl_game = (GridLayout) findViewById(R.id.rl_gl_game);        for (int i = 0; i <game_arr.length ; i++) {            for (int j = 0; j <game_arr[i].length ; j++) {                rl_gl_game.addView(game_arr[i] [j]);            }        }        setNullImageView(game_arr[4] [4]);    }    public void setNullImageView(ImageView fillImageView){        fillImageView.setImageBitmap(null);        blankImageView = fillImageView;    }    public boolean isbyNullImageView(ImageView fillImageView){        GameData blankGameData = (GameData) blankImageView.getTag();        GameData fillGameData = (GameData) fillImageView.getTag();        if (blankGameData.y==fillGameData.y&&blankGameData.x+1==fillGameData.x) {            return true;        }else if(blankGameData.y==fillGameData.y&&blankGameData.x-1==fillGameData.x){            return true;        }else if(blankGameData.x==fillGameData.x&&blankGameData.y+1==fillGameData.y) {            return true;        }else if(blankGameData.x==fillGameData.x&&blankGameData.y-1==fillGameData.y){            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;        }    }    }

正在回答

2 回答

我这个没有写完,只执行了点击可以移到空白的地方,你那个太乱了看着头疼,如果结构分开可以给你看看错误

0 回复 有任何疑惑可以回复我~

你这个太乱了

复制我这个就行

package com.textone.pintu;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

   /*利用二维数组创建若干个游戏小方块*/
   private ImageView[][] iv_game_arr = new ImageView[4][4];
   /*游戏主界面*/
   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.bg)).getBitmap();//获取一张大图
       int tuWandH = bigBm.getWidth() / 4;//每个游戏小方块的宽和高
       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].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[0][3]);
   }

   /*利用动画结束之后,交换两个方块的数据
   *
   * @param mImageView
   *        点击的方块
   */
   public void changeDataByImageView(final ImageView mImageView){
       //创建一个动画,设置好方向,移动的距离
       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) {

           }

           @Override
           public void onAnimationEnd(Animation animation) {
               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);//设置当前点击的是空方块

           }

           @Override
           public void onAnimationRepeat(Animation animation) {

           }
       });
       //执行动画
       mImageView.startAnimation(translateAnimation);
   }

   /*
   * 设置某个方块为空方块
   * @param mImageView 当前要设置为空的方块的实例
   */
   public void setNullImageView(ImageView mImageView) {
       mImageView.setImageBitmap(null);
       iv_null_ImageView = mImageView;
   }

   /*
   * 判断当前点击的方块,是否与空方块的位置关系是相邻关系
   * @param mImageView 所点击的方块
   * @return true:相邻,false:不相邻
   */
   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;
   }
   /*每个游戏小方块上要绑定数据*/
   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;
       }
   }
}


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

在判断图片是否与空图片相邻这,为什么我点击任何方块都显示false?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信