在android中拖放图像并移动其位置

在android中拖放图像并移动其位置,android,xml,android-layout,Android,Xml,Android Layout,我有一个布局,我把图像视图和文本视图放在它上面文本视图的位置在布局上是固定的,但是图像视图不是固定的当我在其他地方拖放图像时,所有图像的位置都会自动改变 示例:我有4个图像,我将3个图像拖放到1个图像的位置,然后1个图像将放置在第二个位置,第二个图像将放置在第三个位置,依此类推。任何一个图像都有代码,请提供我没有代码,我只创建一个包含图像视图和文本视图的简单布局 提前谢谢。这是我的答案 公共类MainActivity扩展了活动{ private ImageView option1, option

我有一个布局,我把图像视图和文本视图放在它上面文本视图的位置在布局上是固定的,但是图像视图不是固定的当我在其他地方拖放图像时,所有图像的位置都会自动改变

示例:我有4个图像,我将3个图像拖放到1个图像的位置,然后1个图像将放置在第二个位置,第二个图像将放置在第三个位置,依此类推。任何一个图像都有代码,请提供我没有代码,我只创建一个包含图像视图和文本视图的简单布局

提前谢谢。

这是我的答案

公共类MainActivity扩展了活动{

private ImageView option1, option2, choice1, choice2;
TextView tv1, tv2;
int mScrollDistance;
MyScrollView myScrollView;
LinearLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv1 = (TextView) findViewById(R.id.tv1);
    tv2 = (TextView) findViewById(R.id.tv2);

    tv1.setTag("0");
    tv2.setTag("0");

    // views to drag
    option1 = (ImageView) findViewById(R.id.option_1);
    option2 = (ImageView) findViewById(R.id.option_2);

    // views to drop onto
    choice1 = (ImageView) findViewById(R.id.choice_1);
    choice2 = (ImageView) findViewById(R.id.choice_2);

    // set touch listeners

    option1.setOnLongClickListener(new ChoiceTouchListener());
    option2.setOnLongClickListener(new ChoiceTouchListener());

    option1.setOnDragListener(new ChoiceDragListener());
    option2.setOnDragListener(new ChoiceDragListener());

    option1.setTag("1");
    option2.setTag("1");
    // set drag listeners
    choice1.setOnLongClickListener(new ChoiceTouchListener());
    choice2.setOnLongClickListener(new ChoiceTouchListener());

    choice1.setOnDragListener(new ChoiceDragListener());
    choice2.setOnDragListener(new ChoiceDragListener());

    choice1.setTag("1");
    choice2.setTag("1");
    tv1.setOnDragListener(new ChoiceDragListener());
    tv2.setOnDragListener(new ChoiceDragListener());

    myScrollView = (MyScrollView) findViewById(R.id.scroll);

    myScrollView
            .setOnScrollViewListener(new MyScrollView.OnScrollViewListener(){
                @Override
                public void onScrollChanged1(OnScrollViewListener listener) {
                    // TODO Auto-generated method stub
                    mScrollDistance = myScrollView.getScrollY();
                }
            });

}

private final class ChoiceTouchListener implements OnLongClickListener {

    @SuppressLint("NewApi")
    public boolean onTouch(View view, MotionEvent motionEvent) {

        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            // setup drag

            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                    view);
            // start dragging the item touch ed

            view.startDrag(data, shadowBuilder, view, 0);
        }
        return true;
    }

    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        ClipData data = ClipData.newPlainText("", "");
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
        // start dragging the item touch ed

        v.startDrag(data, shadowBuilder, v, 0);
        return true;
    }

}

private class ChoiceDragListener implements OnDragListener {

    @SuppressWarnings("deprecation")
    @Override
    public boolean onDrag(View v, DragEvent event) {
        // handle drag events
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            // no action necessary
            System.out.println("Started");
            break;
        case DragEvent.ACTION_DRAG_ENTERED:

            System.out.println("ENtered");
            // no action necessary
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            // no action necessary
            System.out.println("Exited");
            break;

        case DragEvent.ACTION_DRAG_LOCATION:

            int y = Math.round(v.getY());// + Math.round(event.getY());
            int translatedY = y - mScrollDistance;
            // System.out.printf("translated", "" + translatedY + " "
            // + mScrollDistance + " " + y);
            int threshold = 50;
            // make a scrolling up due the y has passed the threshold
            if (translatedY < 200) {
                // make a scroll up by 30 px
                myScrollView.smoothScrollBy(0, -30);
            }
            // make a autoscrolling down due y has passed the 500 px border
            if (translatedY + threshold > 500) {
                // make a scroll down by 30 px
                myScrollView.smoothScrollBy(0, 30);
            }
            break;

        case DragEvent.ACTION_DROP:

            View view = (View) event.getLocalState();

            if (((String) v.getTag()).equals("0")) {

            } else if (((String) v.getTag()).equals("1")) {
                ImageView dropTarget = (ImageView) v;

                // view being dragged and dropped

                ImageView dropped = (ImageView) view;

                Drawable temp_img = dropped.getBackground();

                dropped.setBackgroundDrawable(dropTarget.getBackground());

                dropTarget.setBackgroundDrawable(temp_img);
            }
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            // no action necessary
            break;
        default:
            break;
        }
        return true;
    }
}
}

public class MyScrollView extends ScrollView {

public OnScrollViewListener mListener;

public MyScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    // TODO Auto-generated method stub
    super.onScrollChanged(l, t, oldl, oldt);
    if (mListener != null) {
        mListener.onScrollChanged1(mListener);
    }
}

public void setOnScrollViewListener(OnScrollViewListener listener) {
    mListener = listener;
}

public static interface OnScrollViewListener {
    public void onScrollChanged1(OnScrollViewListener listener);
}