主界面
public class GameActivity extends Activity implements AdapterView.OnItemClickListener {
private static List<BitmapBean> bitmaps = new ArrayList<>();
private BitmapBean blank;
private GridAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.penguins);
cutBitmap(bitmap, 3, 3);
GridView gridView = (GridView) this.findViewById(R.id.game_grid);
adapter = new GridAdapter(this, bitmaps);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this);
}
//裁剪圖片,得到被分割後的圖片集合
public void cutBitmap(Bitmap bitmap, int col, int row) {
int width = bitmap.getWidth() / col;
int height = bitmap.getHeight() / row;
for (int i = 1; i <= col; i++) {
for (int j = 1; j <= row; j++) {
Bitmap item = Bitmap.createBitmap(bitmap, (j - 1) * width, (i - 1) * height, width, height);
bitmaps.add(new BitmapBean((i - 1) * row + j, item));
}
}
bitmaps.remove(col * row - 1);
Bitmap last = BitmapFactory.decodeResource(getResources(), R.mipmap.blank);
BitmapBean blankBitmap = new BitmapBean(col * row, last);
blank = blankBitmap;
bitmaps.add(blankBitmap);
}
@Override
protected void onDestroy() {
bitmaps.clear();
super.onDestroy();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
BitmapBean bean = bitmaps.get(position);
int blankId = blank.getId() - 1;
if (Math.abs(blankId - position) == 3) {
swapItem(blank, bean);
}
if (((blankId / 3) == (position / 3)) && Math.abs(blankId - position) == 1) {
swapItem(blank, bean);
}
swapItem(blank, bean);
adapter.notifyDataSetChanged();
if (judge()) {
Toast.makeText(this, "success", Toast.LENGTH_LONG).show();
}
}
public void swapItem(BitmapBean blankBitmap, BitmapBean bean) {
BitmapBean temp = new BitmapBean();
temp.setId(bean.getId());
temp.setBitmap(bean.getBitmap());
bean.setId(blankBitmap.getId());
bean.setBitmap(blankBitmap.getBitmap());
blankBitmap.setId(temp.getId());
blankBitmap.setBitmap(temp.getBitmap());
blank = bean;
}
public boolean judge() {
for (int i = 0; i < bitmaps.size(); i++) {
if (bitmaps.get(i).getId() != (i + 1)) {
return false;
}
}
return true;
}
}
適配器
public class GridAdapter extends BaseAdapter {
private Context mContext;
private List<BitmapBean> bitmaps;
private LayoutInflater inflater;
public GridAdapter(Context context, List<BitmapBean> bitmaps) {
this.mContext = context;
this.bitmaps = bitmaps;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return bitmaps.size();
}
@Override
public Object getItem(int position) {
return bitmaps.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView == null ? inflater.inflate(R.layout.item_game, parent, false) : convertView;
ViewHolder holder = (ViewHolder) view.getTag();
if (null == holder) {
holder = new ViewHolder();
holder.itemImage = (ImageView) view.findViewById(R.id.item_game);
view.setTag(holder);
}
Bitmap bitmap = bitmaps.get(position).getBitmap();
if (null != bitmap) {
float size = ScreenUtil.getScreenSize(mContext).widthPixels / 3;
holder.itemImage.setImageBitmap(resizeBitmap(bitmap, size, size));
}
return view;
}
//調整圖片大小
public Bitmap resizeBitmap(Bitmap bitmap, float reqWidth, float reqHeight) {
Matrix matrix = new Matrix();
matrix.postScale(reqWidth / bitmap.getWidth(), reqHeight / bitmap.getHeight());
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
static class ViewHolder {
ImageView itemImage;
}
}
BitmapBean
public class BitmapBean {
private int id;
private Bitmap bitmap;
public BitmapBean() {
}
public BitmapBean(int id, Bitmap bitmap) {
this.id = id;
this.bitmap = bitmap;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
}
主界面佈局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.delta.wenqiwang.puzzle.GameActivity">
<GridView
android:id="@+id/game_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
android:horizontalSpacing="2dp"
android:verticalSpacing="2dp"
></GridView>
</RelativeLayout>
item佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/item_game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>