我正在制作滑动拼图游戏(http://en.wikipedia.org/wiki/Sliding_puzzle)。我希望用户能够选择图像,然后显示该图像。
然而,在我能够做到这一点之前,我需要将他们选择的图像分成9个不同的正方形组成原始图像。
关于如何在没有画布的情况下做到这一点的任何想法?或者我应该只使用画布。
谢谢:)
答案 0 :(得分:2)
您可以使用Bitmap API。看一下这个例子:http://androidattop.blogspot.de/2012/05/splitting-image-into-smaller-chunks-in.html
在上面链接中提供的示例中,提供了一个ImageView,您可以从中获取图像的位图。然后暂时可以将拆分的部分保存到位图(ArrayList)的arraylist中。唯一剩下的就是将这些位图提供给GridView。 以下是您需要在代码中添加的方法:
private void splitImage(ImageView image, int chunkNumbers) {
//For the number of rows and columns of the grid to be displayed
int rows,cols;
//For height and width of the small image chunks
int chunkHeight,chunkWidth;
//To store all the small image chunks in bitmap format in this list
ArrayList<bitmap> chunkedImages = new ArrayList<bitmap>(chunkNumbers);
//Getting the scaled bitmap of the source image
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;
}
/* Now the chunkedImages has all the small image chunks in the form of Bitmap class.
* You can do what ever you want with this chunkedImages as per your requirement.
* I pass it to a new Activity to show all small chunks in a grid for demo.
* You can get the source code of this activity from my Google Drive Account.
*/
//Start a new activity to show these chunks into a grid
Intent intent = new Intent(ImageActivity.this, ChunkedImageActivity.class);
intent.putParcelableArrayListExtra("image chunks", chunkedImages);
startActivity(intent);
}
请注意,在上面的示例中,正在使用intent和putParcelableArrayListExtra将它们传递给新活动,但您并不需要这样做。您可以在布局中声明GridView。然后,您可以创建一个扩展BaseAdapter类并设置Images的类。最后,调用GridView的setAdapter方法。
答案 1 :(得分:0)
这可能是您的解决方案:Cutting the image in jigsaw shapes in android
Here's一个完整的例子,我特别看一下createPieces
方法:
答案 2 :(得分:-1)
以下代码段显示了如何将图像拆分/分割成更小的块/块数。
package com.android.imagesplitter;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class ImageActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.three);
Button b2 = (Button) findViewById(R.id.four);
Button b3 = (Button) findViewById(R.id.five);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
}
@Override
public void onClick(View view){
int chunkNumbers = 0;
switch (view.getId()) {
case R.id.three:
chunkNumbers = 9 ;
break;
case R.id.four:
chunkNumbers = 16 ;
break;
case R.id.five:
chunkNumbers = 25 ;
}
//Getting the source image to split
ImageView image = (ImageView) findViewById(R.id.source_image);
//invoking method to split the source image
splitImage(image, chunkNumbers);
}
private void splitImage(ImageView image, int chunkNumbers) {
//For the number of rows and columns of the grid to be displayed
int rows,cols;
//For height and width of the small image chunks
int chunkHeight,chunkWidth;
//To store all the small image chunks in bitmap format in this list
ArrayList<bitmap> chunkedImages = new ArrayList<bitmap>(chunkNumbers);
//Getting the scaled bitmap of the source image
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;
}
/* Now the chunkedImages has all the small image chunks in the form of Bitmap class.
* You can do what ever you want with this chunkedImages as per your requirement.
* I pass it to a new Activity to show all small chunks in a grid for demo.
* You can get the source code of this activity from my Google Drive Account.
*/
//Start a new activity to show these chunks into a grid
Intent intent = new Intent(ImageActivity.this, ChunkedImageActivity.class);
intent.putParcelableArrayListExtra("image chunks", chunkedImages);
startActivity(intent);
}
}
http://androidattop.blogspot.com/2012/05/splitting-image-into-smaller-chunks-in.html