2017-04-13 8 views
0

私はパズルゲームを作っています。ドラッグ可能なオブジェクトとドロップのゾーンタグが等しいかどうか確認してください

ランダムにイメージを配列から選択し、4つの部分に分割します。

マイレイアウトは3つのテーブルローから構成されます。

最初の行は2行のLinearLayoutです。このレイアウトはランダムに選択された画像を透明度が0.3の背景として持つ。

2番目と3番目の行は、単に分割画像の4つの部分にすぎない。

私の質問とこれまでの記述は、私の問題を既に明らかにしていると思います。だから、

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_above="@+id/adView" 
    android:weightSum="1"> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_marginBottom="7dp" 
     android:layout_weight=".5"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:id="@+id/puzzleBackground" 
      android:weightSum="1"> 

      <TableRow 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:weightSum="1" 
       android:layout_weight=".5"> 

       <TextView 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight=".5" 
        android:id="@+id/piece1" 
        android:tag="piece1" 
        /> 

       <TextView 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight=".5" 
        android:id="@+id/piece2" 
        android:tag="piece2" 
        /> 

      </TableRow> 

      <TableRow 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:weightSum="1" 
       android:layout_weight=".5"> 

       <TextView 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight=".5" 
        android:id="@+id/piece3" 
        android:tag="piece3" 
        /> 
       <TextView 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight=".5" 
        android:id="@+id/piece4" 
        android:tag="piece4" 
        /> 

      </TableRow> 

     </LinearLayout> 

    </TableRow> 

    <TableRow 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_marginBottom="2dp" 
     android:layout_weight=".25" 
     android:weightSum="1"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="2dp" 
      android:layout_weight=".5" 
      android:id="@+id/part1" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="2dp" 
      android:layout_weight=".5" 
      android:id="@+id/part2" /> 

    </TableRow> 

    <TableRow 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight=".25" 
     android:weightSum="1"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="2dp" 
      android:layout_weight=".5" 
      android:id="@+id/part3" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="2dp" 
      android:layout_weight=".5" 
      android:id="@+id/part4" /> 

    </TableRow> 

</LinearLayout> 

enter image description here

