OnTouchEvent , my scene go back if i touch some place in scene - android

my question can be pretty strange, but I can not understand why my scene is moving back, if I touch the screen elsewhere, please help me figure it out.
https://youtu.be/oKdwqn0HC0Y - in this video record my problem,
To better understand the problem
#Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
return true;
}
switch (event.getPointerCount()) {
case 3:
Log.e("Event", "PointerCount = " +event.getPointerCount());
return mScaleDetector.onTouchEvent(event);
case 2:
Log.e("Event", "PointerCount = " +event.getPointerCount());
return doRotationEvent(event);
case 1:
Log.e("Event", "PointerCount = " +event.getPointerCount());
return doMoveEvent(event);
}
return true;
}
private boolean doMoveEvent(MotionEvent event)
{
final int action = event.getAction();
Log.e("doMoveEvent", "action = " +action);
switch (action) {
case MotionEvent.ACTION_DOWN: {
Log.e("doMoveEvent", "action_down");
final float x = event.getX();
Log.e("doMoveEvent", " x = " +x);
final float y = event.getY();
Log.e("doMoveEvent", " y = " +y);
// Remember where we started
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_MOVE: {
final float x = event.getX();
final float y = event.getY();
// Calculate the distance moved
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
Log.e("doMoveEvent", " DX = " +dx);
Log.e("doMoveEvent", " DY = " +dy);
renderer.angleX += dy;
renderer.angleY += dx;
mLastTouchX = x;
mLastTouchY = y;
// Invalidate to request a redraw
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
}
return true;
}

I solved my problem this way
#Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
final float x = event.getX(); //(NEW)
final float y = event.getY(); //(NEW)
mLastTouchX = x; //(NEW)
mLastTouchY = y; //(NEW)
return true;
}
switch (event.getPointerCount()) {
case 3:
Log.e("Event", "PointerCount = " +event.getPointerCount());
return mScaleDetector.onTouchEvent(event);
case 2:
Log.e("Event", "PointerCount = " +event.getPointerCount());
return doRotationEvent(event);
case 1:
Log.e("Event", "PointerCount = " +event.getPointerCount());
return doMoveEvent(event);
}
return true;
}

Related

How to implement “Two Finger Drag or Scale” of canvas in Android?

