Android 根據座標擷取控制項方法

來源:互聯網
上載者:User

標籤:android   控制項   座標   

/** * 根據座標擷取相對應的子控制項<br> * 在Activity使用 *  * @param x座標 * @param y座標 * @return 目標View */public View getViewAtActivity(int x, int y) {// 從Activity裡擷取容器View root = getWindow().getDecorView();return findViewByXY(root, x, y);}/** * 根據座標擷取相對應的子控制項<br> * 在重寫ViewGroup使用 *  * @param x座標 * @param y座標 * @return 目標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) {// 父容器,遍曆子控制項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;// 判斷view是否可以聚焦ArrayList<View> 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;}

Android 根據座標擷取控制項方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.