mcfly soft mcfly soft - 1 month ago 14
Java Question

GestureListener is not recognized

I have a


  • custom
    View
    with a circle in it

  • MainActivity



I am displaying the custom
View
, which works. I can see the circle.

I have implemented the
OnGestureListener
in the
View
, but it is not recognized. All the gesture methods are never called.

What am I doing wrong?

Code:

MainActivity
:

package com.example.testpath;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rlayout = (RelativeLayout) findViewById(getResourceId("main","id"));
WorldMap worldmap = new WorldMap(this);
rlayout.addView(worldmap);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public int getResourceId(String pVariableName, String pResourcename)
{
try {
return getResources().getIdentifier(pVariableName, pResourcename, getPackageName());
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}


Custom
View
:

package com.example.testpath;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;

public class WorldMap extends View implements OnGestureListener{

// private GestureDetector gestureScanner;

static float translatex = -2200;
static float translatey = -1500;

public WorldMap(Context context) {
super(context);
// TODO Auto-generated constructor stub
}


protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
canvas.translate(translatex,translatey);
paint.setColor(Color.RED);
canvas.drawCircle(100, 100, 50, paint);
paint.setColor(Color.BLUE);
canvas.drawCircle(2290, 1590, 50, paint);
}


@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
Log.v("DEBUG TEST", "onDown ");
return false;
}

@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.v("DEBUG TEST", "onShowPress ");

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
Log.v("DEBUG TEST", "onSingleTapUp ");
return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub
float vx = e1.getRawX();
float vy = e1.getRawY();
Log.v("DEBUG TEST", "onScroll : " + vx + " " + vy);
translatex=translatex+vx;
translatey=translatey+vy;
invalidate();
return false;
}

@Override
public void onLongPress(MotionEvent e) {
Log.v("DEBUG TEST", "onLongPress ");
// TODO Auto-generated method stub
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// TODO Auto-generated method stub
Log.v("DEBUG TEST", "onFling ");
return false;
}
}

Answer

Declare a GestureDetector in WorldMap:

private GestureDetector detector;

Initialize it:

public WorldMap(Context context) {
    super(context);
    detector = new GestureDetector(context, this);
}

And override onTouchEvent():

@Override
public boolean onTouchEvent(MotionEvent event) {
    detector.onTouchEvent(event);
    return super.onTouchEvent(event);
}
Comments