Android draggable ImageVIew

I have an ImageView that I want to change on the Y axis when the user touches and moves. I can move it, but when I move it, it flickers and is not smooth. It also does not follow my finger. Is there a way to remove flicker and improve positioning? I use 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; } }); 
+5
source share
2 answers

Your image wants to move. Only along the Y axis is not always the case. 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 the DragAndDropBasicActivity class comments on this line, as I did

 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); } 
+2
source

try it

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

Source: https://habr.com/ru/post/1200124/


All Articles