zoukankan      html  css  js  c++  java
  • android 根据坐标返回触摸到的View

    //根据坐标返回触摸到的View
    private View getTouchTarget(View rootView, int x, int y) {
    View targetView = null;
    // 判断view是否可以聚焦
    ArrayList<View> touchableViews = rootView.getTouchables();
    for (View touchableView : touchableViews){
    if (isTouchPointInView(touchableView, x, y)){
    targetView = touchableView;
    break;
    }
    }
    return targetView;
    }

    //(x,y)是否在view的区域内
    private boolean isTouchPointInView(View view, int x, int y) {
    if (view == null){
    return false;
    }
    int[] position = new int[2];
    view.getLocationOnScreen(position);
    int left = position[0];
    int top = position[1];
    int right = left + view.getMeasuredWidth();
    int bottom = top + view.getMeasuredHeight();
    if (x >= left && x <= right && y >= top && y <= bottom){
    return true;
    }
    return false;
    }

    示例:
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
    //获取当前触摸到的View
    final View touchedView = getTouchTarget(mContentView,(int) ev.getX(), (int) ev.getY());
    return super.dispatchTouchEvent(ev);
    }
  • 相关阅读:
    HDU 1097
    HDU 1045
    HDU 1039 -Easier Done Than Said?
    HDU 1038
    HDU 1037 - Keep on Truckin'
    HDU 1036 - Average is not Fast Enough!
    hdu 1701 ACMer
    hdu 1711 Number Sequence(kmp)
    hdu 2087 剪花布条
    字符串匹配-KMP算法学习笔记
  • 原文地址:https://www.cnblogs.com/agilezhu/p/7640025.html
Copyright © 2011-2022 走看看