android: move a view on touch move (ACTION_MOVE)
Something like this:
public class MyActivity extends Activity implements View.OnTouchListener {
TextView _view;
ViewGroup _root;
private int _xDelta;
private int _yDelta;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_root = (ViewGroup)findViewById(R.id.root);
_view = new TextView(this);
_view.setText("TextView!!!!!!!!");
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 50);
layoutParams.leftMargin = 50;
layoutParams.topMargin = 50;
layoutParams.bottomMargin = -250;
layoutParams.rightMargin = -250;
_view.setLayoutParams(layoutParams);
_view.setOnTouchListener(this);
_root.addView(_view);
}
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
_root.invalidate();
return true;
}}
In main.xml
just RelativeLayout
with @+id/root
Android move view on touch event
USe the following code to perform a simple Touch to move
:
layout_counter.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event)
{
if (currentState != State.EDIT_MOVE) return false;
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
if (view.getId() != R.id.layout_counter) return false;
switch (event.getAction())
{
case MotionEvent.ACTION_MOVE:
params.topMargin = (int) event.getRawY() - view.getHeight();
params.leftMargin = (int) event.getRawX() - (view.getWidth() / 2);
view.setLayoutParams(params);
break;
case MotionEvent.ACTION_UP:
params.topMargin = (int) event.getRawY() - view.getHeight();
params.leftMargin = (int) event.getRawX() - (view.getWidth() / 2);
view.setLayoutParams(params);
break;
case MotionEvent.ACTION_DOWN:
view.setLayoutParams(params);
break;
}
return true;
}
});
Where layout_counter
is the view you want to move.
Don't forget to put your movable elements into a FrameLayout
How to move a view alongside with the finger movements in android?
you can do it like this way :
Kotlin version
var x : Double? = 0.0
var y : Double? = 0.0
override fun onCreate(savedInstanceState: Bundle?) {
.......
var yourImage = findViewById<ImageView>(R.id.pong)
yourImage.setOnTouchListener{view , event->
when (event.action){
MotionEvent.ACTION_DOWN -> {
x = view.x.toDouble() - event.rawX
y = view.y.toDouble() - event.rawY
true
}
MotionEvent.ACTION_MOVE -> {
// animate the image on x axis line only according to your finger movements
yourImage.animate()
.x(event.rawX + x!!.toFloat())
.setDuration(0)
.start()
true
}
else -> {
true
}
}
}
}
Java version
float x ;
float y ;
protected void onCreate(Bundle savedInstanceState) {
.........
ImageView yourImage = (ImageView)findViewById(R.id.pong);
yourImage.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN : {
x = v.getX() - event.getRawX();
y = v.getY() - event.getRawY();
return true;
}
case MotionEvent.ACTION_MOVE : {
yourImage.animate()
.x(event.getRawX() + x)
.setDuration(0)
.start();
return true;
}
default: return true;
}
}
});
}
- inside ACTION_DOWN event is where you can get the x and y coordinates
when the user touch down the screen with his finger - inside ACTION_MOVE event is where you can get the x and y every time the user moves his finger , then use the x and y coordinates to animate the property(x and y) of the image view in the way you want ,
Drag a view on touch and back to origin position on touch release
top2AxOriginXCoordinate = top2AX.getX();
top2AxOriginYCoordinate = top2AX.getY();
may be this two variables are 0.0 , if it's so then told me I'll help you
Moving a View with ACTION_MOVE in Scaled Layout
For further reference, this situation can be solved by applying the parent's scale to the getRawX() methods in both occurrences. I somehow hadn't tried that...
float dX, dY;
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
dX = view.getX() - event.getRawX() / dScale;
dY = view.getY() - event.getRawY() / dScale;
break;
case MotionEvent.ACTION_MOVE:
setX(event.getRawX() / dScale + dX);
setY(event.getRawY() / dScale + dY);
break;
default:
return false;
} return true;
}
I'd also recommend putting the MOVE case first in the switch.
I suppose the reasoning behind this solution can be vaguely explained by "view positioning methods know how to handle scaling, touch callback methods don't".
Hope this will help some!
OnTouchListener ACTION_MOVE is not working while moving finger on the screen
That's because when You set onTouchListener
on buttons and first start moving on container (LinearLayout) touch event it's not passed to child (button). To make it work You have to set a listener on the container and then check if touch coordinates contains in buttons rectangle.
Code:
private Button bt1, bt2, bt3;
private LinearLayout lin;
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ------ buttons findViewByID
bt1 = (Button) findViewById(R.id.b1);
bt2 = (Button) findViewById(R.id.b2);
bt3 = (Button) findViewById(R.id.b3);
lin = findViewById(R.id.lin);
//-------set up touchlisteners for buttons
lin.setOnTouchListener(this);
//
mGestureDetector = new GestureDetector(this, this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
if (motionEvent.getAction() == MotionEvent.ACTION_MOVE)
{
Rect but1 = new Rect();
bt1.getHitRect(but1);
if (but1.contains((int) motionEvent.getX(), (int) motionEvent.getY()))
{
bt1.setBackgroundColor(getColor(R.color.colorAccent));
return true;
}
Rect but2 = new Rect();
bt2.getHitRect(but2);
if (but2.contains((int) motionEvent.getX(), (int) motionEvent.getY()))
{
bt2.setBackgroundColor(getColor(R.color.colorAccent));
return true;
}
Rect but3 = new Rect();
bt3.getHitRect(but3);
if (but3.contains((int) motionEvent.getX(), (int) motionEvent.getY()))
{
bt3.setBackgroundColor(getColor(R.color.colorAccent));
return true;
}
}
return true;
}
I tested this code and it's working fine.
Of course, You can make it better, when You have 200 buttons it will be better to load rectangles in onCreate()
than to create them every time in touching event
Related Topics
How to Enable Logcat/Console in Eclipse For Android
How to Add Calendar Events in Android
How to Get the Width and Height of an Android.Widget.Imageview
Android: How to Validate Edittext Input
How to Set Navigation Drawer to Be Opened from Right to Left
Launch Sms Application with an Intent
Where Are Shared Preferences Stored
Firestore: Permission_Denied: Missing or Insufficient Permissions
Receive Result from Dialogfragment
How to Install Google Frameworks (Play, Accounts, etc.) on a Genymotion Virtual Device
How to Return a Result (Startactivityforresult) from a Tabhost Activity
How to Get Uri from an Asset File
How to Use Disk Caching in Picasso
How to Make Ellipsize="Marquee" Always Scroll
Communication Between Activity and Service
How to Use Jni Bitmap Operations for Helping to Avoid Oom When Using Large Images
How to Fire Onlistitemclick in Listactivity with Buttons in List