public class Puzzle extends AppCompatActivity { 

private String[] puzzleIMGS; 
private String randomPuzzleIMG; 
private ImageView part1, part2, part3, part4; 
private TextView piece1, piece2, piece3, piece4; 
private LinearLayout puzzleBackground; 
private Bitmap bm1, bm2, bm3, bm4; 
private List<Bitmap> parts = new ArrayList<>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_puzzle); 

    //Select random image 
    puzzleIMGS = getResources().getStringArray(R.array.all_animal_imgs); 
    randomPuzzleIMG = puzzleIMGS[new Random().nextInt(puzzleIMGS.length)]; 

    //Get the elements 
    part1 = (ImageView) findViewById(R.id.part1); 
    part2 = (ImageView) findViewById(R.id.part2); 
    part3 = (ImageView) findViewById(R.id.part3); 
    part4 = (ImageView) findViewById(R.id.part4); 
    piece1 = (TextView) findViewById(R.id.piece1); 
    piece2 = (TextView) findViewById(R.id.piece2); 
    piece3 = (TextView) findViewById(R.id.piece3); 
    piece4 = (TextView) findViewById(R.id.piece4); 

    part1.setOnTouchListener(new MyTouchListener()); 
    part2.setOnTouchListener(new MyTouchListener()); 
    part3.setOnTouchListener(new MyTouchListener()); 
    part4.setOnTouchListener(new MyTouchListener()); 
    piece1.setOnDragListener(new MyDragListener()); 
    piece2.setOnDragListener(new MyDragListener()); 
    piece3.setOnDragListener(new MyDragListener()); 
    piece4.setOnDragListener(new MyDragListener()); 

    //Convert randomly selected resource image to bitmap 
    Bitmap originalBm = BitmapFactory.decodeResource(getResources(), getImageId(this, randomPuzzleIMG)); 

    //Split bitmap to 4 parts 
    bm1 = Bitmap.createBitmap(originalBm, 0, 0, (originalBm.getWidth()/2), (originalBm.getHeight()/2)); 
    bm2 = Bitmap.createBitmap(originalBm, (originalBm.getWidth()/2), 0, (originalBm.getWidth()/2), (originalBm.getHeight()/2)); 
    bm3 = Bitmap.createBitmap(originalBm, 0, (originalBm.getHeight()/2), (originalBm.getWidth()/2), (originalBm.getHeight()/2)); 
    bm4 = Bitmap.createBitmap(originalBm, (originalBm.getWidth()/2), (originalBm.getHeight()/2), (originalBm.getWidth()/2), (originalBm.getHeight()/2)); 

    //Make the background transparent 
    piece1.setBackgroundDrawable(new BitmapDrawable(getResources(), bm1)); 
    piece1.setAlpha(0.3f); 
    piece2.setBackgroundDrawable(new BitmapDrawable(getResources(), bm2)); 
    piece2.setAlpha(0.3f); 
    piece3.setBackgroundDrawable(new BitmapDrawable(getResources(), bm3)); 
    piece3.setAlpha(0.3f); 
    piece4.setBackgroundDrawable(new BitmapDrawable(getResources(), bm4)); 
    piece4.setAlpha(0.3f); 

    //Place parts in an array 
    parts.add(bm1); 
    parts.add(bm2); 
    parts.add(bm3); 
    parts.add(bm4); 

    //Shuffle the array 
    Collections.shuffle(parts); 

    //Assign the correct piece tag to each part 
    for(int i=0;i<4;i++){ 
     if(i==1) { 
      part1.setImageBitmap(parts.get(i)); 
      if (parts.get(i).equals(bm1)){ 
       part1.setTag("piece1"); 
      } else if (parts.get(i).equals(bm2)){ 
       part1.setTag("piece2"); 
      } else if (parts.get(i).equals(bm3)){ 
       part1.setTag("piece3"); 
      } else { 
       part1.setTag("piece4"); 
      } 
     } else if(i==2){ 
      part2.setImageBitmap(parts.get(i)); 
      if (parts.get(i).equals(bm1)){ 
       part2.setTag("piece1"); 
      } else if (parts.get(i).equals(bm2)){ 
       part2.setTag("piece2"); 
      } else if (parts.get(i).equals(bm3)){ 
       part2.setTag("piece3"); 
      } else { 
       part2.setTag("piece4"); 
      } 
     } else if(i==3){ 
      part3.setImageBitmap(parts.get(i)); 
      if (parts.get(i).equals(bm1)){ 
       part3.setTag("piece1"); 
      } else if (parts.get(i).equals(bm2)){ 
       part3.setTag("piece2"); 
      } else if (parts.get(i).equals(bm3)){ 
       part3.setTag("piece3"); 
      } else { 
       part3.setTag("piece4"); 
      } 
     } else { 
      part4.setImageBitmap(parts.get(i)); 
      if (parts.get(i).equals(bm1)){ 
       part4.setTag("piece1"); 
      } else if (parts.get(i).equals(bm2)){ 
       part4.setTag("piece2"); 
      } else if (parts.get(i).equals(bm3)){ 
       part4.setTag("piece3"); 
      } else { 
       part4.setTag("piece4"); 
      } 
     } 
    } 
} 

private static int getImageId(Context context, String imageName) { 
    return context.getResources().getIdentifier("drawable/" + imageName, null, context.getPackageName()); 
} 

private final class MyTouchListener implements View.OnTouchListener { 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
      ClipData data = ClipData.newPlainText("", ""); 
      View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
      view.startDrag(data, shadowBuilder, view, 0); 
      return true; 
     } else { 
      return false; 
     } 
    } 
} 

class MyDragListener implements View.OnDragListener { 

