17th days Android Touch event Learning 4 get finger Touch position

Source: Internet
Author: User


For more information about the touch event Learning Series, see:

Android Touch event Learning Series Summary


Return to the parameter MotionEvent class passed by the onTouchEvent method. The object has four methods to obtain the location information of the current finger on the screen, but one is relative address and the other is absolute address, see the differences below.

1. Differences between Android Touch events rawX, rawY, and x and y MotionEvent have four methods: getRawX (), event. getRawY (), getX (), getY (), why are two variables required for the same x and Y axes?


Let's take a look at the following:



As you can see
RawX and rawY are the relative positions at 0, 0 in the upper left corner of the screen. rawX = 223 indicates that the distance between the touch point and the leftmost part of the screen is 223.
X and y are the relative positions of the touch points with 0 and 0 in the upper left corner of the gray area, respectively. x = 96 indicates that the distance from the touch point to the leftmost of the gray area is 96.


Coordinates of rawX and rawY relative to the screen
Coordinates of x and y relative to the current control
RawX and X increase to the right and decrease to the left.
RawY and Y move down to increase, and move up to decrease


2. Corresponding Code 1. The gray area in the middle is a custom TextView, which is used to listen to Touch events. There is a LogListener interface which is used to output information at the Touch Location in Actvity in real time.



Public class CustomTextView extends TextView {private LogListener mLogListener; public CustomTextView (Context context) {super (context);} public CustomTextView (Context context, AttributeSet attrs) {super (context, attrs);} public CustomTextView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);} public void setLogListener (LogListener pListener) {mLogListener = pListener; } @ Overridepublic boolean onTouchEvent (MotionEvent event) {int action = event. getAction (); switch (action) {case MotionEvent. ACTION_MOVE: float rawX = event. getRawX (); float rawY = event. getRawY (); float x = event. getX (); float y = event. getY (); if (mLogListener! = Null) {mLogListener. output ("rawX =" + rawX + "\ n rawY =" + rawY + "\ n x =" + x + "\ n Y =" + y );} break;} return true;}/*** is used to output information at the Touch Location in real time in Actvity */public interface LogListener {public void output (String pOutput );}}


2. Configure layout in AndroidManifast. xml

 
     
              
      
 

3. Load the view in the Activity to control the touch position information.


Public class TouchDemoActivity extends Activity {private TextView mOutput; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mOutput = (TextView) findViewById (R. id. output); mOutput. setText ("rawX = 0 \ n rawY = 0 \ n x = 0 \ n Y = 0"); CustomTextView customTextView = (CustomTextView) findViewById (R. id. custom_textview); customTextView. setLogListener (new CustomLogListener ();}/*** used to obtain location information in TouchEvent */private class CustomLogListener implements LogListener {@ Overridepublic void output (String pOutput) {mOutput. setText (pOutput );}}}

Download

Download example


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.