qq__4290
2016-08-02 11:34
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;
package com.example.androidtest; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; 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 Activity { // 利用二维数组创建游戏小方块; 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) nullImageView.getTag();// null_image替换为nullImageView 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 == nullGameData.x) {// nGameData.x // + // 1 // 替换为nGameData.x return true; } else if (nullGameData.y == nGameData.y - 1 && nGameData.x == nullGameData.x) {// nGameData.x // + // 1 // 替换为nGameData.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; this.y = y; this.bm = bm; } } }
public boolean isHasByImageView(ImageView nullImageView) { GameData nullGameData = (GameData) null_image.getTag(); GameData nGameData = (GameData) nullImageView.getTag();//null_image替换为nullImageView 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 == nullGameData.x) {//nGameData.x + 1 替换为nGameData.x return true; } else if (nullGameData.y == nGameData.y - 1 && nGameData.x == nullGameData.x) {//nGameData.x + 1 替换为nGameData.x return true; } return false; }
你现在改好了么?我的也是移动不了,改了上面给的答案以后,只能动一个方块然后就再也动不了了
还是不能移动,我把你的代码直接复制过去了,其它代码没动,然后点击图片以后小方块直接就没有了,也没有变换位置?
快速实现不一样的移动拼图
15576 学习 · 67 问题
相似问题