5

我是安卓新手。当前代码可以将多个图像视图拖放到单个 ImageView 上,但我无法将它们拖放到多个图像视图上。请帮助我如何修改我的代码或任何其他现有代码。

MainActivity.java

package n.f.letters;

import android.app.Activity;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements OnTouchListener {
    /** Called when the activity is first created. */
    private View selected_item = null;
    private int offset_x = 0;
    private int offset_y = 0;
    Boolean touchFlag=false;
    boolean dropFlag=false;
    LayoutParams imageParams;
    ImageView imageDrop,image1,image2,image3,image4;
    int crashX,crashY;
    Drawable dropDrawable,selectDrawable;
    Rect dropRect,selectRect;
    int topy,leftX,rightX,bottomY;

    int dropArray[]; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        ViewGroup container = (ViewGroup) findViewById(R.id.container);
        imageDrop=(ImageView) findViewById(R.id.ImgDrop);       
        image1=(ImageView) findViewById(R.id.img1);
        image2=(ImageView) findViewById(R.id.img2);
        image3=(ImageView) findViewById(R.id.img3);
        image4=(ImageView) findViewById(R.id.img4);
        container.setOnTouchListener(new View.OnTouchListener() 
        {
            public boolean onTouch(View v, MotionEvent event) 
            {
                if(touchFlag==true)
                {
                  switch (event.getActionMasked()) 
                    {
                    case MotionEvent.ACTION_DOWN :

                         topy=imageDrop.getTop();
                         leftX=imageDrop.getLeft();
                         rightX=imageDrop.getRight();   
                         bottomY=imageDrop.getBottom();

                        //opRect.
                        break;
                    case MotionEvent.ACTION_MOVE:
                        crashX=(int) event.getX();
                        crashY=(int) event.getY();


                        int x = (int) event.getX() - offset_x;
                        int y = (int) event.getY()- offset_y;                                          

                        int w = getWindowManager().getDefaultDisplay().getWidth() - 50;
                        int h = getWindowManager().getDefaultDisplay().getHeight() - 10;
                        if (x > w)
                            x = w;
                        if (y > h)
                            y = h;                      
                        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(new ViewGroup.MarginLayoutParams(  RelativeLayout.LayoutParams.WRAP_CONTENT,   RelativeLayout.LayoutParams.WRAP_CONTENT));
                        lp.setMargins(x, y, 0, 0);                  

                        //Drop Image Here                       
                        if(crashX > leftX && crashX < rightX && crashY > topy && crashY < bottomY )                     
                        {                           
                            Drawable temp=selected_item.getBackground();                            
                            imageDrop.setBackgroundDrawable(temp);
                            imageDrop.bringToFront();                           
                            dropFlag=true;
                            selected_item.setVisibility(View.INVISIBLE);
                        }
                        //Drop Image Here                       
                        selected_item.setLayoutParams(lp);
                        break;  
                    case MotionEvent.ACTION_UP:
                        //                      
                        touchFlag=false;
                        if(dropFlag==true)
                        {
                            dropFlag=false;
                        }
                        else
                        {
                            selected_item.setLayoutParams(imageParams);
                        }                       
                        break;
                    default:
                        break;
                    }
                }else
                {
                    System.err.println("Display Else Part ::->"+touchFlag);
                }               
                return true;
            }
        });

        image1.setOnTouchListener(this);
        image2.setOnTouchListener(this);
        image3.setOnTouchListener(this);
        image4.setOnTouchListener(this);
    }

    public boolean onTouch(View v, MotionEvent event) 
    {   
        switch (event.getActionMasked()) 
        {
        case MotionEvent.ACTION_DOWN:
            touchFlag=true;
            offset_x = (int) event.getX();
            offset_y = (int) event.getY();
            selected_item = v;
            imageParams=v.getLayoutParams();
            break;
        case MotionEvent.ACTION_UP:
            selected_item=null;
            touchFlag=false;
            break;
        default:
            break;
        }       
        return false;
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/ImgDrop"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="#FFF123" />

<ImageView
    android:id="@+id/img4"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="75dp"
    android:layout_marginTop="61dp"
    android:background="@drawable/ic_launcher" />

<ImageView
    android:id="@+id/img3"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignTop="@+id/img4"
    android:layout_toRightOf="@+id/ImgDrop"
    android:background="@drawable/ic_launcher" />

<ImageView
    android:id="@+id/img2"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignRight="@+id/ImgDrop"
    android:layout_alignTop="@+id/img3"
    android:layout_marginRight="23dp"
    android:background="@drawable/ic_launcher" />

<ImageView
    android:id="@+id/img1"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignTop="@+id/img2"
    android:layout_marginRight="40dp"
    android:layout_toLeftOf="@+id/img2"
    android:background="@drawable/ic_launcher" />

4

1 回答 1

1

拖放 借助 Android 拖放框架,您可以允许用户使用图形拖放手势将数据从一个视图移动到当前布局中的另一个视图。该框架包括一个拖动事件类、拖动侦听器以及辅助方法和类。

尽管该框架主要是为数据移动而设计的,但您可以将其用于其他 UI 操作。例如,您可以创建一个在用户将颜色图标拖到另一个图标上时混合颜色的应用程序。然而,本主题的其余部分描述了数据移动方面的框架。 链接见

于 2015-09-05T13:09:59.763 回答