Android可拖动的ImageVIew

时间:2014-08-12 13:51:00

标签: android drag-and-drop imageview ontouchlistener

我有一个想要在用户触摸和移动时在Y轴上重新定位的ImageView。我可以重新定位它但是当我移动它时它会闪烁并且不平滑。它也不能完美地跟随我的手指。有没有办法消除闪烁并改善定位?我正在使用标准的ImageView。

以下是我在OnTouch方法中的内容:

imageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN: {
                    final float y = event.getY();

                    // Remember where we started
                    mLastTouchY = y;
                }
                    break;

                case MotionEvent.ACTION_MOVE:
                    final float y = event.getY();

                    // Calculate the distance moved
                    final float dy = y - mLastTouchY;

                    // Move the object
                    mPosY += dy;

                    // Remember this touch position for the next move event
                    mLastTouchY = y;

                    imageView.setTranslationY(mPosY);
                    return true;
            }
            return false;
        }
    });

2 个答案:

答案 0 :(得分:2)

你的图像想要移动只沿Y轴而不是在哪里..如果这是你的问题这就是答案

1.从此(https://android-drag-and-drop-basic.googlecode.com/archive/942b651010cc6cad4376df1651123982c1a7474f.zip

下载项目

2.在 DragAndDropBasicActivity 类注释这些行,我是怎么做到的

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

//This method helps the image to keep center of your touch point
private void setAbsoluteLocationCentered(View v, int x, int y) {
    setAbsoluteLocation(v, x - v.getWidth() / 2, y - v.getHeight() / 2);
}

// this method help to place the image in exact position when your finger moved up (that is Action UP)
private void setAbsoluteLocation(View v, int x, int y) {
    AbsoluteLayout.LayoutParams alp = (AbsoluteLayout.LayoutParams) v.getLayoutParams();

// don't forget to comment this line

    //alp.x = x;
    alp.y = y;
    v.setLayoutParams(alp);
}

答案 1 :(得分:0)

试试这个

chatHead.setOnTouchListener(new View.OnTouchListener() {
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;
        private LayoutParams params = (LayoutParams) chatHead
                .getLayoutParams();

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                initialX = params.x;
                initialY = params.y;
                initialTouchX = event.getRawX();
                initialTouchY = event.getRawY();
                return true;
            case MotionEvent.ACTION_UP:
                return true;
            case MotionEvent.ACTION_MOVE:
                params.x = initialX
                        + (int) (event.getRawX() - initialTouchX);
                params.y = initialY
                        + (int) (event.getRawY() - initialTouchY);
                windowManager.updateViewLayout(chatHead, params);
                return true;
            }
            return false;
        }
    });