Java Code Examples for android.view.View.getLocationInWindow()

The following are Jave code examples for showing how to use getLocationInWindow() of the android.view.View class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: simple-keyboard   File: MoreKeysKeyboardView.java   Source Code and License Vote up 7 votes
@Override
public void showMoreKeysPanel(final View parentView, final Controller controller,
        final int pointX, final int pointY, final KeyboardActionListener listener) {
    mController = controller;
    mListener = listener;
    final View container = getContainerView();
    // The coordinates of panel's left-top corner in parentView's coordinate system.
    // We need to consider background drawable paddings.
    final int x = pointX - getDefaultCoordX() - container.getPaddingLeft() - getPaddingLeft();
    final int y = pointY - container.getMeasuredHeight() + container.getPaddingBottom()
            + getPaddingBottom();

    parentView.getLocationInWindow(mCoordinates);
    // Ensure the horizontal position of the panel does not extend past the parentView edges.
    final int maxX = parentView.getMeasuredWidth() - container.getMeasuredWidth();
    final int panelX = Math.max(0, Math.min(maxX, x)) + CoordinateUtils.x(mCoordinates);
    final int panelY = y + CoordinateUtils.y(mCoordinates);
    container.setX(panelX);
    container.setY(panelY);

    mOriginX = x + container.getPaddingLeft();
    mOriginY = y + container.getPaddingTop();
    controller.onShowMoreKeysPanel(this);
}
 
Example 2
Project: GitHub   File: PopupWindowProxy.java   Source Code and License Vote up 6 votes
/**
 * fix showAsDropDown when android api ver is over N
 * <p>
 * https://code.google.com/p/android/issues/detail?id=221001
 *
 * @param anchor
 * @param xoff
 * @param yoff
 * @param gravity
 */
@Override
public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
    if (isFixAndroidN && anchor != null) {
        int[] anchorLocation = new int[2];
        anchor.getLocationInWindow(anchorLocation);
        Activity activity = (Activity) anchor.getContext();

        xoff = anchorLocation[0] + xoff;
        yoff = anchorLocation[1] + anchor.getHeight() + yoff;
        setFocusable(false);
        super.showAtLocation((activity).getWindow().getDecorView(), Gravity.NO_GRAVITY, xoff, yoff);
        initSystemBar(getContentView());
        setFocusable(true);
        update();
    } else {
        if (isOverAndroidN) {
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        setFocusable(false);
        super.showAsDropDown(anchor, xoff, yoff, gravity);
        initSystemBar(getContentView());
        setFocusable(true);
        update();
    }
}
 
Example 3
Project: HeadlineNews   File: BaseActivity.java   Source Code and License Vote up 6 votes
public boolean isShouldHideInput(View v, MotionEvent event) {
    if (v != null && (v instanceof EditText)) {
        int[] leftTop = {0, 0};
        //获取输入框当前的location位置
        v.getLocationInWindow(leftTop);
        int left = leftTop[0];
        int top = leftTop[1];
        int bottom = top + v.getHeight();
        int right = left + v.getWidth();
        if (event.getX() > left && event.getX() < right
                && event.getY() > top && event.getY() < bottom) {
            // 点击的是输入框区域,保留点击EditText的事件
            return false;
        } else {
            return true;
        }
    }
    return false;
}
 
Example 4
Project: quickblox-android   File: Toaster.java   Source Code and License Vote up 6 votes
public static void shortToast(View view, Window window, @StringRes int text) {
    Toast toast = makeToast(CoreApp.getInstance().getString(text), Toast.LENGTH_SHORT);

    Rect rect = new Rect();
    window.getDecorView().getWindowVisibleDisplayFrame(rect);
    int[] viewLocation = new int[2];
    view.getLocationInWindow(viewLocation);
    int viewLeft = viewLocation[0] - rect.left;
    int viewTop = viewLocation[1] - rect.top;

    DisplayMetrics metrics = new DisplayMetrics();
    window.getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(metrics.widthPixels, View.MeasureSpec.UNSPECIFIED);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(metrics.heightPixels, View.MeasureSpec.UNSPECIFIED);
    toast.getView().measure(widthMeasureSpec, heightMeasureSpec);

    int toastY = viewTop + view.getHeight();

    toast.setGravity(Gravity.TOP | Gravity.CENTER_VERTICAL, 0, toastY);
    toast.show();
}
 
