我在android中做了一个图像益智游戏我成功地从图库中加载图像并分割并随机播放位图并制作了一个拼图现在我想检查是否已经完成了点击按钮我已尝试完成像这样,
public void checkresult(View view)
{
if(beforeshuffle.toArray().equals(aftershuffle.toArray()))
{
Toast.makeText(getApplicationContext(),"correct",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"wrong",Toast.LENGTH_SHORT).show();
}
}
但是id没有用,所以请告诉我一个逻辑来做这件事。
我的完整代码
public class SmallImageActivity extends Activity {
ImageView img;
GridView image_grid;
Bitmap bs,as;
ArrayList<Bitmap> beforeshuffle = new ArrayList<Bitmap>(9);
ArrayList<Bitmap> aftershuffle = new ArrayList<Bitmap>(9);
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.child_image);
//Getting the image chunks sent from the previous activity
// ArrayList<Bitmap> smallImage = getIntent().getParcelableArrayListExtra("small images");
//Getting the grid view and setting an adapter to it
img = (ImageView) findViewById(R.id.image);
image_grid = (GridView) findViewById(R.id.gridview);
Intent intent = getIntent();
String pathinphone = intent.getExtras().getString("path");
Log.d("path", pathinphone);
loadImageFromStorage(pathinphone);
}
private void splitImage(ImageView image, int smallimage_Numbers) {
final ArrayList<Bitmap> smallimages = new ArrayList<Bitmap>(9);
//For the number of rows and columns of the grid to be displayed
int rows, cols;
//For height and width of the small image smallimage_s
int smallimage_Height, smallimage_Width;
//To store all the small image smallimage_s in bitmap format in this list
//Getting the scaled bitmap of the source image
BitmapDrawable mydrawable = (BitmapDrawable) image.getDrawable();
Bitmap bitmap = mydrawable.getBitmap();
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
rows = cols = (int) Math.sqrt(smallimage_Numbers);
smallimage_Height = bitmap.getHeight() / rows;
smallimage_Width = bitmap.getWidth() / cols;
//xCo and yCo are the pixel positions of the image smallimage_s
int yCo = 0;
for (int x = 0; x < rows; x++) {
int xCo = 0;
for (int y = 0; y < cols; y++) {
smallimages.add(Bitmap.createBitmap(scaledBitmap, xCo, yCo, smallimage_Width, smallimage_Height));
xCo += smallimage_Width;
}
yCo += smallimage_Height;
}
Array []in=new Array[9];
for(int i=0;i<smallimages.size();i++)
{
beforeshuffle.add(smallimages.get(i));
}
Collections.shuffle(smallimages);
image_grid.setAdapter(new SmallImageAdapter(this, smallimages));
image_grid.setNumColumns((int) Math.sqrt(smallimages.size()));
image_grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
int counter=0;
int firstclick;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
counter ++;
if(counter % 2 == 0){
firstclick = position;
Bitmap data1 = smallimages.get(position);
}
else {
for(int i=0;i<smallimages.size();i++)
{
Bitmap swapImage = smallimages.get(position);
smallimages.set(position, smallimages.get(firstclick));
smallimages.set(firstclick, swapImage);
image_grid.invalidateViews();
aftershuffle.add(smallimages.get(i));
}
}
}
});
}
public void checkresult(View view)
{
if(beforeshuffle.toArray().equals(aftershuffle.toArray()))
{
Toast.makeText(getApplicationContext(),"correct",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"wrong",Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
Bitmap 类中有一个名为 sameAS 的方法可供比较,
你可以尝试下面这样的东西,
public void checkresult(View view)
{
if(beforeshuffle.size() > 0 && aftershuffle.size() > 0){
if(beforeshuffle.get(0).sameAs(aftershuffle.get(0)))
{
Toast.makeText(getApplicationContext(),"correct",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"wrong",Toast.LENGTH_SHORT).show();
}
}
}
我希望这可以帮到你,一切顺利
答案 2 :(得分:0)
我认为如果你删除toArray()
它会完成这项任务(比较2个数组与equals相同,与array1 == array2
进行比较,但比较2个数据与等于将比较它们的内容)