交换位图(n-puzzle,Android)

时间:2017-03-12 15:47:06

标签: android gridview bitmap

我目前正在制作一款n-puzzle游戏,我想知道如何交换位图。 我正在使用位图的ArrayList来存储切片图像,我将它们显示在GridView中,我想将空白图像与我点击的图像交换。这是我的代码:

public class ImageAdapter extends BaseAdapter {


    private Context mContext;
    private ArrayList<Bitmap> chunkedImages;
    private int imageWidth;
    private int imageHeight;

    public ImageAdapter(Context c, ImageView image, int chunkNumbers) {
        this.imageHeight = (int) (image.getHeight() / Math.sqrt(chunkNumbers));
        this.imageWidth = (int) (image.getWidth() / Math.sqrt(chunkNumbers));
        mContext = c;
        int rows, cols;
        int chunkHeight, chunkWidth;
        chunkedImages = new ArrayList<Bitmap>(chunkNumbers);

        BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);

        rows = cols = (int) Math.sqrt(chunkNumbers);
        chunkHeight = bitmap.getHeight() / rows;
        chunkWidth = bitmap.getWidth() / cols;

        //xCoord and yCoord are the pixel positions of the image chunks
        int yCoord = 0;
        for (int x = 0; x < rows; x++) {
            int xCoord = 0;
            for (int y = 0; y < cols; y++) {
                chunkedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
                xCoord += chunkWidth;
            }
            yCoord += chunkHeight;
        }

    }


    @Override
    public int getCount() {
        return chunkedImages.size();
    }

    public Object getItem(int position) {
        return chunkedImages.get(position);
    }

    public long getItemId(int position) {
        return position;
    }


    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(imageWidth-1, imageHeight-1));
            imageView.setPadding(4,4,4,4);


        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageBitmap(chunkedImages.get(position));

        chunkedImages.set(chunkedImages.size()-1,null);


        return imageView;
    }



    // references to our images
    /*public int[] mThumbIds = {


    };*/
}

GameActivity:

public class game_activity extends AppCompatActivity {
    ImageView img;
    GridView grid;
    ImageAdapter imageAdapter;
    int masterTilePos = 15;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_activity);

        Intent i = getIntent();
        String name = i.getStringExtra("ID_IMG");

        Bitmap bmp = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier(name, "drawable",getPackageName()));

        img = new ImageView(this);
        img.setImageBitmap(bmp);
        ImageAdapter adpt = new ImageAdapter(this, img, 9);

        grid = (GridView) findViewById(R.id.grid);
        grid.setAdapter(adpt);
        grid.setNumColumns(3);

    }

}

0 个答案:

没有答案