Example 5
Project: Tutors   File: TutorialLayout.java   Source Code and License Vote up 6 votes
public void showTutorial(View view, CharSequence text, boolean isLast) {
    this.isLast = isLast;

    if (this.bitmap != null) {
        this.bitmap.recycle();
    }
    if (this.lastTutorialView != null) {
        this.lastTutorialView.setDrawingCacheEnabled(false);
    }

    final int[] location = new int[2];
    this.lastTutorialView = view;
    view.getLocationInWindow(location);
    view.setDrawingCacheEnabled(true);

    this.text.setText(text);
    this.bitmap = view.getDrawingCache();
    this.x = location[0];
    this.y = location[1] - getStatusBarHeight();

    this.setVisibility(View.VISIBLE);
    moveText(!inTop());
    postInvalidate();
}
 
Example 6
Project: BrotherWeather   File: AppUtils.java   Source Code and License Vote up 6 votes
public static boolean isEventInVIew(final View view, final MotionEvent event) {
  if (view != null) {
    int[] leftTop = { 0, 0 };
    //获取view当前的location位置
    view.getLocationInWindow(leftTop);
    int left = leftTop[0];
    int top = leftTop[1];
    int bottom = top + view.getHeight();
    int right = left + view.getWidth();
    if (event.getRawX() > left
        && event.getRawX() < right
        && event.getRawY() > top
        && event.getRawY() < bottom) {
      return true;
    } else {
      return false;
    }
  }
  return false;
}
 
Example 7
Project: chromium-for-android-56-debug-video   File: ViewUtils.java   Source Code and License Vote up 5 votes
/**
 * Helper for overriding {@link ViewGroup#gatherTransparentRegion} for views that are fully
 * opaque and have children extending beyond their bounds. If the transparent region
 * optimization is turned on (which is the case whenever the view hierarchy contains a
 * SurfaceView somewhere), the children might otherwise confuse the SurfaceFlinger.
 */
public static void gatherTransparentRegionsForOpaqueView(View view, Region region) {
    view.getLocationInWindow(sLocationTmp);
    region.op(sLocationTmp[0], sLocationTmp[1],
            sLocationTmp[0] + view.getRight() - view.getLeft(),
            sLocationTmp[1] + view.getBottom() - view.getTop(), Region.Op.DIFFERENCE);
}
 
Example 8
Project: GitHub   File: KeyboardActivity.java   Source Code and License Vote up 5 votes
private boolean isShouldHideKeyboard(View v, MotionEvent event) {
    if (v != null && (v instanceof EditText)) {
        int[] l = {0, 0};
        v.getLocationInWindow(l);
        int left = l[0],
                top = l[1],
                bottom = top + v.getHeight(),
                right = left + v.getWidth();
        return !(event.getX() > left && event.getX() < right
                && event.getY() > top && event.getY() < bottom);
    }
    return false;
}
 
Example 9
Project: PlayAndroid   File: BaseActivity.java   Source Code and License Vote up 5 votes
public boolean isShouldHideInput(View v, MotionEvent event) {
    if (v != null && (v instanceof EditText)) {
        int[] leftTop = {0, 0};
        // 获取输入框当前的location位置
        v.getLocationInWindow(leftTop);
        int left = leftTop[0];
        int top = leftTop[1];
        int bottom = top + v.getHeight();
        int right = left + v.getWidth();
        return !(event.getX() > left && event.getX() < right
                && event.getY() > top && event.getY() < bottom);
    }
    return false;
}
 
