public int compare(View lhs, View rhs) { lhs.getLocationOnScreen(a); rhs.getLocationOnScreen(b); if (a[axis1] != b[axis1]) { return a[axis1] < b[axis1] ? -1 : 1; } if (a[axis2] < b[axis2]) { return -1; } return a[axis2] == b[axis2] ? 0 : 1; } }
private static float[] getViewLocationOnScreen(View view) { int[] location = new int[2]; view.getLocationOnScreen(location); float[] result = new float[2]; result[0] = location[0]; result[1] = location[1]; return result; }
View globalView = ...; // the main view of my activity/application DisplayMetrics dm = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(dm); int topOffset = dm.heightPixels - globalView.getMeasuredHeight(); View tempView = ...; // the view you'd like to locate int[] loc = new int[2]; tempView.getLocationOnScreen(loc); final int y = loc[1] - topOffset;
@Override public void onClick(View v) { int[] location = {0, 0}; v.getLocationOnScreen(location); switchItem(list.get(finalI), location[1] + v.getHeight() / 2); } });
@NonNull public int[] getLocationOnScreenOfOverlayView(@NonNull ViewGroup sceneRoot, @NonNull View overlayView) { int[] location = new int[2]; overlayView.getLocationOnScreen(location); return location; }
private void captureValues(TransitionValues transitionValues) { View view = transitionValues.view; view.getLocationOnScreen(mTempLoc); int left = mTempLoc[0]; int top = mTempLoc[1]; int right = left + view.getWidth(); int bottom = top + view.getHeight(); transitionValues.values.put(PROPNAME_SCREEN_BOUNDS, new Rect(left, top, right, bottom)); }
private boolean isEventWithinView(MotionEvent e, View child) { Rect viewRect = new Rect(); int[] childPosition = new int[2]; child.getLocationOnScreen(childPosition); int left = childPosition[0]; int right = left + child.getWidth(); int top = childPosition[1]; int bottom = top + child.getHeight(); viewRect.set(left, top, right, bottom); return viewRect.contains((int) e.getRawX(), (int) e.getRawY()); } };
private boolean isViewUnder(View view, int x, int y) { if (view == null) return false; int[] viewLocation = new int[2]; view.getLocationOnScreen(viewLocation); int[] parentLocation = new int[2]; this.getLocationOnScreen(parentLocation); int screenX = parentLocation[0] + x; int screenY = parentLocation[1] + y; return screenX >= viewLocation[0] && screenX < viewLocation[0] + view.getWidth() && screenY >= viewLocation[1] && screenY < viewLocation[1] + view.getHeight(); }
public boolean dispatchTouchEvent(MotionEvent ev) { View contentsView = findViewById(android.R.id.content); int test1[] = new int[2]; contentsView.getLocationInWindow(test1); int test2[] = new int[2]; contentsView.getLocationOnScreen(test2); System.out.println(test1[1] + " " + test2[1]); return super.dispatchTouchEvent(ev); }
/** * Emulates View.toLocalMotionEvent(). This implementation does not handle transformations * (scaleX, scaleY, etc). */ private boolean toLocalMotionEvent(View view, MotionEvent event) { final int[] loc = mTmpLocation; view.getLocationOnScreen(loc); event.offsetLocation(-loc[0], -loc[1]); return true; }
public static void click(View view) { final int[] locationOnScreen = new int[2]; view.getLocationOnScreen(locationOnScreen); click(new Point( locationOnScreen[0] + view.getWidth() / 2, locationOnScreen[1] + view.getHeight() / 2)); }
private boolean isDragViewUnder(int x, int y) { if (mDragView == null) return false; int[] viewLocation = new int[2]; mDragView.getLocationOnScreen(viewLocation); int[] parentLocation = new int[2]; this.getLocationOnScreen(parentLocation); int screenX = parentLocation[0] + x; int screenY = parentLocation[1] + y; return screenX >= viewLocation[0] && screenX < viewLocation[0] + mDragView.getWidth() && screenY >= viewLocation[1] && screenY < viewLocation[1] + mDragView.getHeight(); }
/** * Emulates View.toGlobalMotionEvent(). This implementation does not handle transformations * (scaleX, scaleY, etc). */ private boolean toGlobalMotionEvent(View view, MotionEvent event) { final int[] loc = mTmpLocation; view.getLocationOnScreen(loc); event.offsetLocation(loc[0], loc[1]); return true; }
@Override public void onCommentsClick(View v, int position) { final Intent intent = new Intent(this, CommentsActivity.class); int[] startingLocation = new int[2]; v.getLocationOnScreen(startingLocation); intent.putExtra(CommentsActivity.ARG_DRAWING_START_LOCATION, startingLocation[1]); startActivity(intent); overridePendingTransition(0, 0); }
Rect outRect = new Rect(); int[] location = new int[2]; private boolean isViewInBounds(View view, int x, int y){ view.getDrawingRect(outRect); view.getLocationOnScreen(location); outRect.offset(location[0], location[1]); return outRect.contains(x, y); }
@Override public void onProfileClick(View v) { int[] startingLocation = new int[2]; v.getLocationOnScreen(startingLocation); startingLocation[0] += v.getWidth() / 2; UserProfileActivity.startUserProfileFromLocation(startingLocation, this); overridePendingTransition(0, 0); }
@Override public void run() { int[] startingLocation = new int[2]; v.getLocationOnScreen(startingLocation); startingLocation[0] += v.getWidth() / 2; UserProfileActivity.startUserProfileFromLocation(startingLocation, BaseDrawerActivity.this); overridePendingTransition(0, 0); } }, 200);
public static void swipeHorizontally(InstrumentationTestCase test, View v, Direction direction) { int[] xy = new int[2]; v.getLocationOnScreen(xy); final int viewWidth = v.getWidth(); final int viewHeight = v.getHeight(); float distanceFromEdge = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, v.getResources().getDisplayMetrics()); float xStart = xy[0] + ((direction == Direction.LEFT) ? (viewWidth - distanceFromEdge) : distanceFromEdge); float xEnd = xy[0] + ((direction == Direction.LEFT) ? distanceFromEdge : (viewWidth - distanceFromEdge)); float y = xy[1] + (viewHeight / 2.0f); TouchUtils.drag(test, xStart, xEnd, y, y, DRAG_STEP_COUNT); }
public static void swipeVertically(InstrumentationTestCase test, View v, Direction direction) { int[] xy = new int[2]; v.getLocationOnScreen(xy); final int viewWidth = v.getWidth(); final int viewHeight = v.getHeight(); float distanceFromEdge = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, v.getResources().getDisplayMetrics()); float x = xy[0] + (viewWidth / 2.0f); float yStart = xy[1] + ((direction == Direction.UP) ? (viewHeight - distanceFromEdge) : distanceFromEdge); float yEnd = xy[1] + ((direction == Direction.UP) ? distanceFromEdge : (viewHeight - distanceFromEdge)); TouchUtils.drag(test, x, x, yStart, yEnd, DRAG_STEP_COUNT); }
private void setupContextMenuInitialPosition(View openingView) { final int[] openingViewLocation = new int[2]; openingView.getLocationOnScreen(openingViewLocation); int additionalBottomMargin = Utils.dpToPx(16); contextMenuView.setTranslationX(openingViewLocation[0] - contextMenuView.getWidth() / 3); contextMenuView.setTranslationY(openingViewLocation[1] - contextMenuView.getHeight() - additionalBottomMargin); }