Get the co-ordinates of the screen in android application
I am developing an Android applica开发者_C百科tion 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.
精彩评论