Example 10
Project: SliderPreferenceEmbedded   File: PopupIndicator.java   Source Code and License Vote up 5 votes
private void updateLayoutParamsForPosiion(View anchor, WindowManager.LayoutParams p, int yOffset) {
    DisplayMetrics displayMetrics = anchor.getResources().getDisplayMetrics();
    screenSize.set(displayMetrics.widthPixels, displayMetrics.heightPixels);

    measureFloater();
    int measuredHeight = mPopupView.getMeasuredHeight();
    int paddingBottom = mPopupView.mMarker.getPaddingBottom();
    anchor.getLocationInWindow(mDrawingLocation);
    p.x = 0;
    p.y = mDrawingLocation[1] - measuredHeight + yOffset + paddingBottom;
    p.width = screenSize.x;
    p.height = measuredHeight;
}
 
Example 11
Project: MVP-Practice-Project-Template   File: MVPBaseActivity.java   Source Code and License Vote up 5 votes
/**
 * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘
 */
private boolean isShouldHideKeyboard(View v, MotionEvent event) {
    if (v != null && (v instanceof EditText)) {
        int[] l = {0, 0};
        v.getLocationInWindow(l);
        int left = l[0],
                top = l[1],
                bottom = top + v.getHeight(),
                right = left + v.getWidth();
        return !(event.getX() > left && event.getX() < right
                && event.getY() > top && event.getY() < bottom);
    }
    return false;
}
 
Example 12
Project: RNLearn_Project1   File: ReactHorizontalScrollViewTestCase.java   Source Code and License Vote up 5 votes
public void testScrollAndClick() throws Exception {
  SingleTouchGestureGenerator gestureGenerator = createGestureGenerator();

  // Slowly drag the ScrollView to prevent fling
  dragLeft(15000);

  waitForBridgeAndUIIdle();
  getInstrumentation().waitForIdleSync();

  // Find visible item to be clicked
  View visibleItem = null;
  int visibleItemNumber = 0;
  for (; visibleItemNumber < 100; visibleItemNumber++) {
    visibleItem = getViewAtPath(0, 0, visibleItemNumber);
    int pos[] = new int[2];
    visibleItem.getLocationInWindow(pos);
    if (pos[0] >= 0) {
      break;
    }
  }

  // Click first visible item
  gestureGenerator.startGesture(visibleItem).endGesture();
  waitForBridgeAndUIIdle();

  ArrayList<Double> xOffsets = mScrollListenerModule.getXOffsets();
  ArrayList<Integer> itemIds = mScrollListenerModule.getItemsPressed();
  assertFalse("Expected to receive at least one scroll event", xOffsets.isEmpty());
  assertTrue("Expected offset to be greater than 0", xOffsets.get(xOffsets.size() - 1) > 0);
  assertEquals("Expected to receive exactly one item click event", 1, itemIds.size());
  assertEquals(visibleItemNumber, (int) itemIds.get(0));
}
 
Example 13
Project: gamecollection   File: CheckeredGameboardView.java   Source Code and License Vote up 5 votes
public Point getRectangleCoordinates(Tuple<Integer, Integer> tuple) {
    Rect rect = boardSquares[tuple.first][tuple.last];
    View rootLayout = getRootView().findViewById(android.R.id.content);

    int[] viewLocation = new int[2];
    int[] rootLocation = new int[2];
    getLocationInWindow(viewLocation);
    rootLayout.getLocationInWindow(rootLocation);

    int relativeLeft = viewLocation[0] - rootLocation[0];
    int relativeTop = viewLocation[1] - rootLocation[1];
    int x = relativeLeft + rect.left;
    int y = relativeTop + rect.top;
    return new Point(x, y);
}
 
