慕粉4140763
2016-10-21 15:40
照着视频 快速实现不一样的移动拼图 写下来的程序,写到2-3的时候,我点击可滑动方块,本来应该是和空方块进行交换的,但是却没有进行交换,而本来的方块也消失了,就造成了,我点击空方块旁边有图案的方块之后,空方块还是空的,有图案的方块也消失了。
把整个Activity代码贴上来,我帮你找找原因
package com.example.shinelon.android01; 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[3][5]; /*游戏主界面*/ private GridLayout 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_tu06)).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); //根据行跟列切割成小图片 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); } } }); } } /*初始化主界面,并添加游戏方块*/ main_game = (GridLayout) findViewById(R.id.main_game); for (int i = 0; i < iv_game_arr.length; i++) { for (int j = 0; j < iv_game_arr[i].length; j++) { main_game.addView(iv_game_arr[i][j]); } } setNullImageView(iv_game_arr[2][2]); //获取摸个图片,调用setNullImageView方法 } /** * 利用动画结束之后交换两个方块的数据 */ public void changeDataByImageView(final ImageView imageView){ //创建动画,设置好方向,移动的距离 TranslateAnimation translateAnimation = null; if(imageView.getX()>iv_null_ImageView.getX()){ //当前点击的方块在空方块的下方 // 往上移动 translateAnimation =new TranslateAnimation(0.1f, -imageView.getWidth(),0.1f,0.1f); }else if(imageView.getX()<iv_null_ImageView.getX()){ //当前点击的方块在空方块的上方 // 往下移动 translateAnimation =new TranslateAnimation(0.1f, imageView.getWidth(),0.1f,0.1f); }else if(imageView.getY()>iv_null_ImageView.getY()){ //当前点击的方块在空方块的右方 // 往左移动 translateAnimation =new TranslateAnimation(0.1f, 0.1f,0.1f,-imageView.getWidth()); }else if(imageView.getY()<iv_null_ImageView.getY()){ //当前点击的方块在空方块的左方 // 往右移动 translateAnimation =new TranslateAnimation(0.1f, 0.1f,0.1f,imageView.getWidth()); } //设置动画的时长 translateAnimation.setDuration(70); //设置动画结束之后是否停留 translateAnimation.setFillAfter(true); //设置动画结束之后要真正的把数据换了 translateAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { imageView.clearAnimation(); GameData GameData = (GameData)imageView.getTag(); iv_null_ImageView.setImageBitmap(GameData.bm); GameData NullGameData = (GameData)iv_null_ImageView.getTag(); NullGameData.bm = GameData.bm; NullGameData.p_x=GameData.p_x; NullGameData.p_y =GameData.p_y; setNullImageView(imageView); } }); //执行动画 imageView.startAnimation(translateAnimation); } /** * 设置某个方块为空 */ public void setNullImageView(ImageView imageView) { imageView.setImageBitmap(null); iv_null_ImageView = imageView; } //判断当前方块是否与空的方块是相邻的关系 public boolean isHasByNullImageView(ImageView imageView) { GameData NullGameData = (GameData)iv_null_ImageView.getTag(); GameData GameData = (GameData)imageView.getTag(); if(NullGameData.y==GameData.y&&NullGameData.x==GameData.x+1){ //当前点击为上方 return true; }else if(NullGameData.y==GameData.y&&NullGameData.x==GameData.x-1){ //当前点击为下方 return true; }else if(NullGameData.y==GameData.y+1&&NullGameData.x==GameData.x){ //当前点击为左方 return true; }else if(NullGameData.y==GameData.y-1&&NullGameData.x==GameData.x){ //当前点击为右方 return true; } return false; } /**为每个小方块绑定数据*/ class GameData{ /**每个方块的实际位置x*/ public int x = 0; /**每个小方块的实际位置y*/ public int y = 0; /**每个小方块图片*/ public Bitmap bm; /**每个小方块图片的实际位置y*/ public int p_x = 0; /**每个小方块图片的实际位置y*/ public int p_y = 0; public GameData(int x, int y, Bitmap bm) { this.x = x; this.y = y; this.bm = bm; this.p_x = x; this.p_y = y; } } }
为什么交换的时候只是图片交换了,原来初始化空白的实际位置不变,导致只能原来图片只能和初始化的空白位置交换。
@阿旭_
快速实现不一样的移动拼图
15576 学习 · 67 问题
相似问题