Android gestures - android

I am new to android.I am making an app in which I want to get all the dynamic coordinates of scroll gesture. e.g if I draw a line I want to get all the coordinates of that line.How to obtain that?
I have tried to take the values in array like this:
#Override
public boolean onScroll(MotionEvent me1, MotionEvent me2, float distanceX,float distanceY) {
x =me1.getRawX()+me2.getRawX();
xval.add((int) x);
return true;
}
#Override
public boolean onTouchEvent(MotionEvent me) {
this.gDetector.onTouchEvent(me);
onScroll(me,me,x,y);
Intent i=new Intent();
i.putIntegerArrayListExtra("key0", xval );
setResult(RESULT_OK,i);
finish();
}
return gDetector.onTouchEvent(me);
This code is able to fetch only the first coordinate of the scroll gesture

You don't get all the coordinates of the line, its inefficient. Instead you use the mathematical formula for distance from a point to a line, and see if you're close. If you're close enough (never expect the user to tap exactly on a line), then that point counts as on the line.
http://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line

Related

Setting onDragListener for my Map?

so I am using osmdroid map and am trying to set a listener when a user drags the map around like this.
mapView.setOnDragListener(new View.OnDragListener() {
public boolean onDrag(View v, DragEvent event) {
Log.i(PREFS_NAME, "X:" + String.valueOf(event.getX()));
Log.i(PREFS_NAME, "Y:" + String.valueOf(event.getY()));
return false;
}
});
Now, everything works fine, but when I put this code in program exits with an error.
Is this the right thing to use anyway as the reason for this is that I want to get notified whenever a user moves around the map. I want to be able to check where user moves the map, hence the getX and getY I am watching. The reason for this is that I can stop the map movement if it goes out of bounds I set.
Is this the right way to do this?
MapViews don't respond to touch events like onClick() and onLongPress(). What I would do is before onCreate() add this:
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
float x = ev.getX();
float y = ev.getY();
return super.dispatchTouchEvent(ev);
}
x and y will now contain the screen coordinates that the user pressed. Hope this helps!

Android: How to draw simple graphics on a large scrolling canvas

In Android, I need to be able to draw simple graphics (points, lines, circles, text) on a large scrollable canvas (i.e the phone's screen is a viewport in a much larger area). I have hunted for tutorials on how to do this without success.
"World Map FREE" on Android market is a good example of the sort of effect I need to achieve.
This must be a common problem, so I'm surprised I can't find examples.
I've had to implement something fairly recently of this sort. Basically I had coordinates in my custom view that would shift where objects would be drawn, via some auxiliarry functions:
public class BoardView extends View {
//coordinates to shift the view by
public float shiftX=0f;
public float shiftY=0f;
//used in the dragging code
public float lastX=-1f;
public float lastY=-1f;
/*...*/
//functions that take into account the shifted x and y values
//pretty straightforward
final public void drawLine(Canvas c,float x1,float y1,float x2,float y2,Paint p){
c.drawLine(x1+shiftX, y1+shiftY, x2+shiftX, y2+shiftY, p);
}
final public void drawText(Canvas c,String s,int x,int y,Paint p){
c.drawText(s, x+shiftX, y+shiftY, p);
}
/*etc*/
To actually implement the dragging, I wrote a custom onTouchEvent method to implement dragging:
public boolean onTouchEvent(MotionEvent event){
int eventaction=event.getAction();
float x=event.getX();
float y=event.getY();
switch(eventaction){
case MotionEvent.ACTION_DOWN:
time=System.currentTimeMillis();//used in the ACTION_UP case
break;
case MotionEvent.ACTION_MOVE:
if(lastX==-1){ lastX=x;lastY=y;}//initializing X, Y movement
else{//moving by the delta
if(Math.abs(x-lastX)>1 || Math.abs(y-lastY)>1){//this prevents jittery movement I experienced
shiftX+=(x-lastX);//moves the shifting variables
shiftY+=(y-lastY);//in the direction of the finger movement
lastX=x;//used to calculate the movement delta
lastY=y;
invalidate();//DON'T FORGET TO CALL THIS! this redraws the view
}
}
break;
case MotionEvent.ACTION_UP: //this segment is to see whether a press is a selection click(quick press)
//or a drag(long press)
lastX=-1;
if(System.currentTimeMillis()-time)<100)
try {
onClickEvent(x,y);//custom function to deal with selections
} catch (Exception e) {
e.printStackTrace();
}
break;
}
return true;
}
Look into tile engines, there's a lot to find on Google about them.

Get the co-ordinates of the screen in android application

I am developing an Android application and I need to handle various touch events.
Consider the following requirements:
If the user touches on the right side of the screen I want to display next screen.
If user touches on the left side of the screen I want to display previous screen.
On the whole I have about 25 screens.
How can I get the logical coordinates of the screen and trigger the required event?
You can use this method for getting screen X and Y coordinated:
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float xcoordinated =event.getX();
float ycoordinated =event.getY();
return super.onTouchEvent(event);
}
Override
public boolean onTouchEvent(MotionEvent event) {
float x =event.getRawX();
float y =event.getRawY();
return super.onTouchEvent(event);
}
Note : getX() and getY(), returns the position(x,y) of touch in relation with the View, getRawX() and getRawY() returns the position ( x,y ) in relation of the screen of your Android phone.

Android: Measure/detect covered area by a finger touch on screen (NOT only touch coordinates)

I would like to get access to the area covered by a finger for each touch event on an Android.
Every touch event will result in a coordinate pair X and Y independent of how big the finger and consequently the touch area is that triggered the event.
I was wondering if there is a way to get the area data which triggered the touch event e.g. size or coordinates NOT
Any help is much appreciated.
Thanks in advance for you answer or redirects,
Christian
The method motionEvent.getSize() should give you what you want (but the level of accuracy may vary depending on the device's screen).
You need to implement OnGestureListener.
First of all, you need to register GestureDetector in onTouchEvent
#Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return true;
}
In onShowPress you will get starting points
#Override
public void onShowPress(MotionEvent e) {
startX = e.getX();
startY = e.getY();
}
In onScroll you will get the end points.
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
endX = e2.getX();
endY = e2.getY();
}

Android - How to prevent a user interacting with Gallery

OK, I know this questions sounds weird at first, as GALLERY is designed to be user interactive, but, I like the Gallery functionality, as it suits a lot of my needs. i.e. I can give it a number of pictures, move the whole of them from right to left, or left to right and get them an animate (in my case, zoom) when one of them is selected. So all good.
I just need to do the selection programatically, which I currently have working. I just don't want the user to be able to fling, scroll, select, longpress, etc. by themselves. So no user interaction is required.
So, how can I prevent a user from doing theses things, without writting a gallery function myself (and without chopping a users fingers off!).
Thanks.
Try setting the clickable and focusable properties of your Gallery View to false.
Following worked for me:
Gallery gallery = new Gallery( ctx ) {
#Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
};
If some interaction is still left you can also try:
Gallery dotList = new Gallery( ctx ) {
#Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return true;
}
};

Categories

Resources