Android controls retrieved by coordinates

Source: Internet
Author: User

Android controls retrieved by coordinates

/*** Obtain the corresponding child Control Based on coordinates
* Use ** @ param x coordinate * @ param y coordinate * @ return destination View */public View getViewAtActivity (int x, int y) in the Activity) {// obtain the container View root = getWindow () from the Activity (). getDecorView (); return findViewByXY (root, x, y);}/*** obtain the corresponding child Control Based on coordinates
* Use ** @ param x coordinate * @ param y coordinate * @ return target View */public View getViewAtViewGroup (int x, int y) {return findViewByXY (this, x, y);} private View findViewByXY (View view, int x, int y) {View targetView = null; if (view instanceof ViewGroup) {// parent container, traverse the sub-control ViewGroup v = (ViewGroup) view; for (int I = 0; I <v. getChildCount (); I ++) {targetView = findViewByXY (v. getChildAt (I), x, y); if (targetView ! = Null) {break ;}}else {targetView = getTouchTarget (view, x, y) ;}return targetView;} private View getTouchTarget (View view, int x, int y) {View targetView = null; // determines whether a view can focus on the ArrayList. TouchableViews = view. getTouchables (); for (View child: TouchableViews) {if (isTouchPointInView (child, x, y) {targetView = child; break ;}} return targetView ;} private boolean isTouchPointInView (View view, int x, int y) {int [] location = new int [2]; view. getLocationOnScreen (location); int left = location [0]; int top = location [1]; int right = left + view. getMeasuredWidth (); int bottom = top + view. getMeasuredHeight (); if (view. isClickable () & y> = top & y <= bottom & x> = left & x <= right) {return true;} return false ;}

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.