拖放图像并在android中移动它们的位置

时间:2015-10-07 12:59:26

标签: android xml android-layout

我有一个布局,我已经放置了图像视图和文本视图文本视图位置固定在布局上但图像视图不固定当我拖动和图像并在其他地方放下所有图像的位置自动改变了。

示例:我有4个图像,我拖动3个图像并放在1个图像的位置,然后1个图像放在第2个位置,第2个图像放在第3个等等。任何一个有代码请提供我没有代码这我只是创建一个包含图像视图和文本视图的简单布局

提前感谢。

1 个答案:

答案 0 :(得分:0)

这是我的回答

公共类MainActivity扩展了Activity {

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);
}

}