I want to implement dragging and scaling of canvas on two finger.
I have tried with following approach.
Step 1 : onTouchEvent i have checked if getPointerCount == 2 then do dragging of canvas similar to single finger dragging.
Code:
if(ev.getPointerCount() == 2) {
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN: {
final float x = ev.getX(0);
final float y = ev.getY(0);
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_MOVE: {
final float x = ev.getX(0);
final float y = ev.getY(0);
// Only move if the ScaleGestureDetector isn't processing a gesture.
if (!mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
//invalidate();
break;
}
case MotionEvent.ACTION_UP: {
Log.i(TAG,"Double Touch : ACTION_UP SingleTouch:"+isSingleTouch );
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
Log.i(TAG,"Double Touch : ACTION_CANCEL SingleTouch:"+isSingleTouch );
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
Log.i(TAG,"Double Touch : ACTION_POINTER_UP SingleTouch:"+isSingleTouch );
// Extract the index of the pointer that left the touch sensor
final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
}
}
Step 2: add Scale Gesture Detector
Code:
public boolean onTouchEvent(MotionEvent ev) {
mScaleDetector.onTouchEvent(ev);
/*Some code */
}

OnTouchEvent logic for surfaceview drawing app

Below is a switch case logic for drawing on the surface, it doesn't draw properly. Not sure where am i going wrong. EDIT: class was removed as it was not helpful.
#Override
public boolean onTouchEvent(MotionEvent event) {
synchronized (mThread.getSurfaceHolder()) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_DOWN:
currentDrawingPath = null;
currentDrawingPath = new DrawingPath();
currentDrawingPath.getPath().moveTo(eventX, eventY);
startX = eventX;
startY = eventY;
canvasPaths.add(currentDrawingPath);
//invalidate();
break;
case MotionEvent.ACTION_MOVE:
float dx = Math.abs(eventX - startX);
float dy = Math.abs(eventY - startY);
currentDrawingPath.getPath().quadTo(startX, startY,
(eventX + startX)/2, (eventY + startY)/2);
startX = eventX;
startY = eventY;
//currentDrawingPath.getPath().lineTo(startX, startY);
break;
}
return true;
Following is the code that fixed the issue,
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
currentDrawingPath.getPath().lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_DOWN:
currentDrawingPath = new DrawingPath();
currentDrawingPath.setPaintForPath();
// update the starting point of the new path
currentDrawingPath.getPath().moveTo(event.getX(), event.getY());
currentDrawingPath.getPath().lineTo(event.getX(), event.getY());
canvasPaths.add(currentDrawingPath);
break;
case MotionEvent.ACTION_MOVE:
currentDrawingPath.getPath().lineTo(event.getX(), event.getY());
break;
}
return true;

How to invoke a service on pinch to zoom in android

I'm wandering that is it possible to invoke a service on pinch to zoom in/out in android, please don't mind if this is a silly doubt for you, please help me in understanding.
startService(MyService);
How to invoke this service on pinch to zoom in on top of any activity.
To detect pinch to zoom I'm using the following code in my MainActivity
private static final int INVALID_POINTER_ID = -1;
private int mActivePointerId = INVALID_POINTER_ID;
#Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
mLastTouchX = x;
mLastTouchY = y;
// Save the ID of this pointer
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
// Find the index of the active pointer and fetch its position
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
mLastTouchX = x;
mLastTouchY = y;
invalidate();
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
// Extract the index of the pointer that left the touch sensor
final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
}
}
return true;
}
Knowledge sharing would be highly appreciated. Thanks in advance.

How to get the item in listview by touch event in android, so I set background color of that item when I move event on it

I want to move the figure on list view item that contains phone number.
When I move figure from right to left, make a call. I'm failing to get that particoular item in list view to get the phone number and make a call.
My code is
#Override
arg1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
float startX = 0;
float startY;
float endX;
float endY;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = (int) event.getX();
startY = (int) event.getY();
System.out.println("startX" + startX);
System.out.println("startY" + startY);
break;
case MotionEvent.ACTION_MOVE:
endX = event.getX();
endY = (int) event.getY();
System.out.println("endX" + endX);
System.out.println("endY" + endY);
float sub = endX - startX;
System.out.println("sub" + sub);
if ((endX - startX) < 100) {
arg0.setBackgroundColor(Color.GREEN);
}
if ((endX - startX) > 100) {
try {
TextView ph_tv = (TextView) arg0
.findViewById(R.id.textnum);
String Pho_no = ph_tv.getText().toString();
arg0.setBackgroundColor(Color.RED);
Intent intent = new Intent(
Intent.ACTION_CALL, Uri
.parse("tel:" + Pho_no));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
lv.setBackgroundColor(Color.BLACK);
} catch (ActivityNotFoundException e) {
Toast.makeText(
getApplicationContext(),
"Error in your phone call"
+ e.getMessage(),
Toast.LENGTH_LONG).show();
}
section(path_name, lv1);
}
break;
case MotionEvent.ACTION_UP:
endX = (int) event.getX();
endY = (int) event.getY();
}
return true;
}
});
float hX = Float.NaN, historicY = Float.NaN;
static final TRIGGER_DELTA = 50; // Number of pixels to travel till trigger
#Override public boolean onTouchEvent(MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
hX = e.getX();
historicY = e.getY();
break;
case MotionEvent.ACTION_UP:
if (e.getX() - hX > -TRIGGER_DELTA) {
onSlideComplete(Direction.LEFT);
return true;
}
else if (e.getX() - hX > TRIGGER_DELTA) {
onSlideComplete(Direction.RIGHT);
return true;
} break;
default:
return super.onTouchEvent(e);
}
}
enum Direction {
LEFT, RIGHT;
}
interface OnSlideCompleteListener {
void onSlideComplete(Direction dir);
}

