移动不了是这么回事?
Toast可以执行,下面的
if(fiag){
changeDataByImageView((ImageView) v);
} 执行不了,求解答。。。
用的android studio工具写的代码;
public class MainActivity extends AppCompatActivity {
// 利用二维数组创建游戏小方块;
private ImageView[][] imageViews = new ImageView[3][5];
private GridLayout gridLayout;
private ImageView null_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.hg)).getBitmap();
int tu = bitmap.getWidth() / 5;//设置每个小方块的宽和高
for (int i = 0; i < imageViews.length; i++) {
for (int j = 0; j < imageViews[0].length; j++) {
Bitmap bm = Bitmap.createBitmap(bitmap, j * tu, i * tu, tu, tu);
imageViews[i][j] = new ImageView(this);
imageViews[i][j].setImageBitmap(bm);//设置每个小方块放 的图案
imageViews[i][j].setPadding(2, 2, 2, 2);//每个图案之间的间距
imageViews[i][j].setTag(new GameData(i, j, bm));//绑定自定义数据
imageViews[i][j].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean fiag = isHasByImageView((ImageView) v);
Toast.makeText(MainActivity.this, "是否存在" + fiag, Toast.LENGTH_SHORT).show();
if (fiag){
changeDataByImageView((ImageView) v);
}
}
});
}
}
//初始化游戏主界面,并添加若干个小方块
gridLayout = (GridLayout) findViewById(R.id.gridLayout);
for (int i = 0; i < imageViews.length; i++) {
for (int j = 0; j < imageViews[0].length; j++) {
gridLayout.addView(imageViews[i][j]);
}
}
setNullImageView(imageViews[2][4]);
}
public void changeDataByImageView(final ImageView imageView) {
//创建一个动画,设置好方向,移动的距离
TranslateAnimation translateAnimation = null;
if (imageView.getX() > null_image.getX()) {
//往上移
translateAnimation = new TranslateAnimation(0.1f, -imageView.getWidth(), 0.1f, 0.1f);
} else if (imageView.getX() < null_image.getX()) {
//往下移
translateAnimation = new TranslateAnimation(0.1f, imageView.getWidth(), 0.1f, 0.1f);
} else if (imageView.getY() > null_image.getY()) {
//往左移
translateAnimation = new TranslateAnimation(0.1f, 0.1f, 0.1f, -imageView.getWidth());
} else if (imageView.getY() < null_image.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 onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
imageView.clearAnimation();
GameData mGameData = (GameData)imageView.getTag();
null_image.setImageBitmap(mGameData.bm);
GameData mNullGameData = (GameData)null_image.getTag();
mNullGameData.bm=mGameData.bm;
mNullGameData.p_x=mGameData.p_x;
mNullGameData.p_y=mGameData.p_y;
setNullImageView(imageView);
}
});
//执行动画
imageView.startAnimation(translateAnimation);
}
public void setNullImageView(ImageView nullImageView) {
nullImageView.setImageBitmap(null);
null_image = nullImageView;
}
public boolean isHasByImageView(ImageView nullImageView) {
GameData nullGameData = (GameData) null_image.getTag();
GameData nGameData = (GameData) null_image.getTag();
nullImageView.getTag();
if (nullGameData.y == nGameData.y && nGameData.x + 1 == nullGameData.x) {
return true;
} else if (nullGameData.y == nGameData.y && nGameData.x - 1 == nullGameData.x) {
return true;
} else if (nullGameData.y == nGameData.y + 1 && nGameData.x + 1 == nullGameData.x) {
return true;
} else if (nullGameData.y == nGameData.y - 1 && nGameData.x + 1 == nullGameData.x) {
return true;
}
return false;
}
class GameData {
//小方块实际位置
int x = 0;
int y = 0;
//小方块图片位置
int p_x = 0;
int p_y = 0;
public Bitmap bm;
public GameData(int x, int y, Bitmap bm) {
super();
this.x = x;