java图像的条件拖放
因为我是新手,我会问一个问题,也许对你们中的许多人来说很简单,但我,我整天都在遵循教程,以了解这是如何工作的,但不幸的是,我没有那么幸运
我正在开发一个儿童应用程序,他们必须在相应的阴影上拖动动物的图像。因此,在下面的示例中,他们必须将奶牛的图像拖到奶牛的阴影上
如果他们把它拖到错误的图像上,会有一个声音提示这样说,如果他们把它拖到正确的图像上,图像将替换阴影,并发出确认声音
到目前为止,我成功地编码了将奶牛图像拖动到任何阴影上并替换它们的部分。我只想在正确的情况下更换
我将在下面添加我的代码,希望有人能帮助我
这是xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="vertical" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/animals_types"
安卓:textSize="25sp"
安卓:id="@+id/gamesCategory"
安卓:layout_alignParentTop="true"
安卓:layout_centerHorizontal="true" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_gravity="center_horizontal"
安卓:text="@string/games_cow"
安卓:paddingTop="20dp"
安卓:paddingBottom="20dp"
安卓:textSize="35sp"
安卓:id="@+id/text_cow"
安卓:layout_below="@+id/gamesCategory"
安卓:layout_centerHorizontal="true" />
<ImageView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:src="@drawable/games_cow"
安卓:layout_below="@+id/text_cow"
安卓:layout_centerHorizontal="true"
安卓:maxHeight="150dp"
安卓:maxWidth="150dp"
安卓:scaleType="fitCenter"
安卓:adjustViewBounds="true"
安卓:id="@+id/games_cow_item" />
<ImageView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:src="@drawable/games_cow_shadow"
安卓:maxHeight="150dp"
安卓:maxWidth="150dp"
安卓:scaleType="fitCenter"
安卓:adjustViewBounds="true"
安卓:layout_centerHorizontal="true"
安卓:layout_marginLeft="20dp"
安卓:layout_marginStart="20dp"
安卓:layout_below="@+id/games_cow_item"
安卓:layout_alignParentLeft="true"
安卓:layout_alignParentStart="true"
安卓:id="@+id/games_cow_shadow"/>
<ImageView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:src="@drawable/games_pig_shadow"
安卓:maxHeight="130dp"
安卓:maxWidth="130dp"
安卓:scaleType="fitCenter"
安卓:adjustViewBounds="true"
安卓:layout_centerHorizontal="true"
安卓:layout_marginRight="20dp"
安卓:layout_marginEnd="20dp"
安卓:layout_below="@+id/games_cow_item"
安卓:layout_alignParentRight="true"
安卓:layout_alignParentEnd="true"
安卓:id="@+id/games_pig_shadow"/>
<ImageView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:src="@drawable/games_horse_shadow"
安卓:maxHeight="150dp"
安卓:maxWidth="150dp"
安卓:scaleType="fitCenter"
安卓:adjustViewBounds="true"
安卓:layout_centerHorizontal="true"
安卓:layout_margin="20dp"
安卓:layout_below="@+id/games_cow_shadow"
安卓:id="@+id/games_horse_shadow"/>
<Button
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="NEXT"
安卓:id="@+id/button5"
安卓:layout_below="@+id/games_horse_shadow"
安卓:layout_alignParentRight="true"
安卓:layout_alignParentEnd="true" />
<Button
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="BACK"
安卓:id="@+id/button6"
安卓:layout_alignTop="@+id/button5"
安卓:layout_alignParentLeft="true"
安卓:layout_alignParentStart="true" />
</RelativeLayout>
这是Java:
package com.gadgetcatch.firstwords;
import 安卓.content.ClipData;
import 安卓.os.Bundle;
import 安卓.support.annotation.Nullable;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.view.DragEvent;
import 安卓.view.MotionEvent;
import 安卓.view.View;
import 安卓.widget.ImageView;
/**
* Created by Alex on 2/1/2016.
*/
public class GamesAnimals extends AppCompatActivity {
private ImageView option, choice1, choice2, choice3;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.games_animals);
安卓.support.v7.app.ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.mipmap.ic_launcher);
//View to drag
option = (ImageView)findViewById(R.id.games_cow_item);
//Views to drop unto
choice1 = (ImageView)findViewById(R.id.games_cow_shadow);
choice2 = (ImageView)findViewById(R.id.games_pig_shadow);
choice3 = (ImageView)findViewById(R.id.games_horse_shadow);
//set touch listener
option.setOnTouchListener(new ChoiceTouchListener());
//set drag listeners
choice1.setOnDragListener(new ChoiceDragListener());
choice2.setOnDragListener(new ChoiceDragListener());
choice3.setOnDragListener(new ChoiceDragListener());
}
private final class ChoiceTouchListener implements View.OnTouchListener{
public boolean onTouch(View view, MotionEvent motionEvent){
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
//setup drag
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
//start dragging the item touched
view.startDrag(data, shadowBuilder, view, 0);
return true;
}
else {
return false;
}
}
}
private class ChoiceDragListener implements View.OnDragListener{
@Override
public boolean onDrag(View v, DragEvent event) {
//handle drag events
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
//no action necessary
break;
case DragEvent.ACTION_DRAG_ENTERED:
//no action necessary
break;
case DragEvent.ACTION_DRAG_EXITED:
//no action necessary
break;
case DragEvent.ACTION_DROP:
//handle the dragged view being dropped over a drop view - asta de deasupra
//handle the dragged view being dropped over a target view -asta de jos
View view = (View) event.getLocalState();
//stop displaying the view where it was before it was dragged
view.setVisibility(View.INVISIBLE);
//view dragged item is being dropped on
ImageView dropTarget = (ImageView) v;
//view being dragged and dropped
ImageView dropped = (ImageView) view;
//update the image in the target view to reflect the data being dropped
dropTarget.setImageDrawable(dropped.getDrawable());
break;
case DragEvent.ACTION_DRAG_ENDED:
//no action necessary
break;
default:
break;
}
return true;
}
}
}
这是一张照片。以防我对自己解释得不够好
提前感谢您的建议! cow_image
# 1 楼答案
您几乎完成了:)您只需要检查图像是否正确。从你的代码中,我看到所有的逻辑都缺失了。有一百万种方法可以做到这一点,但我建议您使用视图的
TAG
属性来存储关于每个图像的信息标记可以存储在任何视图中。它可以是一个
String
,一个数字,随便什么。为了清楚起见,让我们使用String
我们将为匹配的图像/阴影设置相同的标记:然后,在您的案例ACTION_DROP中,您将检查阴影和已删除图像的两个标记是否相同,如下所示:
现在我们需要在XML中设置标记: