Java Code Examples for android.view.ViewGroup#getLocationOnScreen()

The following examples show how to use android.view.ViewGroup#getLocationOnScreen() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DraggableView.java    From AndroidBottomSheet with Apache License 2.0 6 votes vote down vote up
/**
 * Returns, whether a touch event at a specific position targets a view, which can be scrolled
 * up.
 *
 * @param x
 *         The horizontal position of the touch event in pixels as a {@link Float} value
 * @param y
 *         The vertical position of the touch event in pixels as a {@link Float} value
 * @param viewGroup
 *         The view group, which should be used to search for scrollable child views, as an
 *         instance of the class {@link ViewGroup}. The view group may not be null
 * @return True, if the touch event targets a view, which can be scrolled up, false otherwise
 */
private boolean isScrollUpEvent(final float x, final float y,
                                @NonNull final ViewGroup viewGroup) {
    int location[] = new int[2];
    viewGroup.getLocationOnScreen(location);

    if (x >= location[0] && x <= location[0] + viewGroup.getWidth() && y >= location[1] &&
            y <= location[1] + viewGroup.getHeight()) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View view = viewGroup.getChildAt(i);

            if (view.canScrollVertically(-1)) {
                return true;
            } else if (view instanceof ViewGroup) {
                return isScrollUpEvent(x, y, (ViewGroup) view);
            }
        }
    }

    return false;
}
 
Example 2
Source File: ChannelAdapter.java    From NetEasyNews with GNU General Public License v3.0 6 votes vote down vote up
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];
    parent.getLocationOnScreen(parenLocations);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(bitmap.getWidth(), bitmap.getHeight());
    params.setMargins(locations[0], locations[1] - parenLocations[1], 0, 0);
    parent.addView(mirrorView, params);
    return mirrorView;
}
 
Example 3
Source File: DebugOverlay.java    From Carbon with Apache License 2.0 6 votes vote down vote up
void drawViewGroup(Canvas canvas, ViewGroup viewGroup) {
    drawView(canvas, viewGroup);
    canvas.save();

    int[] l = new int[2];
    viewGroup.getLocationOnScreen(l);
    rect.set(0, 0, viewGroup.getWidth(), viewGroup.getHeight());
    rect.offset(l[0] - location[0], l[1] - location[1]);

    canvas.clipRect(rect);
    for (int i = 0; i < viewGroup.getChildCount(); i++) {
        View v = viewGroup.getChildAt(i);
        if (v instanceof ViewGroup) {
            drawViewGroup(canvas, (ViewGroup) v);
        } else {
            drawView(canvas, v);
        }
    }
    canvas.restore();
}
 
Example 4
Source File: SidePropagation.java    From scene with Apache License 2.0 5 votes vote down vote up
@Override
public long getStartDelay(ViewGroup sceneRoot, SceneVisibilityTransition transition, TransitionPropagationResult result, boolean appear) {
    int directionMultiplier = 1;
    Rect epicenter = transition.getEpicenter();
    if (!appear) {
        directionMultiplier = -1;
    }

    int viewCenterX = getViewX(result);
    int viewCenterY = getViewY(result);

    int[] loc = new int[2];
    sceneRoot.getLocationOnScreen(loc);
    int left = loc[0] + Math.round(sceneRoot.getTranslationX());
    int top = loc[1] + Math.round(sceneRoot.getTranslationY());
    int right = left + sceneRoot.getWidth();
    int bottom = top + sceneRoot.getHeight();

    int epicenterX;
    int epicenterY;
    if (epicenter != null) {
        epicenterX = epicenter.centerX();
        epicenterY = epicenter.centerY();
    } else {
        epicenterX = (left + right) / 2;
        epicenterY = (top + bottom) / 2;
    }

    float distance = distance(sceneRoot, viewCenterX, viewCenterY, epicenterX, epicenterY,
            left, top, right, bottom);
    float maxDistance = getMaxDistance(sceneRoot);
    float distanceFraction = distance / maxDistance;

    long duration = transition.getDuration();
    if (duration < 0) {
        duration = 300;
    }

    return Math.round(duration * directionMultiplier / mPropagationSpeed * distanceFraction);
}
 
