下面列出了android.view.View#getLocationOnScreen ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static boolean onScreenArea(View view) {
if (view == null || view.getVisibility() != View.VISIBLE) {
return false;
}
int[] p = new int[2];
view.getLocationOnScreen(p);
LayoutParams lp = view.getLayoutParams();
int viewH = 0;
if (lp != null) {
viewH = lp.height;
} else {
viewH = view.getHeight();
}
return (p[1] > 0 && (p[1] - WXViewUtils.getScreenHeight(WXEnvironment.sApplication) < 0))
|| (viewH + p[1] > 0 && p[1] <= 0);
}
public static int[] calculatePopWindowPos(final View anchorView, final View contentView) {
final int windowPos[] = new int[2];
final int anchorLoc[] = new int[2];
anchorView.getLocationOnScreen(anchorLoc);
final int anchorHeight = anchorView.getHeight();
final int screenHeight = ScreenUtils.getScreenHeight(anchorView.getContext());
final int screenWidth = ScreenUtils.getScreenWidth(anchorView.getContext());
contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
final int windowHeight = contentView.getMeasuredHeight();
final int windowWidth = contentView.getMeasuredWidth();
final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight);
if (isNeedShowUp) {
windowPos[0] = screenWidth - windowWidth;
windowPos[1] = anchorLoc[1] - windowHeight;
} else {
windowPos[0] = screenWidth - windowWidth;
windowPos[1] = anchorLoc[1] + anchorHeight;
}
return windowPos;
}
/**
* 获取手指触摸的target
*
* @param rawX
* @param rawY
* @return
*/
private View getTouchTarget(float rawX, float rawY) {
int count = getChildCount();
for (int i = 0; i < count; i++) {
View target = getChildAt(i);
int[] location = new int[2];
target.getLocationOnScreen(location);
int left = location[0];
int top = location[1];
int right = left + target.getWidth();
int bottom = top + target.getHeight();
RectF rect = new RectF(left, top, right, bottom);
if (rect.contains(rawX, rawY)) {
return target;
}
}
return null;
}
/**
* 添加需要移动的 镜像View
*/
private ImageView addMirrorView(ViewGroup parent, RecyclerView recyclerView, View view) {
/**
* 我们要获取cache首先要通过setDrawingCacheEnable方法开启cache,然后再调用getDrawingCache方法就可以获得view的cache图片了。
buildDrawingCache方法可以不用调用,因为调用getDrawingCache方法时,若果cache没有建立,系统会自动调用buildDrawingCache方法生成cache。
若想更新cache, 必须要调用destoryDrawingCache方法把旧的cache销毁,才能建立新的。
当调用setDrawingCacheEnabled方法设置为false, 系统也会自动把原来的cache销毁。
*/
view.destroyDrawingCache();
view.setDrawingCacheEnabled(true);
final ImageView mirrorView = new ImageView(recyclerView.getContext());
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
mirrorView.setImageBitmap(bitmap);
view.setDrawingCacheEnabled(false);
int[] locations = new int[2];
view.getLocationOnScreen(locations);
int[] parenLocations = new int[2];
recyclerView.getLocationOnScreen(parenLocations);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(bitmap.getWidth(), bitmap.getHeight());
params.setMargins(locations[0], locations[1] - parenLocations[1], 0, 0);
parent.addView(mirrorView, params);
return mirrorView;
}
/**
* Tests a AppBarLayout + scrolling content with fitSystemWindows = undefined, with a
* fitSystemWindows = true parent
*/
@Test
public void testScrollingContentPositionWithFitSystemWindowsParent() throws Throwable {
configureContent(
R.layout.design_appbar_toolbar_scroll_fitsystemwindows_parent,
R.string.design_appbar_toolbar_scroll_tabs_pin);
final int[] appbarOnScreenXY = new int[2];
mAppBar.getLocationOnScreen(appbarOnScreenXY);
final View scrollingContent = mCoordinatorLayout.findViewById(R.id.scrolling_content);
final int[] scrollingContentOnScreenXY = new int[2];
scrollingContent.getLocationOnScreen(scrollingContentOnScreenXY);
// Assert that they have the same left
assertEquals(appbarOnScreenXY[0], scrollingContentOnScreenXY[0]);
// ...and the same width
assertEquals(mAppBar.getWidth(), scrollingContent.getWidth());
// ...and are vertically stacked
assertEquals(mAppBar.getBottom(), scrollingContent.getTop());
}
private boolean isTouchPointInView(View view, float x, float y) {
if (view == null) {
return false;
}
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();
//view.isClickable() &&
if (y >= top && y <= bottom && x >= left
&& x <= right) {
return true;
}
return false;
}
public static Rect locateView(View v) {
int[] location = new int[2];
if (v == null) {
return null;
}
try {
v.getLocationOnScreen(location);
} catch (NullPointerException npe) {
// Happens when the view doesn't exist on screen anymore.
return null;
}
Rect locationRect = new Rect();
locationRect.left = location[0];
locationRect.top = location[1];
locationRect.right = locationRect.left + v.getWidth();
locationRect.bottom = locationRect.top + v.getHeight();
return locationRect;
}
public static Rect locateView(View v) {
int[] location = new int[2];
if (v == null) {
return null;
}
try {
v.getLocationOnScreen(location);
} catch (NullPointerException npe) {
// Happens when the view doesn't exist on screen anymore.
return null;
}
Rect locationRect = new Rect();
locationRect.left = location[0];
locationRect.top = location[1];
locationRect.right = locationRect.left + v.getWidth();
locationRect.bottom = locationRect.top + v.getHeight();
return locationRect;
}
private boolean isTouchPointInView(View view, float x) {
int diff = 40;
if (view == null) {
return false;
}
int[] location = new int[2];
view.getLocationOnScreen(location);
int left = location[0];
int right = left + view.getMeasuredWidth();
if ((x >= left && x <= right) ||(x >= left - diff && x <= right+diff)) {
return true;
}
return false;
}
public void removeVideoList() {
int size = mList.size() - 1;
for (int i = size; i > 0; i--) {
mList.remove(i);
}
mAdapter.notifyItemRangeRemoved(1, size);
final View view = mRecycler.getChildAt(0);
final int[] location = new int[2];
view.getLocationOnScreen(location);
final ImageView image = view.findViewById(R.id.adapter_video_list_image);
final FrameLayout container = view.findViewById(R.id.adapter_video_list_container);
final TextView title = view.findViewById(R.id.adapter_video_list_title);
final LinearLayout bottom = view.findViewById(R.id.bottom_layout);
title.postDelayed(new Runnable() {
@Override
public void run() {
title.setVisibility(View.GONE);
bottom.setVisibility(View.GONE);
image.setVisibility(View.GONE);
container.animate().scaleX((float) mAttr.getWidth() / container.getMeasuredWidth())
.scaleY((float) mAttr.getHeight() / container.getMeasuredHeight())
.setDuration(DURATION);
view.animate().translationY(mAttr.getY() - location[1]).setDuration(DURATION);
ObjectAnimator animator = ObjectAnimator.ofInt(mRoot, "backgroundColor", 0xff000000, 0x00000000);
animator.setEvaluator(new ArgbEvaluator());
animator.setDuration(DURATION);
animator.start();
}
}, 250);
}
private boolean isViewContains(View view, int rx, int ry) {
view.getLocationOnScreen(_tempArrayOfInt2);
int x = _tempArrayOfInt2[0];
int y = _tempArrayOfInt2[1];
int w = view.getWidth();
int h = view.getHeight();
if (rx < x || rx > x + w || ry < y || ry > y + h) {
return false;
}
return true;
}
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 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);
}
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;
}
/**
* Checks if an input to coordinates lay within a View
*
* @param view View to check with
* @param x x value of the input
* @param y y value of the input
* @return True if input coordinates lay within the given view
*/
private boolean isViewHit(View view, int x, int y) {
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();
}
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 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());
}
public ViewInfo(View v, int position) {
int[] screenLocation = new int[2];
v.getLocationOnScreen(screenLocation);
left = screenLocation[0];
top = screenLocation[1];
width = v.getWidth();
height = v.getHeight();
orientation = v.getResources().getConfiguration().orientation;
this.position = position;
}
@NonNull static Point locationOnScreen(@NonNull final View view) {
final int[] location = new int[2];
view.getLocationOnScreen(location);
return new Point(location[0], location[1]);
}
/**
* view Location in the screen
* @param view
* @return
*/
public static int[] locationOnScreen(View view) {
int[] location = new int[2];
view.getLocationOnScreen(location);
return location;
}