Android:拖放图片

时间:2013-01-11 13:40:59

标签: java android drag-and-drop

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

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

这些是我一直在研究的代码

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>

但是,我还想添加其他糖果。我该如何实现呢?

1 个答案:

答案 0 :(得分:2)

好的,既然你是初学者,我会彻底解决这个问题。

您选择的方法并不适合您正在尝试的内容,您可能更适合使用自定义视图并使用“画布”。

但是你仍然可以用这种方法做你想做的事情(这样可以避免重写)

这里的问题是当你想要多个实例时,你有一个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);
}

现在,你的屏幕上添加了许多糖果,你需要能够移动它们。

现在需要编辑onTouch的代码。

现在,而不是boolean dragging拖动int表示正在拖动哪个视图。

也代替

setSameAbsoluteLocation(letterView, emptyLetterView);

使用

setSameAbsoluteLocation(v, emptyLetterView);

因此代码适用于所有听取的视图(即所有糖果)。在letterViewMOUSE_UP

中的所有MOUSE_DOWN位置执行此操作

现在,在您的MOUSE_MOVE中,您必须执行以下操作才能拖动相应的视图

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