Example 5
Source File: CircularPropagation.java    From scene with Apache License 2.0 5 votes vote down vote up
@Override
public long getStartDelay(ViewGroup sceneRoot, SceneVisibilityTransition transition, TransitionPropagationResult result, boolean appear) {
    int directionMultiplier = 1;
    TransitionValues positionValues;
    if (!appear) {
        directionMultiplier = -1;
    }

    int viewCenterX = getViewX(result);
    int viewCenterY = getViewY(result);

    Rect epicenter = transition.getEpicenter();
    int epicenterX;
    int epicenterY;
    if (epicenter != null) {
        epicenterX = epicenter.centerX();
        epicenterY = epicenter.centerY();
    } else {
        int[] loc = new int[2];
        sceneRoot.getLocationOnScreen(loc);
        epicenterX = Math.round(loc[0] + (sceneRoot.getWidth() / 2)
                + sceneRoot.getTranslationX());
        epicenterY = Math.round(loc[1] + (sceneRoot.getHeight() / 2)
                + sceneRoot.getTranslationY());
    }
    float distance = distance(viewCenterX, viewCenterY, epicenterX, epicenterY);
    float maxDistance = distance(0, 0, sceneRoot.getWidth(), sceneRoot.getHeight());
    float distanceFraction = distance / maxDistance;

    long duration = transition.getDuration();
    if (duration < 0) {
        duration = 300;
    }

    return Math.round(duration * directionMultiplier / mPropagationSpeed * distanceFraction);
}
 
Example 6
Source File: ActivityTransitionCoordinator.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
protected ArrayList<View> createSnapshots(Bundle state, Collection<String> names) {
    int numSharedElements = names.size();
    ArrayList<View> snapshots = new ArrayList<View>(numSharedElements);
    if (numSharedElements == 0) {
        return snapshots;
    }
    Context context = getWindow().getContext();
    int[] decorLoc = new int[2];
    ViewGroup decorView = getDecor();
    if (decorView != null) {
        decorView.getLocationOnScreen(decorLoc);
    }
    Matrix tempMatrix = new Matrix();
    for (String name: names) {
        Bundle sharedElementBundle = state.getBundle(name);
        View snapshot = null;
        if (sharedElementBundle != null) {
            Parcelable parcelable = sharedElementBundle.getParcelable(KEY_SNAPSHOT);
            if (parcelable != null && mListener != null) {
                snapshot = mListener.onCreateSnapshotView(context, parcelable);
            }
            if (snapshot != null) {
                setSharedElementState(snapshot, name, state, tempMatrix, null, decorLoc);
            }
        }
        // Even null snapshots are added so they remain in the same order as shared elements.
        snapshots.add(snapshot);
    }
    return snapshots;
}
 
Example 7
Source File: DemoUtils.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("RestrictTo")
private static boolean shouldApplyBottomInset(ViewGroup scrollView, int systemWindowInsetBottom) {
  View scrollableContent = scrollView.getChildAt(0);
  int scrollableContentHeight = scrollableContent.getHeight();
  int scrollViewHeight = scrollView.getHeight();
  int[] scrollViewLocation = new int[2];
  scrollView.getLocationOnScreen(scrollViewLocation);
  Context context = scrollView.getContext();

  Activity activity = ContextUtils.getActivity(context);
  return scrollViewHeight + scrollViewLocation[1] >= getContentViewHeight(activity)
      && scrollableContentHeight + systemWindowInsetBottom >= scrollViewHeight;
}
 
Example 8
Source File: ViewOverlayApi14.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
public void add(View child) {
  assertNotDisposed();
  if (child.getParent() instanceof ViewGroup) {
    ViewGroup parent = (ViewGroup) child.getParent();
    if (parent != hostView
        && parent.getParent() != null
        && ViewCompat.isAttachedToWindow(parent)) {
      // Moving to different container; figure out how to position child such that
      // it is in the same location on the screen
      int[] parentLocation = new int[2];
      int[] hostViewLocation = new int[2];
      parent.getLocationOnScreen(parentLocation);
      hostView.getLocationOnScreen(hostViewLocation);
      ViewCompat.offsetLeftAndRight(child, parentLocation[0] - hostViewLocation[0]);
      ViewCompat.offsetTopAndBottom(child, parentLocation[1] - hostViewLocation[1]);
    }
    parent.removeView(child);
    //                if (parent.getLayoutTransition() != null) {
    //                    // LayoutTransition will cause the child to delay removal - cancel it
    //                    parent.getLayoutTransition().cancel(LayoutTransition.DISAPPEARING);
    //                }
    // fail-safe if view is still attached for any reason
    if (child.getParent() != null) {
      parent.removeView(child);
    }
  }
  super.addView(child);
}
 
