检查2个位图是否相同

时间:2017-11-10 07:44:23

标签: java android

我试图在android studio中做一个记忆游戏,我有以下代码,但它不起作用。谁能告诉我为什么/如何解决它?

 public void onClick1(View view) {
            if(counter<2) {
                button1[0][0].setBackgroundResource(pics[0][0]);
                clicked[0][0]=true;
                if(counter==1) {
                    for(int i= clicked.length -1; i>0;i--) {
                        for (int j=clicked[i].length -1; j>0; j--){
                            if(clicked[i][j]==true){
                                Bitmap bitmap = ((BitmapDrawable)button1[0][0].getBackground()).getBitmap();
                                Bitmap bitmap2 = ((BitmapDrawable)button1[i][j].getBackground()).getBitmap();
                                if(bitmap == bitmap2)
                                {
                                    button1[i][j].setVisibility(View.INVISIBLE);
                                    button1[0][0].setVisibility(View.INVISIBLE);
                                }
                                else {
                                    button1[0][0].setBackgroundResource(R.drawable.pic1);
                                    clicked[0][0]=false;
                                    button1[i][j].setBackgroundResource(R.drawable.pic1);
                                    clicked[i][j]=false;
                                    counter=0;
                                }
                            }
                        }
                    }
                }
                else
                    counter++;
            }
        }

5 个答案:

答案 0 :(得分:1)

对于Bitmap个对象,您可以使用方法Bitmap.sameAs()来比较2个位图是否相同。

  

布尔sameAs(其他位图)

     

给定另一个位图,如果它具有相同的尺寸,则返回true,   配置和像素数据作为此位图。如果其中任何一个不同,请返回   假。如果other为null,则返回false。

所以而不是:

if(bitmap == bitmap2)

使用:

if(bitmap.sameAs(bitmap2))

答案 1 :(得分:0)

我正在使用Bitmap.sameAs()方法对两个findNearestUsers(currentLocation,userLocations); 进行比较,并在文档中说明:

Bitmap

例如:

/**
 *  Given another bitmap, return true if it has the same dimensions, config,
 *  and pixel data as this bitmap. If any of those differ, return false.
 *  If other is null, return false.
 */

如果您手动创建位图,请确保至少他们具有相同的配置以便直观地比较它们

答案 2 :(得分:0)

你可以做这样的事情来比较位图:

private fun compareBitMap(bitmap: Bitmap, otherBitmap: Bitmap): Boolean {
    val bitMapAreSame = bitmap.sameAs(otherBitmap) &&
            (bitmap.width == otherBitmap.width &&
                    bitmap.width == otherBitmap.width &&
                    bitmap.height == otherBitmap.height &&
                    bitmap.byteCount == otherBitmap.byteCount &&
                    bitmap.density == otherBitmap.density)
    bitmap.recycle()
    otherBitmap.recycle()
    return bitMapAreSame
}

答案 3 :(得分:0)

我做了一个这样的硬检查:

private fun compareBitMap(bitmap: Bitmap, otherBitmap: Bitmap): Boolean {
    val bitMapAreSame = bitmap.sameAs(otherBitmap) &&
            (bitmap.width == otherBitmap.width &&
                    bitmap.width == otherBitmap.width &&
                    bitmap.height == otherBitmap.height &&
                    bitmap.byteCount == otherBitmap.byteCount &&
                    bitmap.density == otherBitmap.density)
    bitmap.recycle()
    otherBitmap.recycle()
    return bitMapAreSame
}

答案 4 :(得分:-1)

您可以通过此

检查其arrayByte是否相同
public boolen isSameBitmap(Bitmap bitmap1, Bitmap bitmap2) {
   ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
   bmp1.compress(Bitmap.CompressFormat.PNG, 100, stream1);
   byte[] byteArrayBitmap1 = stream.toByteArray();

   ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
   bmp2.compress(Bitmap.CompressFormat.PNG, 100, stream2);
   byte[] byteArrayBitmap2 = stream.toByteArray();

  return Arrays.equals(byteArrayBitmap1, byteArrayBitmap2);
}