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

最后测试出问题

老师,为什么我最后的功能是每次把我要交换的图片给弄成空的了

private ImageView[][] iv_game_arr=new ImageView[3][3];
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.pplo)).getBitmap();
   int tuWandH = bigBm.getWidth() / 3;
   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[2][2]);

}


   //交换方块数据
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);
}


   //设置某个方块为空方块
   public void setNullImageView(ImageView mImageView){
     mImageView.setImageBitmap(null);
        iv_null_ImageView=mImageView;
   }

   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.p_x=x;
       this.p_y=y;

   }
}

正在回答

1 回答

在构造函数是没有对bm初始化


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

举报

0/150
提交
取消

最后测试出问题

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