2017-11-10 9 views
-1

私はアンドロイドスタジオでメモリゲームをしようとしていますが、次のコードがありますが、動作しません。誰かがそれを修正する理由/方法を教えてもらえますか?2ビットマップが同じかどうかを確認してください

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++; 
      } 
     } 
+0

あなたが働いていない以上何手の込んだことはできますか?それはエラーでクラッシュしますか?はいの場合はエラーを投稿してください。クラッシュしないと、予期した結果と実際の結果がポストされます。ちょうど "働かない"というのは役に立たない。 – pleft

+0

問題が何であるか正確にご記入ください。 「働いていない」は役に立たない。 –

答えて

0

あなたは自分のarrayByte私はBitmap.sameAs()メソッドを使用して2つのBitmapを比較しています。この

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); 
} 
0

によって同じであるかどうかを確認することができますし、ドキュメントに書かれています:たとえば

/** 
* 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. 
*/ 

Bitmap dogeBitmap; 
Bitmap duckBitmap; 

if(dogeBitmap.sameAs(duckBitmap)) { 
    // they are same 
} 

あなたが、少なくとも彼らは2ビットマップが同じである場合は、比較する方法Bitmap.sameAs()を使用することができますBitmapオブジェクトの場合、視覚的に

0

それらを比較するために同じ設定を持っていることを確認し、手動でビットマップを作成している場合。

ブールsameAsの(他のビットマップ)

このビットマップのような他のビットマップ、それは同じ寸法を有する場合にtrueを返し、 設定、及び画素データが与えられます。それらのいずれかが異なる場合は、 を返します。 otherがnullの場合はfalseを返します。

ので、代わりの:

if(bitmap == bitmap2) 

用途:

if(bitmap.sameAs(bitmap2)) 
関連する問題