Example 14
Project: AOSP-Kayboard-7.1.2   File: MoreKeysKeyboardView.java   Source Code and License Vote up 5 votes
@Override
public void showMoreKeysPanel(final View parentView, final Controller controller,
        final int pointX, final int pointY, final KeyboardActionListener listener) {
    mController = controller;
    mListener = listener;
    final View container = getContainerView();
    // The coordinates of panel's left-top corner in parentView's coordinate system.
    // We need to consider background drawable paddings.
    final int x = pointX - getDefaultCoordX() - container.getPaddingLeft() - getPaddingLeft();
    final int y = pointY - container.getMeasuredHeight() + container.getPaddingBottom()
            + getPaddingBottom();

    parentView.getLocationInWindow(mCoordinates);
    // Ensure the horizontal position of the panel does not extend past the parentView edges.
    final int maxX = parentView.getMeasuredWidth() - container.getMeasuredWidth();
    final int panelX = Math.max(0, Math.min(maxX, x)) + CoordinateUtils.x(mCoordinates);
    final int panelY = y + CoordinateUtils.y(mCoordinates);
    container.setX(panelX);
    container.setY(panelY);

    mOriginX = x + container.getPaddingLeft();
    mOriginY = y + container.getPaddingTop();
    controller.onShowMoreKeysPanel(this);
    final MoreKeysKeyboardAccessibilityDelegate accessibilityDelegate = mAccessibilityDelegate;
    if (accessibilityDelegate != null
            && AccessibilityUtils.getInstance().isAccessibilityEnabled()) {
        accessibilityDelegate.onShowMoreKeysKeyboard();
    }
}
 
Example 15
Project: GitHub   File: RelativePopupWindow.java   Source Code and License Vote up 4 votes
/**
 * Show at relative position to anchor View with translation.
 * @param anchor Anchor View
 * @param vertPos Vertical Position Flag
 * @param horizPos Horizontal Position Flag
 * @param x Translation X
 * @param y Translation Y
 * @param fitInScreen Automatically fit in screen or not
 */
public void showOnAnchor(@NonNull View anchor, @VerticalPosition int vertPos, @HorizontalPosition int horizPos, int x, int y, boolean fitInScreen) {
    setClippingEnabled(fitInScreen);
    View contentView = getContentView();
    contentView.measure(makeDropDownMeasureSpec(getWidth()), makeDropDownMeasureSpec(getHeight()));
    final int measuredW = contentView.getMeasuredWidth();
    final int measuredH = contentView.getMeasuredHeight();
    if (!fitInScreen) {
        final int[] anchorLocation = new int[2];
        anchor.getLocationInWindow(anchorLocation);
        x += anchorLocation[0];
        y += anchorLocation[1] + anchor.getHeight();
    }
    switch (vertPos) {
        case VerticalPosition.ABOVE:
            y -= measuredH + anchor.getHeight();
            break;
        case VerticalPosition.ALIGN_BOTTOM:
            y -= measuredH;
            break;
        case VerticalPosition.CENTER:
            y -= anchor.getHeight()/2 + measuredH/2;
            break;
        case VerticalPosition.ALIGN_TOP:
            y -= anchor.getHeight();
            break;
        case VerticalPosition.BELOW:
            // Default position.
            break;
    }
    switch (horizPos) {
        case HorizontalPosition.LEFT:
            x -= measuredW;
            break;
        case HorizontalPosition.ALIGN_RIGHT:
            x -= measuredW - anchor.getWidth();
            break;
        case HorizontalPosition.CENTER:
            x += anchor.getWidth()/2 - measuredW/2;
            break;
        case HorizontalPosition.ALIGN_LEFT:
            // Default position.
            break;
        case HorizontalPosition.RIGHT:
            x += anchor.getWidth();
            break;
    }
    if (fitInScreen) {
        PopupWindowCompat.showAsDropDown(this, anchor, x, y, Gravity.NO_GRAVITY);
    } else {
        showAtLocation(anchor, Gravity.NO_GRAVITY, x, y);
    }
}
 
Example 16
Project: Multi-SwipeBackLayout   File: SwipeBackLayout.java   Source Code and License Vote up 4 votes
/***
 * clampViewPositionHorizontal()之后调用
 */
