user3925833
user3925833

Reputation:

Android draggable ImageVIew

I have an ImageView that I want to reposition on the Y axis when the user touches and moves. I can reposition it but when I'm moving it it flickers and is not smooth. It also doesn't follow my finger perfectly. Is there a way to remove the flicker and improve the positioning? I'm using the standard ImageView.

Here is what I have in the OnTouch method:

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

Upvotes: 5

Views: 7487

Answers (2)

jigar
jigar

Reputation: 307

try this

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

Upvotes: 0

SaravanaRaja
SaravanaRaja

Reputation: 3406

Your image want to move Only along Y axis not Ever where.. If this is your Question this is the answer

1.Download the project from this (https://android-drag-and-drop-basic.googlecode.com/archive/942b651010cc6cad4376df1651123982c1a7474f.zip)

2.In DragAndDropBasicActivity class comment these line, how i done here

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

Upvotes: 2

Related Questions