移動不了是這么回事?
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;
2016-08-03
2016-08-02
2016-08-05
你現在改好了么?我的也是移動不了,改了上面給的答案以后,只能動一個方塊然后就再也動不了了
2016-08-03
還是不能移動,我把你的代碼直接復制過去了,其它代碼沒動,然后點擊圖片以后小方塊直接就沒有了,也沒有變換位置?