Gilles30
Gilles30

Reputation: 33

View is slightly shift on touchevent

I'm trying to implement some kind of drag and drop on ImageView. It's working not too bad except one thing : when I begin to drag object, there is a slight shift on ImageView and I can't resolve this problem. For example, when I touch the view, I get on logcat :

ACTION_DOWN (margin) : 12x300 then (without moving) ACTION_MOVE : (margin) 12x245

So the wiew is immediatly redraw and it's not very pretty ! The offset on Y axis between ACTION_DOWN and ACTION_MOVE seems to be depending on bitmap.

Here is my onTouch event code :

the layout left and top margin are set on screen.

public boolean onTouch(View v, MotionEvent event) {
    FrameLayout.LayoutParams layout = (LayoutParams) v.getLayoutParams();
    if (layout != null){
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Log.d( tag, "ACTION_DOWN (margin) : " + layout.leftMargin + "x" + layout.topMargin);
    // finger position on current view
            localX = (int) event.getX();
            localY = (int) event.getY();

            // initial position
            parentX = (int) event.getRawX();
            parentY = (int) event.getRawY();

            v.bringToFront();
            Log.d(tag, "ACTION_DOWN : Local " + localX + "x" + localY);
            Log.d(tag, "ACTION_DOWN : Raw " + parentX + "x" + parentY);
        }
        else if (event.getAction() == MotionEvent.ACTION_MOVE) {
    // move current view on screen
            layout.leftMargin = (int) event.getRawX() - localX;
            layout.topMargin = (int) event.getRawY() - localY;
            Log.d(tag, "ACTION_MOVE : (margin) " + layout.leftMargin+ "x" + layout.topMargin);
            v.setLayoutParams(layout);
        }
    }
    return true;
}

Is anybody can pointed me what i am doing wrong ? Thanks.

Upvotes: 0

Views: 182

Answers (1)

user2861691
user2861691

Reputation: 96

I had a problem, which seems a bit similar. I solved it by setting gravity on the imageView's layoutParams to Gravity.NO_GRAVITY.

Upvotes: 1

Related Questions