Example 9
Source File: ViewGroupOverlayUtils.java    From Transitions-Everywhere with Apache License 2.0 5 votes vote down vote up
@Override
public void moveViewInOverlay(@NonNull ViewGroup sceneRoot, @NonNull View overlayView, int screenX, int screenY) {
    if (screenX != 0 || screenY != 0) {
        int[] loc = new int[2];
        sceneRoot.getLocationOnScreen(loc);
        overlayView.offsetLeftAndRight((screenX - loc[0]) - overlayView.getLeft());
        overlayView.offsetTopAndBottom((screenY - loc[1]) - overlayView.getTop());
    }
}
 
Example 10
Source File: ViewGroupOverlayUtils.java    From Transitions-Everywhere with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public int[] getLocationOnScreenOfOverlayView(@NonNull ViewGroup sceneRoot, @NonNull View overlayView) {
    int[] location = new int[2];
    sceneRoot.getLocationOnScreen(location);
    location[0] += overlayView.getLeft();
    location[1] += overlayView.getTop();
    return location;
}
 
Example 11
Source File: SidePropagation.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public long getStartDelay(ViewGroup sceneRoot, Transition transition,
        TransitionValues startValues, TransitionValues endValues) {
    if (startValues == null && endValues == null) {
        return 0;
    }
    int directionMultiplier = 1;
    Rect epicenter = transition.getEpicenter();
    TransitionValues positionValues;
    if (endValues == null || getViewVisibility(startValues) == View.VISIBLE) {
        positionValues = startValues;
        directionMultiplier = -1;
    } else {
        positionValues = endValues;
    }

    int viewCenterX = getViewX(positionValues);
    int viewCenterY = getViewY(positionValues);

    int[] loc = new int[2];
    sceneRoot.getLocationOnScreen(loc);
    int left = loc[0] + Math.round(sceneRoot.getTranslationX());
    int top = loc[1] + Math.round(sceneRoot.getTranslationY());
    int right = left + sceneRoot.getWidth();
    int bottom = top + sceneRoot.getHeight();

    int epicenterX;
    int epicenterY;
    if (epicenter != null) {
        epicenterX = epicenter.centerX();
        epicenterY = epicenter.centerY();
    } else {
        epicenterX = (left + right) / 2;
        epicenterY = (top + bottom) / 2;
    }

    float distance = distance(sceneRoot, viewCenterX, viewCenterY, epicenterX, epicenterY,
            left, top, right, bottom);
    float maxDistance = getMaxDistance(sceneRoot);
    float distanceFraction = distance/maxDistance;

    long duration = transition.getDuration();
    if (duration < 0) {
        duration = 300;
    }

    return Math.round(duration * directionMultiplier / mPropagationSpeed * distanceFraction);
}
 
Example 12
Source File: CircularPropagation.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public long getStartDelay(ViewGroup sceneRoot, Transition transition,
        TransitionValues startValues, TransitionValues endValues) {
    if (startValues == null && endValues == null) {
        return 0;
    }
    int directionMultiplier = 1;
    TransitionValues positionValues;
    if (endValues == null || getViewVisibility(startValues) == View.VISIBLE) {
        positionValues = startValues;
        directionMultiplier = -1;
    } else {
        positionValues = endValues;
    }

    int viewCenterX = getViewX(positionValues);
    int viewCenterY = getViewY(positionValues);

    Rect epicenter = transition.getEpicenter();
    int epicenterX;
    int epicenterY;
    if (epicenter != null) {
        epicenterX = epicenter.centerX();
        epicenterY = epicenter.centerY();
    } else {
        int[] loc = new int[2];
        sceneRoot.getLocationOnScreen(loc);
        epicenterX = Math.round(loc[0] + (sceneRoot.getWidth() / 2)
                + sceneRoot.getTranslationX());
        epicenterY = Math.round(loc[1] + (sceneRoot.getHeight() / 2)
                + sceneRoot.getTranslationY());
    }
    double distance = distance(viewCenterX, viewCenterY, epicenterX, epicenterY);
    double maxDistance = distance(0, 0, sceneRoot.getWidth(), sceneRoot.getHeight());
    double distanceFraction = distance/maxDistance;

    long duration = transition.getDuration();
    if (duration < 0) {
        duration = 300;
    }

    return Math.round(duration * directionMultiplier / mPropagationSpeed * distanceFraction);
}
 
