如何将图像拖到另一个图像的顶部?

时间:2014-11-20 21:21:11

标签: android drag-and-drop ontouchlistener

我想要实现的功能是将位于屏幕底部中心的图像(imageView)拖动到屏幕中间居中的图像。我面临的问题是,当我拖动我的图标时,图标周围的实际布局会调整大小。一旦周围的布局触及屏幕的中心,这就构成了将图像拖动到屏幕的中心。但实际上这并没有发生,图像周围的布局到达了中心。此外,一旦我开始拖动它,图像就会消失。

我需要一些帮助来重新配置我的代码。我按照教程。这就是我所拥有的。

windowWidth = getWindowManager().getDefaultDisplay().getWidth();
windowHeight = getWindowManager().getDefaultDisplay().getHeight();

MarginLayoutParams marginParams = new MarginLayoutParams(ivDragObject.getLayoutParams());
marginParams.setMargins((windowWidth/24)*10, ((windowHeight/32)*8), 0, 0);

RelativeLayout.LayoutParams layoutDragObject = new RelativeLayout.LayoutParams(marginParams);
ivDragObject.setLayoutParams(layoutDragObject);

LinearLayout llDestination = (LinearLayout) findViewById(R.id.llDestination);
llDestination.setPadding(0,0,0,(windowHeight/32)*3);

MarginLayoutParams marginParamsDestination = new MarginLayoutParams(ivDestination.getLayoutParams());
marginParamsDestination.setMargins((windowWidth/24)*10, 0, (windowHeight/32)*8, 0);
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(marginParamsDestination);
ivDestination.setLayoutParams(layout);

ivDragObject.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    layoutParams = (LayoutParams) v.getLayoutParams();
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        int[] unlockPos = new int[2];
                        lockPos = new int[2];
                        ivHome.getLocationOnScreen(unlockPos);
                        endPointX = unlockPos[0];
                        endPointY = unlockPos[1];

                        break;
                    case MotionEvent.ACTION_MOVE:
                        int xCord = (int) event.getRawX();
                        int yCord = (int) event.getRawY();

                        if (xCord > windowWidth - (windowWidth/24)) {
                            xCord = windowWidth - (windowWidth/24) * 2;
                        }

                        if (yCord > windowHeight - (windowHeight/32)) { //32
                            yCord = windowHeight - (windowHeight/32) * 2;
                        }

                        layoutParams.leftMargin = xCord;
                        layoutParams.topMargin = yCord;

                        ivLock.getLocationOnScreen(lockPos);
                        v.setLayoutParams(layoutParams);

                        if (((xCord - endPointX) <= (windowWidth/24)*5 && (endPointX - xCord) <= (windowWidth/24)*4)
                                && ((endPointY - yCord) <= (windowHeight/32)*5)) {
                            Log.e(TAG, "Reached destination");

                        } else {
                            Log.e(TAG, "NOT OVERLAP");
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        int xCordUp = (int) event.getRawX();
                        int yCordUp = (int) event.getRawY();

                        if (((xCordUp - endPointX) <= (windowWidth/24)*5 && (endPointX - xCordUp) <= (windowWidth/24)*4)
                                && ((endPointY - yCordUp) <=(windowHeight/32)*5)) {
                            Log.e(TAG, "Reach destination");

                        } else {
                            layoutParams.leftMargin = (windowWidth/24)*10;
                            layoutParams.topMargin = (windowHeight/32)*8;
                            v.setLayoutParams(layoutParams);
                        }
                    }
                    return true;
                }
            });

我也有这些方法

public void onSlideTouch(View view, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        break;
    case MotionEvent.ACTION_MOVE:
        int xCord = (int) event.getRawX();
        int yCord = (int) event.getRawY();

        if (xCord > windowWidth) {
            xCord = windowWidth;
        }
        if (yCord > windowHeight) {
            yCord = windowHeight;
        }

        layoutParams.leftMargin = xCord - 25;
        layoutParams.topMargin = yCord - 75;

        view.setLayoutParams(layoutParams);
        break;
    default:
        break;

    }

}

@Override
public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {

    if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
            || (keyCode == KeyEvent.KEYCODE_POWER)
            || (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
            || (keyCode == KeyEvent.KEYCODE_CAMERA)) {

        return true;
    }
    if ((keyCode == KeyEvent.KEYCODE_HOME)) {

        return true;
    }
    return false;
}

public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER
            || (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN)
            || (event.getKeyCode() == KeyEvent.KEYCODE_POWER)) {
        return false;
    }
    if ((event.getKeyCode() == KeyEvent.KEYCODE_HOME)) {
        return true;
    }
    return false;
}

提前谢谢。

0 个答案:

没有答案