【问题标题】:Conditional Drag & Drop of images有条件的拖放图像
【发布时间】:2016-02-01 19:46:02
【问题描述】:

由于我是新手,所以我会问一个问题,对你们中的许多人来说可能很简单,但我整天都在关注教程以了解它是如何工作的,但不幸的是我没有那么幸运。

我正在构建一个儿童应用程序,他们必须将动物的图像拖到相应的阴影上。因此,在下面的示例中,他们必须将牛的图像拖到牛的阴影上。

如果他们将其拖到错误的图像上,则会有声音通知说,如果他们将其拖到正确的图像上,图像将替换阴影并发出确认声音。

到目前为止,我设法编写了将奶牛图像拖过任何阴影并替换它们的部分。我只想在正确的时候替换它。

我将在我的代码下方添加,希望有人能够帮助我!

这是xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/animals_types"
    android:textSize="25sp"
    android:id="@+id/gamesCategory"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="@string/games_cow"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:textSize="35sp"
    android:id="@+id/text_cow"
    android:layout_below="@+id/gamesCategory"
    android:layout_centerHorizontal="true" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/games_cow"
    android:layout_below="@+id/text_cow"
    android:layout_centerHorizontal="true"
    android:maxHeight="150dp"
    android:maxWidth="150dp"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:id="@+id/games_cow_item" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/games_cow_shadow"
    android:maxHeight="150dp"
    android:maxWidth="150dp"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:layout_centerHorizontal="true"
   android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    android:layout_below="@+id/games_cow_item"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/games_cow_shadow"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/games_pig_shadow"
    android:maxHeight="130dp"
    android:maxWidth="130dp"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:layout_centerHorizontal="true"
    android:layout_marginRight="20dp"
    android:layout_marginEnd="20dp"
    android:layout_below="@+id/games_cow_item"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:id="@+id/games_pig_shadow"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/games_horse_shadow"
    android:maxHeight="150dp"
    android:maxWidth="150dp"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="20dp"
    android:layout_below="@+id/games_cow_shadow"
    android:id="@+id/games_horse_shadow"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="NEXT"
    android:id="@+id/button5"
    android:layout_below="@+id/games_horse_shadow"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="BACK"
    android:id="@+id/button6"
    android:layout_alignTop="@+id/button5"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

</RelativeLayout>

这是Java:

 package com.gadgetcatch.firstwords;

import android.content.ClipData;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.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);

        android.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;
        }
    }
}

这是 .xml 的图像,以防我解释得不够好。

提前感谢您的任何建议! cow_image

【问题讨论】:

  • 您做得很好,先生,非常棒。你擅长你的工作。顺便说一句

标签: java android image if-statement drag-and-drop


【解决方案1】:

您几乎完成了 :) 您只需要检查图像是否正确。从您的代码中,我看到所有逻辑都丢失了。有上百万种方法可以做到这一点,但我建议您使用视图的 TAG 属性来存储有关每个图像的信息。

标签是您可以存储在任何视图中的内容。它可以是String、数字等。为清楚起见,让我们使用String我们将为匹配的图像/阴影设置相同的 TAG:

  • Image COW SHADOW 将带有标签“COW”,与 Image COW 相同
  • Image PIG SHADOW 将带有标签“PIG”,与 Image PIG 相同。
  • Image HORSE SHADOW 将带有标签“HORSE”,与 Image Horse 相同。

然后在你的情况下 ACTION_DROP,你将检查阴影和丢弃图像的两个标签是否相同,如下所示:

   .
   .
   .
   case DragEvent.ACTION_DROP:
       .
       .
       .
       // view dragged item is being dropped on
       ImageView dropTarget = (ImageView) v;
       // view being dragged and dropped
       ImageView dropped = (ImageView) view;

       String tagDropTarget = (String)dropTarget.getTag(),
              tagDroppedImage = (String)dropped.getTag();

       if ((tagDropTarget != null) && (tagDropTarget.equals (tagDroppedImage)) {

            // yippie! correct!!
            // update the image in the target view to reflect the data being dropped

            dropTarget.setImageDrawable(dropped.getDrawable());
        } else {
            // oppps, wrong!!!!
            .
            .
        }
        break;

现在我们需要在 XML 中设置标签:

<ImageView
    android:tag="COW"
    .
    .
    android:src="@drawable/games_cow"
    android:id="@+id/games_cow_item" />

<ImageView
    android:tag="COW"
    .
    .
    android:src="@drawable/games_cow_shadow"
    android:id="@+id/games_cow_shadow"/>

<ImageView
    android:tag="PIG"
    .
    .
    android:src="@drawable/games_pig_shadow"
    android:id="@+id/games_pig_shadow"/>

<ImageView
    android:tag="HORSE"
    .
    .
    android:src="@drawable/games_horse_shadow"
    android:id="@+id/games_horse_shadow"/>

【讨论】:

  • 非常感谢!它就像一个魅力:-) 我看到了你的个人资料,在编程方面你不仅仅是任何人:-) 我也可以给你一个建议吗......我刚开始使用 Android 开发和问题我有的是 Java 语法。你会推荐什么书或者你会推荐什么适合初学者的在线培训,这样我才能更好地理解这一点。我目前正准备开始udacity.com/courses/cs046,但恐怕我还没有要求的知识来正确理解它。感谢您的任何建议!
  • 我已经提到了该课程,因为我已经从他们那里学到了 Android 开发初学者课程和如何在 Android 中创建 课程。完成 Java 课程后,我想参加他们的 Nanodegree 付费课程以获得 Google 认证。
  • 嗯,他们中的第一个说不需要以前的计算经验,刚刚启动它,看起来很基础。去吧,那么谷歌的课程似乎是合乎逻辑的一步。但是不要小看自己,唯一的关键就是一遍一遍的练习!做这样的游戏!不要专注于让一切正常工作,而是要了解正在发生的事情。也许你以前可以单独练习 Java,但请注意,Android 应用程序有很多事情要做,一开始可能会让你不知所措。
  • 这是我的问题:-) 我已经不知所措了,因为我正在做很多我不完全理解的事情。 Java 是我的周点。我需要了解更多,这就是我要参加那门课程的原因。我这样做的方法是先让它工作,然后再尝试理解为什么我会这样做:-)事实上,如果我可以在不阅读文档的情况下将所有这些信息倒在我的脑海中......那就太好了:-)感谢您的宝贵时间!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多