Zoom in/out state management

I am new in Android. I am working on opengl for 2 weeks and now stucked at one point. I am able to drag the 3d image at single finger touch and zoom in/out with 2 finger touch. But after one touch, when Second time start to zoom then it is not rendering the image from the previous state but it is first zooming the 3d object and then start zoom in/out.
Here is my code. Plz have a look on it..
public class AnimateActivity extends RendererActivity
{
Object3dContainer objModel;
// touch events
private final int NONE = 0;
private final int DRAG = 0;
private final int ZOOM = 0;
// pinch to zoom
float oldDist = 100.0f;
float newDist;
int mode = 0;
private float mPreviousX;
private float mPreviousY;
private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
private GLSurfaceView mGLSurfaceView;
Renderer renderer;
#Override
public void initScene() {
scene.lights().add(new Light());
IParser parser = Parser.createParser(Parser.Type.OBJ,
getResources(),"com.example.magic3d:raw/camaro_obj", true);
parser.parse();
//mGLSurfaceView=new GLSurfaceView(this);
objModel = parser.getParsedObject();
// objModel.scale().x = objModel.scale().y = objModel.scale().z = .8f;
objModel.scale().x = objModel.scale().y = objModel.scale().z = .8f;
scene.addChild(objModel);
scene.backgroundColor().setAll(Color.WHITE);
renderer=new Renderer(scene);
}
#Override
public void updateScene() {
/*objModel.rotation().x++;
objModel.rotation().z++;*/
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mGLSurfaceView=new GLSurfaceView(this);
}
#Override public boolean onTouchEvent(MotionEvent e) {
float x = e.getX();
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN: // one touch: drag
Log.d("ShaderActivity", "mode=DRAG" );
System.out.println("Action Down");
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN: // two touches: zoom
Log.d("ShaderActivity", "mode=ZOOM" );
System.out.println("Action zoom");
oldDist = spacing(e);
if (oldDist > 10.0f) {
mode = ZOOM; // zoom
}
break;
case MotionEvent.ACTION_UP: // no mode
mode = NONE;
System.out.println("Action up");
Log.d("ShaderActivity", "mode=NONE" );
oldDist = 100.0f;
break;
case MotionEvent.ACTION_POINTER_UP: // no mode
mode = NONE;
Log.d("ShaderActivity", "mode=NONE" );
oldDist = 100.0f;
break;
case MotionEvent.ACTION_MOVE:
System.out.println("Action Move");
// rotation
if (e.getPointerCount() > 1 && mode == ZOOM) {
newDist = spacing(e);
Log.d("SPACING: ", "OldDist: " + oldDist + ", NewDist: " + newDist);
if (newDist > 15.0f) {
float scale = newDist/oldDist; // scale
// scale in the renderer
renderer.changeScale(scale);
objModel.scale().x =renderer.scaleX;
objModel.scale().y = renderer.scaleY ;
objModel.scale().z =renderer.scaleZ;
oldDist = newDist;
}
}
else if (mode == DRAG){
float dx = x - mPreviousX;
float dy = y - mPreviousY;
renderer.mAngleX += dx * TOUCH_SCALE_FACTOR;
renderer.mAngleY += dy * TOUCH_SCALE_FACTOR;
//mGLSurfaceView.requestRender();
updateScene();
objModel.rotation().x=renderer.mAngleY;
objModel.rotation().y=renderer.mAngleX;
// objModel.rotation().z=
//Toast.makeText(getApplicationContext(), "action move downl", Toast.LENGTH_SHORT).show();
// updateScene();
}
}
mPreviousX = x;
mPreviousY = y;
return true;
}
// finds spacing
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
}
Thanks....

Categories

Resources