2017-07-22 16 views
0

私はアンドロイドで画像パズルゲームをやっています。ギャラリーから画像を読み込み、ビットマップを分割してシャッフルして、パズルを作ったのですか?私はこのようにしようとした最初のボタンをクリックすることで終了し、2つのbitmaparraylistが同じかどうかを見つける方法

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

を参照してください、私は願っていますあなたが

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

、このように下に何かを試すことができます

、比較することsameAsのでビットマップクラスと呼ばれますこれはあなたを助けるかもしれません、すべてのベスト

+0

正しい場合でも間違って返します。私はあなたのロジックは正しいと思うが、シャッフル前とシャッフル後のロジックが正しいかどうかチェックして言う –

0

あなたMinはバージョンが14以上でサポートされている場合は、Bitmapクラスはメソッドあなたがビットマップを比較するために、ここで使用することができます

bitmap.sameAs() 

を持っています。

方法があります説明 here

+0

opにはビットマップの同じインスタンスが追加されていますが、したがって、これは必要ではありません – njzk2

0

toArray()を削除すると、2つの配列をequalsと比較することはarray1 == array2と同じですが、2つのリストをequalsと比較すると内容が比較されます)

関連する問題