Example 13
Source File: TouchInterceptor.java    From GravityBox with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (mDragListener != null || mDropListener != null) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                int x = (int) ev.getX();
                int y = (int) ev.getY();
                int itemnum = pointToPosition(x, y);
                if (itemnum == AdapterView.INVALID_POSITION) {
                    break;
                }
                ViewGroup item = (ViewGroup) getChildAt(itemnum - getFirstVisiblePosition());
                mDragPoint = y - item.getTop();
                mCoordOffset = ((int) ev.getRawY()) - y;
                View dragger = item.findViewById(R.id.grabber);
                Rect r = mTempRect;
                dragger.getDrawingRect(r);
                if (shouldStartDragging(x, r.width())) {
                    // Fix x position while dragging
                    int[] itemPos = new int[2];
                    item.getLocationOnScreen(itemPos);

                    item.setDrawingCacheEnabled(true);
                    // Create a copy of the drawing cache so that it does
                    // not get recycled
                    // by the framework when the list tries to clean up
                    // memory
                    Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache());
                    startDragging(bitmap, itemPos[0], y);
                    mDragPos = itemnum;
                    mFirstDragPos = mDragPos;
                    mHeight = getHeight();
                    int touchSlop = mTouchSlop;
                    mUpperBound = Math.min(y - touchSlop, mHeight / 3);
                    mLowerBound = Math.max(y + touchSlop, mHeight * 2 / 3);
                    return false;
                }
                stopDragging();
                break;
        }
    }
    return super.onInterceptTouchEvent(ev);
}
 
Example 14
Source File: SidePropagation.java    From Transitions-Everywhere with Apache License 2.0 4 votes vote down vote up
@Override
public long getStartDelay(@NonNull ViewGroup sceneRoot, @NonNull Transition transition,
                          @Nullable TransitionValues startValues, @Nullable TransitionValues endValues) {
    if (startValues == null && endValues == null) {
        return 0;
    }
    int directionMultiplier = 1;
    Rect epicenter = transition.getEpicenter();
    TransitionValues positionValues;
    if (endValues == null || getViewVisibility(startValues) == View.VISIBLE) {
        positionValues = startValues;
        directionMultiplier = -1;
    } else {
        positionValues = endValues;
    }

    int viewCenterX = getViewX(positionValues);
    int viewCenterY = getViewY(positionValues);

    int[] loc = new int[2];
    sceneRoot.getLocationOnScreen(loc);
    int left = loc[0] + Math.round(sceneRoot.getTranslationX());
    int top = loc[1] + Math.round(sceneRoot.getTranslationY());
    int right = left + sceneRoot.getWidth();
    int bottom = top + sceneRoot.getHeight();

    int epicenterX;
    int epicenterY;
    if (epicenter != null) {
        epicenterX = epicenter.centerX();
        epicenterY = epicenter.centerY();
    } else {
        epicenterX = (left + right) / 2;
        epicenterY = (top + bottom) / 2;
    }

    float distance = distance(sceneRoot, viewCenterX, viewCenterY, epicenterX, epicenterY,
            left, top, right, bottom);
    float maxDistance = getMaxDistance(sceneRoot);
    float distanceFraction = distance/maxDistance;

    long duration = transition.getDuration();
    if (duration < 0) {
        duration = 300;
    }

    return Math.round(duration * directionMultiplier / mPropagationSpeed * distanceFraction);
}
 
Example 15
Source File: CircularPropagation.java    From Transitions-Everywhere with Apache License 2.0 4 votes vote down vote up
@Override
public long getStartDelay(@NonNull ViewGroup sceneRoot, @NonNull Transition transition,
                          @Nullable TransitionValues startValues, @Nullable TransitionValues endValues) {
    if (startValues == null && endValues == null) {
        return 0;
    }
    int directionMultiplier = 1;
    TransitionValues positionValues;
    if (endValues == null || getViewVisibility(startValues) == View.VISIBLE) {
        positionValues = startValues;
        directionMultiplier = -1;
    } else {
        positionValues = endValues;
    }

    int viewCenterX = getViewX(positionValues);
    int viewCenterY = getViewY(positionValues);

    Rect epicenter = transition.getEpicenter();
    int epicenterX;
    int epicenterY;
    if (epicenter != null) {
        epicenterX = epicenter.centerX();
        epicenterY = epicenter.centerY();
    } else {
        int[] loc = new int[2];
        sceneRoot.getLocationOnScreen(loc);
        epicenterX = Math.round(loc[0] + (sceneRoot.getWidth() / 2)
                + sceneRoot.getTranslationX());
        epicenterY = Math.round(loc[1] + (sceneRoot.getHeight() / 2)
                + sceneRoot.getTranslationY());
    }
    double distance = distance(viewCenterX, viewCenterY, epicenterX, epicenterY);
    double maxDistance = distance(0, 0, sceneRoot.getWidth(), sceneRoot.getHeight());
    double distanceFraction = distance/maxDistance;

    long duration = transition.getDuration();
    if (duration < 0) {
        duration = 300;
    }

    return Math.round(duration * directionMultiplier / mPropagationSpeed * distanceFraction);
}