    @Override 
    public boolean onDrag(View v, DragEvent event) { 

     int action = event.getAction(); 

     switch (action) { 
      case DragEvent.ACTION_DRAG_STARTED: 
       // do nothing 
       break; 
      case DragEvent.ACTION_DRAG_ENTERED: 
       break; 
      case DragEvent.ACTION_DRAG_EXITED: 
       break; 
      case DragEvent.ACTION_DROP: 
       // Dropped, reassign View to ViewGroup 
       View view = (View) event.getLocalState(); 

       if(view.getTag().equals("piece1")){ 
        ViewGroup owner = (ViewGroup) view.getParent(); 
        owner.removeView(view); 
        piece1.setBackgroundDrawable(new BitmapDrawable(getResources(), bm1)); 
        piece1.setAlpha(0.9f); 
       } else if (view.getTag().equals("piece2")){ 
        ViewGroup owner = (ViewGroup) view.getParent(); 
        owner.removeView(view); 
        piece2.setBackgroundDrawable(new BitmapDrawable(getResources(), bm2)); 
        piece2.setAlpha(0.9f); 
       } else if (view.getTag().equals("piece3")){ 
        ViewGroup owner = (ViewGroup) view.getParent(); 
        owner.removeView(view); 
        piece3.setBackgroundDrawable(new BitmapDrawable(getResources(), bm3)); 
        piece3.setAlpha(0.9f); 
       } else if (view.getTag().equals("piece4")){ 
        ViewGroup owner = (ViewGroup) view.getParent(); 
        owner.removeView(view); 
        piece4.setBackgroundDrawable(new BitmapDrawable(getResources(), bm4)); 
        piece4.setAlpha(0.9f); 
       } 

       break; 
      case DragEvent.ACTION_DRAG_ENDED: 
       break; 
      default: 
       break; 
     } 
     return true; 
    } 
} 

} 、彼らは道それが今である私のコードは、自動的に正しい場所にドロップパーツを配置します。たとえば、最初の部分を上記のスクリーンショットからドラッグして上の図のどこにでもドロップすれば、自動的に正しい位置に移動し、それがそのままです。すべての部品で同じことが起こります。私は何をしたいか

enter image description here

私はタグ「piece4」とのTextView上記タグ「piece1」との部分を削除した場合、私はそれがドロップを拒否し、単純に部品を配置しないようにしたいです自動的に正しい場所ではなく.removeView(view);

は、私は、これはケースDragEvent.ACTION_DROPに実装する必要がありますかなり確信している:そして私は から私のif文を修正するために持っていると思うならば(view.getTag()等号(「piece1」)。){ へ何かのようなもの if(view.getTag()。equals(dropZone.getTag())){

私はここに私の髪を引っ張っているように助けていただければ幸いです。 ありがとうございます。

答えて

0

ああ、ちょうどそれを解決しました。

この

if(view.getTag().equals(v.getTag())){ 
    if(view.getTag().equals("piece1")){ 
     owner.removeView(view); 
     piece1.setBackgroundDrawable(new BitmapDrawable(getResources(), bm1)); 
     piece1.setAlpha(0.9f); 
    } else if (view.getTag().equals("piece2")){ 
     owner.removeView(view); 
     piece2.setBackgroundDrawable(new BitmapDrawable(getResources(), bm2)); 
     piece2.setAlpha(0.9f); 
    } else if (view.getTag().equals("piece3")){ 
     owner.removeView(view); 
     piece3.setBackgroundDrawable(new BitmapDrawable(getResources(), bm3)); 
     piece3.setAlpha(0.9f); 
    } else if (view.getTag().equals("piece4")) { 
     owner.removeView(view); 
     piece4.setBackgroundDrawable(new BitmapDrawable(getResources(), bm4)); 
     piece4.setAlpha(0.9f); 
    } 
} 
ようなステートメント場合、私がしなければならなかった唯一のことは、単に別の内部DragEvent.ACTION_DROPの巣私のif文でした
関連する問題