@Override
public int clampViewPositionVertical(final View child, final int top, final int dy) {
    if (mScrollChild instanceof ViewPager) {
        final ViewPager pager = (ViewPager) mScrollChild;
        mScrollChild = pager.getChildAt(pager.getCurrentItem());
        final int cacheCount = pager.getChildCount();
        int[] points = new int[2];
        for (int i = 0; i < cacheCount; i++) {
            View view = pager.getChildAt(i);
            view.getLocationInWindow(points);
            if (mTouchX >= points[0] && mTouchX < points[0] + view.getWidth()) {
                mScrollChild = view;
                break;
            }

        }
    }
    int topBounds;
    int bottomBounds;
    if (isAllowDragDirection(UP)
            && !childCanScrollDown()
            && top >= mOriginalY
            && mCurDragDirection == UP) {
        topBounds = getPaddingTop();
        bottomBounds = mVerticalDragRange;
        if (null != mOnSwipeBackCallback
                && mOnSwipeBackCallback.onIntercept(mCurDragDirection, mTouchX, mTouchY)
                && mCurDragDirection != NONE
                ) {
            mCurDragDirection = NONE;
            return mOriginalY;
        }
        return Math.min(Math.max(top, topBounds), bottomBounds);
    }
    if (isAllowDragDirection(DOWN)
            && !childCanScrollUp()
            && top <= mOriginalY
            && mCurDragDirection == DOWN) {
        topBounds = -mVerticalDragRange;
        bottomBounds = getPaddingTop();
        if (null != mOnSwipeBackCallback
                && mOnSwipeBackCallback.onIntercept(mCurDragDirection, mTouchX, mTouchY)
                && mCurDragDirection != NONE
                ) {
            mCurDragDirection = NONE;
            return mOriginalY;
        }
        return Math.min(Math.max(top, topBounds), bottomBounds);
    }
    return mOriginalY;
}
 
Example 17
Project: GracefulMovies   File: CheckPermissionsActivity.java   Source Code and License Vote up 4 votes
/**
 * 带水波动画的Activity跳转
 */
@SuppressLint("NewApi")
protected void navigateWithRippleCompat(final Activity activity, final Intent intent,
                                        final View triggerView, @ColorRes int color) {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        ActivityOptionsCompat option = ActivityOptionsCompat.makeClipRevealAnimation(triggerView, 0, 0,
                triggerView.getMeasuredWidth(), triggerView.getMeasuredHeight());
        ActivityCompat.startActivity(activity, intent, option.toBundle());

        return;
    }

    int[] location = new int[2];
    triggerView.getLocationInWindow(location);
    final int cx = location[0] + triggerView.getWidth() / 2;
    final int cy = location[1] + triggerView.getHeight() / 2;
    final ImageView view = new ImageView(activity);
    view.setImageResource(color);
    final ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
    int w = decorView.getWidth();
    int h = decorView.getHeight();
    decorView.addView(view, w, h);
    int finalRadius = (int) Math.sqrt(w * w + h * h) + 1;
    Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
    anim.setDuration(500);
    anim.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);

            activity.startActivity(intent);
            activity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            decorView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    decorView.removeView(view);
                }
            }, 500);
        }
    });
    anim.start();
}
 
Example 18
Project: CodeCompilerApp   File: ViewUtils.java   Source Code and License Vote up 4 votes
public static int getLeft(@NonNull View view) {
    final int[] coordinates = new int[3];
    view.getLocationInWindow(coordinates);
    return coordinates[0];
}
 
Example 19
Project: CodeCompilerApp   File: ViewUtils.java   Source Code and License Vote up 4 votes
public static int getTop(@NonNull View view) {
    final int[] coordinates = new int[3];
    view.getLocationInWindow(coordinates);
    return coordinates[1];
}
 
Example 20
Project: fragmentnav   File: Androids.java   Source Code and License Vote up 4 votes
public static int[] getLocationOnWindow(View view) {
    int[] location = new int[2];
    view.getLocationInWindow(location);

    return location;
}