【问题标题】:Android: Drag and Drop ImagesAndroid:拖放图像
【发布时间】:2013-01-11 13:40:59
【问题描述】:

我有这个“拖放”的东西。

所以基本上我们正在制作一个原型,让用户(孩子)将糖果拖放到罐子里。

这些是我研究了一段时间的代码

package edu.sbcc.cs123.draganddropbasic;

import android.app.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

@SuppressWarnings("deprecation")
public class DragAndDropBasicActivity extends Activity implements OnTouchListener {
    private ImageView letterView;                       // The letter that the user drags.
    private ImageView emptyLetterView;              // The letter outline that the user is supposed to drag letterView to.
    private AbsoluteLayout mainLayout;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mainLayout = (AbsoluteLayout) findViewById(R.id.mainLayout);
        mainLayout.setOnTouchListener(this);
        letterView = (ImageView) findViewById(R.id.letterView);
        letterView.setOnTouchListener(this);

        emptyLetterView = (ImageView) findViewById(R.id.emptyLetterView);
    }

    private boolean dragging = false;
    private Rect hitRect = new Rect();

    @Override
    /**
     * NOTE:  Had significant problems when I tried to react to ACTION_MOVE on letterView.   Kept getting alternating (X,Y) 
     * locations of the motion events, which caused the letter to flicker and move back and forth.  The only solution I could 
     * find was to determine when the user had touched down on the letter, then process moves in the ACTION_MOVE 
     * associated with the mainLayout.
     */
    public boolean onTouch(View v, MotionEvent event) {
        boolean eventConsumed = true;
        int x = (int)event.getX();
        int y = (int)event.getY();

        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            if (v == letterView) {
                dragging = true;
                eventConsumed = false;
            }
        } else if (action == MotionEvent.ACTION_UP) {

            if (dragging) {
                emptyLetterView.getHitRect(hitRect);
                if (hitRect.contains(x, y))
                    setSameAbsoluteLocation(letterView, emptyLetterView);
            }
            dragging = false;
            eventConsumed = false;

        } else if (action == MotionEvent.ACTION_MOVE) {
            if (v != letterView) {
                if (dragging) {
                    setAbsoluteLocationCentered(letterView, x, y);
                }
            }
        }

        return eventConsumed;

    }


    private void setSameAbsoluteLocation(View v1, View v2) {
        AbsoluteLayout.LayoutParams alp2 = (AbsoluteLayout.LayoutParams) v2.getLayoutParams();
        setAbsoluteLocation(v1, alp2.x, alp2.y);
    }


    private void setAbsoluteLocationCentered(View v, int x, int y) {
        setAbsoluteLocation(v, x - v.getWidth() / 2, y - v.getHeight() / 2);
    }


    private void setAbsoluteLocation(View v, int x, int y) {
        AbsoluteLayout.LayoutParams alp = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
        alp.x = x;
        alp.y = y;
        v.setLayoutParams(alp);
    }
}

这些用于 main.xml

<?xml version="1.0" encoding="utf-8"?>


<AbsoluteLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout">

<ImageView 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/emptyLetterView" 
    android:src="@drawable/candy" 
    android:layout_x="200px" 
    android:layout_y="300px"></ImageView>

<ImageView 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/letterView" 
    android:src="@drawable/candy1" >

</ImageView>
</AbsoluteLayout>

但是,我还想添加其他要拖动的糖果。我该如何实现?

【问题讨论】:

  • 你只有 2 颗糖果。其他糖果是什么意思?你能详细说明你想要什么吗?
  • 抱歉。在该代码中,我尝试过“candy1”是可以拖动的,“candy”是放置的位置。所以我想添加另一个糖果说“candy2”,它与“candy1”具有相同的功能(也可以拖动)我做了同样的事情,将 android:id 分配给“@+id/letterView”,但是它什么也不做。它甚至不能被拖动。
  • 也就是说,它只能与一种糖果一起使用。我应该如何编辑代码以便它可以与许多糖果一起使用

标签: java android drag-and-drop


【解决方案1】:

好的,既然你是初学者,我会彻底完成这个。

您选择的方法不适合您正在尝试的内容,您可能会更好地使用自定义视图和使用 Canvas。

但是你仍然可以用这个方法做你想做的事(而且它会避免重写)

这里的问题是你有一个ImageView 当你想要它的多个实例时。

因此,既然您想要多个实例动态创建它们,首先从您的 XML 文件中删除糖果,然后按照此代码进行操作

ImageView[] candies = new ImageView[10];
 for(int i=0;i<candies.length;i++){
      candies[i]=new ImageView(this);
      // TODO: Set LayoutParams for each imageView
      // i.e. AbsoluteLayout.LayoutParams imageParams1 = new AbsoluteLayout.LayoutParams(imageWidth, imageHeight); 

     candies[i].setBackgroundResource(R.drawable.candy); 
     layout.addView(candies[i], imageParams1);
}

现在您的屏幕上添加了“n”个糖果,您需要能够移动它们。

您现在需要编辑onTouch 的代码。

现在拖动 int 代替 boolean dragging 指示正在拖动哪个视图。

也代替

setSameAbsoluteLocation(letterView, emptyLetterView);

使用

setSameAbsoluteLocation(v, emptyLetterView);

因此该代码适用于所有正在监听的视图(即所有糖果)。在MOUSE_UPMOUSE_DOWN 中的所有letterView 位置执行此操作

现在在您的MOUSE_MOVE 中,您必须执行类似的操作才能拖动适当的视图

if (v != letterView) {
    if (dragging!=-1) {
        setAbsoluteLocationCentered(letterView[dragging], x, y);
     }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多