我正在製作益智遊戲。檢查可拖動的對象和放置的區域標籤是否相等
我隨機從一個數組中選擇一個圖像,並將其分成4部分。
我的佈局包含3個TableRows。
第一行也是一個2行的LinearLayout。此佈局將隨機選擇的圖像作爲背景,透明度爲0.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>
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;
}
}
} 所以,我的代碼,他們的方式是正確的,現在,自動放置在正確的位置放置的部分。例如,如果我將上面顯示的屏幕截圖中的第一部分拖放到上面的圖片中的任何位置,它將自動進入正確的位置,就是這樣。所有部件都會發生同樣的情況。
我想要做的是,如果我與標籤刪除部分「piece1」與標籤「piece4」 TextView的上面,我希望它拒絕的下降,根本就沒有放在部分正確的地方自動而不是.removeView(view);
我敢肯定,這與本案DragEvent.ACTION_DROP得以實施:和我想我已經從 修改我的if語句if(view.getTag()等於(「piece1」)){ 到像 if(view.getTag()。equals(dropZone.getTag())){
任何幫助將高度讚賞,因爲我拉我的頭髮在這裏。 謝謝大家提前。