【问题标题】:How to make correct image to drop in respective imageview?如何制作正确的图像以放入相应的图像视图?
【发布时间】:2015-07-20 06:16:03
【问题描述】:

在这里,我想拖放正确的图像,即字母 A 从左侧到右侧的图像,并将分数提高 1。但是在编写以下代码后,所有图像都会在视图之外消失。而且所有的图像都使分数随着正确的图像而上升。任何人都可以帮助我吗??

我的游戏活动代码:

    img1 = (ImageView) findViewById(R.id.ic_one);
    img2 = (ImageView) findViewById(R.id.ic_two);
    img3 = (ImageView) findViewById(R.id.ic_three);
    img4 = (ImageView) findViewById(R.id.ic_four);
    imgmain = (ImageView) findViewById(R.id.ic_main);

    img1.setImageResource(R.mipmap.a1);
img2.setImageResource(R.mipmap.a2);
img3.setImageResource(R.mipmap.a3);
img4.setImageResource(R.mipmap.a4);
imgmain.setImageResource(R.mipmap.a1);

    //setting touch listener

    img1.setOnTouchListener(listenTouch);
    img2.setOnTouchListener(listenTouch);
    img3.setOnTouchListener(listenTouch);
    img4.setOnTouchListener(listenTouch);
    imgmain.setOnDragListener(listenDrag);

    display = (TextView) findViewById(R.id.txtScore);

}

public View.OnTouchListener listenTouch = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //setup drag
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
            v.startDrag(data, shadowBuilder, v, 0);
           v.setVisibility(View.INVISIBLE);

            return true;
        } else {
            return false;
        }
    }
};
public View.OnDragListener listenDrag = new View.OnDragListener() {
    @Override
    public boolean onDrag(View v, DragEvent event) {

        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:


            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
               if(v.getId() == R.id.ic_main) {
                  View view = (View) event.getLocalState();
                  ViewGroup viewgroup = (ViewGroup) view.getParent();
                 viewgroup.removeView(view);

                  //change the text
                  score++;
                  display.setText("Your score is : "+score);


                  view.setVisibility(View.VISIBLE);
              } else {
                  View view = (View) event.getLocalState();
                  view.setVisibility(View.VISIBLE);
                  Context context = getApplicationContext();
                  Toast.makeText(context, "You can't drop the image here",
                          Toast.LENGTH_LONG).show();
                  break;
               }
                break;
            case DragEvent.ACTION_DRAG_ENDED:
                //no action necessary
                break;
            default:
                break;
        }

        return true;
    }
};

}

还有我的布局代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.bhuwan.firstproject.GameActivity"
android:id="@+id/layout_game">

<ImageView
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:id="@+id/ic_main"
    android:layout_alignBottom="@+id/ic_two"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="36dp" />

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/ic_one"
    android:layout_alignParentTop="true" />

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/ic_two"
    android:layout_marginTop="143dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/ic_three"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/ic_one"
    android:layout_toEndOf="@+id/ic_one" />

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/ic_four"
    android:layout_alignTop="@+id/ic_two"
    android:layout_toRightOf="@+id/ic_two"
    android:layout_toEndOf="@+id/ic_two" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/empty"
    android:id="@+id/txtScore"
    android:layout_marginBottom="60dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="42dp"
    android:layout_marginStart="42dp" />

【问题讨论】:

  • 请更好地解释你有什么。你有五个我们可以看到的图像视图。它们包含什么?字母 A-E 的图片?现在,如果您将 A 拖到 D 那么会发生什么?你没说。为什么两个 imageview 包含同一张图片?
  • 我有五个图像视图,其中一个是主视图,其他四个图像视图中的图像应该被丢弃,在四个图像视图上我有来自 a - e 的字母图像,在主视图上我有一个在四张图片中,我需要拖放正确的一张,而错误的则照常进行。 @greenapps
  • ' 所有图像在视野外消失时都会消失。 '。那是因为您立即使它们在 onToch 中不可见。不要那样做。
  • 你有没有看到 if(v.getId() == R.id.ic_main) 总是正确的?所以这对你没有帮助。相反,你应该比较 View view = (View) event.getLocalState();使用 img1 是具有正确图像的图像。不要删除。让隐形就足够了。
  • 感谢您的回复,但我仍然无法获得确切的视图 ID。

