package com.micasa.test3;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import static android.R.attr.x;
import static android.R.attr.y;
public class MainActivity extends AppCompatActivity {
private ImageView[] [] game_arr = new ImageView[5][5];
private GridLayout rl_gl_game;
private ImageView blankImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap bigBm = ((BitmapDrawable)getResources().getDrawable(R.drawable.zhi)).getBitmap();
int bmwandh = bigBm.getWidth()/5;
int arrwandh = getWindowManager().getDefaultDisplay().getWidth()/5;
for (int i = 0; i <game_arr.length ; i++) {
for (int j = 0; j <game_arr[i].length ; j++) {
Bitmap bm = Bitmap.createBitmap(bigBm,j*bmwandh,i*bmwandh,bmwandh,bmwandh);
game_arr[i] [j] = new ImageView(this);
game_arr[i] [j].setLayoutParams(new RelativeLayout.LayoutParams(arrwandh,arrwandh));
game_arr[i] [j].setImageBitmap(bm);
game_arr[i] [j].setPadding(1,1,1,1);
game_arr[i] [j].setTag(new GameData(x,y,bm));
game_arr[i] [j].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean flag = isbyNullImageView((ImageView)v);
Toast.makeText(MainActivity.this, ""+flag, Toast.LENGTH_SHORT).show();
}
});
}
}
rl_gl_game = (GridLayout) findViewById(R.id.rl_gl_game);
for (int i = 0; i <game_arr.length ; i++) {
for (int j = 0; j <game_arr[i].length ; j++) {
rl_gl_game.addView(game_arr[i] [j]);
}
}
setNullImageView(game_arr[4] [4]);
}
public void setNullImageView(ImageView fillImageView){
fillImageView.setImageBitmap(null);
blankImageView = fillImageView;
}
public boolean isbyNullImageView(ImageView fillImageView){
GameData blankGameData = (GameData) blankImageView.getTag();
GameData fillGameData = (GameData) fillImageView.getTag();
if (blankGameData.y==fillGameData.y&&blankGameData.x+1==fillGameData.x) {
return true;
}else if(blankGameData.y==fillGameData.y&&blankGameData.x-1==fillGameData.x){
return true;
}else if(blankGameData.x==fillGameData.x&&blankGameData.y+1==fillGameData.y) {
return true;
}else if(blankGameData.x==fillGameData.x&&blankGameData.y-1==fillGameData.y){
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.bm = bm;
this.p_x = x;
this.p_y = y;
}
}
}