Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android可拖动图像视图_Android_Drag And Drop_Imageview_Ontouchlistener - Fatal编程技术网

Android可拖动图像视图

Android可拖动图像视图,android,drag-and-drop,imageview,ontouchlistener,Android,Drag And Drop,Imageview,Ontouchlistener,我有一个ImageView,当用户触摸和移动时,我想在Y轴上重新定位它。我可以重新定位它,但当我移动它时,它会闪烁并且不平滑。它也不完全符合我的手指。有没有办法消除闪烁并改进定位?我使用的是标准的ImageView 以下是我在OnTouch方法中得到的: imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionE

我有一个ImageView,当用户触摸和移动时,我想在Y轴上重新定位它。我可以重新定位它,但当我移动它时,它会闪烁并且不平滑。它也不完全符合我的手指。有没有办法消除闪烁并改进定位?我使用的是标准的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;
        }
    });

您的图像只希望沿Y轴移动,而不希望在任何地方移动。。如果这是你的问题,这就是答案

1.从此()下载项目

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);
}
试试这个

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

检查这个,对我来说很好,我试过了,但它没有达到我想要的效果。我只想沿Y轴移动ImageView,而不是将其拖到整个位置