标签: android android-studio drag-and-drop


【解决方案1】:

你的逻辑有问题。我做了一些改变。试着告诉我它是否更好:

img1 = (ImageView) findViewById(R.id.ic_one);
    img2 = (ImageView) findViewById(R.id.ic_two);
    img3 = (ImageView) findViewById(R.id.ic_three);
    img4 = (ImageView) findViewById(R.id.ic_four);
    imgmain = (ImageView) findViewById(R.id.ic_main);

    img1.setImageResource(R.mipmap.a1);
    img2.setImageResource(R.mipmap.a2);
    img3.setImageResource(R.mipmap.a3);
    img4.setImageResource(R.mipmap.a4);
    imgmain.setImageResource(R.mipmap.a1);

    //setting touch listener

    img1.setOnTouchListener(listenTouch);
    img2.setOnTouchListener(listenTouch);
    img3.setOnTouchListener(listenTouch);
    img4.setOnTouchListener(listenTouch);
    imgmain.setOnDragListener(listenDrag);

    display = (TextView) findViewById(R.id.txtScore);

}

public View.OnTouchListener listenTouch = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //setup drag
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
            v.startDrag(data, shadowBuilder, v, 0);
            v.setVisibility(View.INVISIBLE);
            img1.setOnTouchListener(null);
            img2.setOnTouchListener(null);
            img3.setOnTouchListener(null);
            img4.setOnTouchListener(null);
        }
        return false;
    }
};
public View.OnDragListener listenDrag = new View.OnDragListener() {
    @Override
    public boolean onDrag(View v, DragEvent event) {

        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
            case DragEvent.ACTION_DRAG_ENTERED:
            case DragEvent.ACTION_DRAG_EXITED:
                break;
            case DragEvent.ACTION_DRAG_ENDED:
                img1.setOnTouchListener(listenDrag);
                img2.setOnTouchListener(listenDrag);
                img3.setOnTouchListener(listenDrag);
                img4.setOnTouchListener(listenDrag);
                View view = (View) event.getLocalState();
                view.setVisibility(View.VISIBLE);
                if (!getResoult()) {
                    Context context = getApplicationContext();
                    Toast.makeText(context, "You can't drop the image here", Toast.LENGTH_LONG).show();
                }
                break;
            case DragEvent.ACTION_DROP:
                //handle the dragged view being dropped over a drop view
                    View viewCorrect = (View) event.getLocalState();
                    ViewGroup viewgroup = (ViewGroup) viewCorrect.getParent();
                    viewgroup.removeView(viewCorrect);
                    //change the text
                    score++;
                    display.setText("Your score is : "+score);
                break;
        }

        return true;
    }
};

【讨论】:

  • 我很抱歉,但我真的不知道“if (!getResoult())”到底做了什么? @yshahak
  • 当查看向 DragEvent 注册的事件时,DragEvent.ACTION_DROP 他决定做什么并返回布尔值。这将在 DragEvent.ACTION_DRAG_ENDED 中发送给所有注册到 DragEvents 的视图,并通过调用 getResults() 来访问它。在您的情况下,当ACTION_DROP 从不调用(丢弃在您的注册视图之外)这意味着 getResults() 返回 false
  • 您当然应该只发布getResoult() 的代码而不是谈论它。此外,您可以解释为什么要删除触摸侦听器。没有意义。
  • “所有图像在视野之外消失”是这样做的意义。 “只需发布 getResoult() 的代码” - 做我的客人
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-19
  • 1970-01-01
  • 2021-11-10
  • 1970-01-01
相关资源
最近更新 更多