图片裁剪的使用——拼图游戏

3 篇文章 0 订阅
2 篇文章 0 订阅

主界面
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>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值