Android MotionEven.Action_MOVE - java

I have the following snippet of code (From this chathead tutorial), the point of the last switch case is to handle the drag on the view, but I can't quit understand how the logic works :
...
params.gravity = Gravity.TOP | Gravity.LEFT ;
params.x = 0;
params.y = 100;
chatHead.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
#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;
}
});
...
So when i'm going to click on my View with my finger and drag it to the Right by 4 params.x will receive O (the initialX) + 4 (since i moved it to x=4) - the initialTouchX (dont know the value of this variable at this moment), is this right ?
Thanks,

Your params.x will receive 0 (initial value) + 4 (moving delta), as this code only track delta change of finger move.

Related

How can i get the dropped image x and y coordinates?

I have an image OnTouchListener i want to know the x and y coordinates of image dropped and can i get the coordinates in points? Please help!!
imageView.setOnTouchListener(onTouchListener());
private View.OnTouchListener onTouchListener() {
return (view, 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_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = x - xDelta;
layoutParams.topMargin = y - yDelta;
layoutParams.rightMargin = 0;
layoutParams.bottomMargin = 0;
view.setLayoutParams(layoutParams);
break;
}
mainLayout.invalidate();
return true;
};
}

How to get the touch effect as in the word search games

I know how to set ontouchlistener on a button. I can touch linearlayout and move my finger on it but that finger does not detect the touch on buttons. it seems as I touch the buttons but actually I am touching linearlayout. how can I touch the buttons placed on linear layout in sequence.
layoutVertical.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int actionPeformed = event.getAction();
switch (actionPeformed) {
case MotionEvent.ACTION_DOWN: {
final float x = event.getX();
final float y = event.getY();
lastXAxis = x;
lastYAxis = y;
textView2.setText("Button pressed");
//buttons[0][0].setBackgroundColor(Color.RED);
for(int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
}
}
break;
}
case MotionEvent.ACTION_MOVE: {
final float x = event.getX();
final float y = event.getY();
final float dx = x - lastXAxis;
final float dy = y - lastYAxis;
ed4 = (EditText) findViewById(R.id.editText4);
xAxis += dx;
yAxis += dy;
ed4.setText(Float.toString(yAxis));
break;
}
case MotionEvent.ACTION_UP: {
textView2.setText("released");
buttons[0][0].setBackgroundColor(Color.WHITE);
GradientDrawable drawable = new GradientDrawable();
//drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(2, Color.MAGENTA);
drawable.setColor(Color.WHITE);
buttons[rows][cols].setBackgroundDrawable(drawable);
mp.pause();
}
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
}
return true;
}
});

Rotation of 3d object with touch in Android using "min3d" framework

I am facing problem for rotating 3d model object. when i load my 3d object in GLSerfaceView with Scene Object the rotation is not working properly.
when i touch the screen and move down means top to bottom then it is working fine as i expected. but when i touch the screen from left to right the it is not rotate as i expected.
glSurfaceView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
previousX = motionEvent.getX();
previousY = motionEvent.getY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
x = motionEvent.getX();
y = motionEvent.getY();
float deltaX = (x - previousX) / density / 2f;
float deltaY = (y - previousY) / density / 2f;
objModel.rotation().x = objModel.rotation().x + deltaY;
objModel.rotation().y = objModel.rotation().y + deltaX;
previousX = x;
previousY = y;
break;
}
return true;
}
});

Collision Between 2 views in android

I have 2 views in android and they are dragable.I want to detect collision between them.
Currently I am using this code to detect collision but I don't think its working .Please suggest what need to check the collision.
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 ParamsA = (RelativeLayout.LayoutParams) view
.getLayoutParams();
ParamsA.leftMargin = X - _xDelta;
ParamsA.topMargin = Y - _yDelta;
ParamsA.rightMargin = -250;
ParamsA.bottomMargin = -250;
for (int i = 0; i < balls.size(); i++) {
if (balls.get(i).getTag() != view.getTag()) {
RelativeLayout.LayoutParams ParamsB = (RelativeLayout.LayoutParams) balls
.get(i).getLayoutParams();
Rect b = new Rect(ParamsB.leftMargin,ParamsB.topMargin,ParamsB.rightMargin,ParamsB.bottomMargin);
Rect a = new Rect(ParamsA.leftMargin,ParamsA.topMargin,ParamsA.rightMargin,ParamsA.bottomMargin);
if(a.intersect(b))
{
Toast.makeText(getApplicationContext(), "Collision Detected", Toast.LENGTH_SHORT).show();
}
}
}
view.setLayoutParams(ParamsA);
break;
}
// _root.invalidate();
return true;
}
"balls" is array of type "View" which are laying in the screen.
I just found solution myself it works perfect in all case.
public boolean CheckCollision(View v1,View v2) {
Rect R1=new Rect(v1.getLeft(), v1.getTop(), v1.getRight(), v1.getBottom());
Rect R2=new Rect(v2.getLeft(), v2.getTop(), v2.getRight(), v2.getBottom());
return R1.intersect(R2);
}

Android drag imageview with finger behaves strange

I'm implementing imageview drag function to my application.
I have tried with below code but it's behaves strange.when I try to drag the image
it's changes the position and during the movement it's shows like zooming the image.
I want to move the image with finger smoothly.
please help me to solve this problem
#Override
public boolean onTouch(View v, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
MarginLayoutParams marginParams = new MarginLayoutParams(mImagePreView.getLayoutParams());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
_xDelta = X - marginParams.leftMargin;
_yDelta = Y - marginParams.topMargin;
break;
case MotionEvent.ACTION_MOVE:
marginParams.leftMargin = X-_xDelta;
marginParams.topMargin = Y - _yDelta;
marginParams.rightMargin = -250;
marginParams.bottomMargin = -250;
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(marginParams);
mImagePreView.setLayoutParams(layoutParams);
break;
default:
break;
}
return true;
}
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mImagePreView.getLayoutParams();
layoutParams.leftMargin += offsetX;
layoutParams.topMargin += offsetY;
And there is error:
_xDelta = X - marginParams.leftMargin;
_yDelta = Y - marginParams.topMargin;
X - events in absolute coordinates, marginParams.leftMargin - coordinates are relative to the parent layout

Categories

Resources