下面列出了android.view.View#getHitRect ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean canChildrenScroolUp(View view, MotionEvent event) {
if (view instanceof ViewGroup) {
final ViewGroup viewgroup = (ViewGroup) view;
int count = viewgroup.getChildCount();
for (int i = 0; i < count; ++i) {
View child = viewgroup.getChildAt(i);
Rect bounds = new Rect();
child.getHitRect(bounds);
if (bounds.contains((int) event.getX(), (int) event.getY())) {
return canViewScrollUp(child, event);
}
}
}
return false;
}
public static ListPosition obtain(ListView listView) {
int position = listView.getFirstVisiblePosition();
int y = 0;
Rect rect = new Rect();
int paddingTop = listView.getPaddingTop(), paddingLeft = listView.getPaddingLeft();
for (int i = 0, count = listView.getChildCount(); i < count; i++) {
View view = listView.getChildAt(i);
view.getHitRect(rect);
if (rect.contains(paddingLeft, paddingTop)) {
position += i;
y = rect.top - paddingTop;
break;
}
}
return new ListPosition(position, y);
}
private void drawShadow(Canvas canvas, View child) {
final Rect childRect = mTmpRect;
child.getHitRect(childRect);
if ((mEdgeFlag & EDGE_LEFT) != 0) {
mShadowLeft.setBounds(childRect.left - mShadowLeft.getIntrinsicWidth(), childRect.top,
childRect.left, childRect.bottom);
mShadowLeft.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
mShadowLeft.draw(canvas);
}
if ((mEdgeFlag & EDGE_RIGHT) != 0) {
mShadowRight.setBounds(childRect.right, childRect.top,
childRect.right + mShadowRight.getIntrinsicWidth(), childRect.bottom);
mShadowRight.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
mShadowRight.draw(canvas);
}
if ((mEdgeFlag & EDGE_BOTTOM) != 0) {
mShadowBottom.setBounds(childRect.left, childRect.bottom, childRect.right,
childRect.bottom + mShadowBottom.getIntrinsicHeight());
mShadowBottom.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
mShadowBottom.draw(canvas);
}
}
private int myPointToPosition(int x, int y) {
if (y < 0) {
// when dragging off the top of the screen, calculate position
// by going back from a visible item
int pos = myPointToPosition(x, y + mItemHeightNormal);
if (pos > 0) {
return pos - 1;
}
}
Rect frame = mTempRect;
final int count = getChildCount();
for (int i = count - 1; i >= 0; i--) {
final View child = getChildAt(i);
child.getHitRect(frame);
if (frame.contains(x, y)) {
return getFirstVisiblePosition() + i;
}
}
return INVALID_POSITION;
}
private void requestChildFocusInner(View child, @NonNull View focused) {
// Try to find first non-null selector to take it as an anchor.
Drawable refSelector = null;
for (Drawable selector : mSelectorDrawables) {
if (selector != null) {
refSelector = selector;
break;
}
}
int scrollState = getScrollState();
if (refSelector != null && scrollState == SCROLL_STATE_IDLE) {
mSelectorSourceRect.set(refSelector.getBounds());
// Focused cannot be null
focused.getHitRect(mSelectorDestRect);
mReusableSelectListener.mToSelect = child;
mReusableSelectListener.mToDeselect = mFocusArchivist.getLastFocus(this);
animateSelectorChange(mReusableSelectListener);
mFocusArchivist.archiveFocus(this, child);
}
}
private boolean canChildrenScroolHorizontally(View view, MotionEvent event, int direction) {
if (view instanceof ViewGroup) {
final ViewGroup viewgroup = (ViewGroup) view;
int count = viewgroup.getChildCount();
for (int i = 0; i < count; ++i) {
View child = viewgroup.getChildAt(i);
Rect bounds = new Rect();
child.getHitRect(bounds);
if (bounds.contains((int) event.getX(), (int) event.getY())) {
if (DEBUG)
Log.d(TAG, "in child " + child.getClass().getName());
return canViewScrollHorizontally(child, event, direction);
}
}
}
return false;
}
private boolean findClickableViewInChild(View view, int x, int y) {
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
final Rect rect = new Rect();
child.getHitRect(rect);
final boolean contains = rect.contains(x, y);
if (contains) {
return findClickableViewInChild(child, x - rect.left, y - rect.top);
}
}
} else if (view != childView) {
return (view.isEnabled() && (view.isClickable() || view.isLongClickable() || view.isFocusableInTouchMode()));
}
return view.isFocusableInTouchMode();
}
private void drawShadow(Canvas canvas, View child) {
final Rect childRect = mTmpRect;
child.getHitRect(childRect);
if ((mEdgeFlag & EDGE_LEFT) != 0) {
mShadowLeft.setBounds(
childRect.left - mShadowLeft.getIntrinsicWidth(),
childRect.top, childRect.left, childRect.bottom);
mShadowLeft.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
mShadowLeft.draw(canvas);
}
if ((mEdgeFlag & EDGE_RIGHT) != 0) {
mShadowRight.setBounds(childRect.right, childRect.top,
childRect.right + mShadowRight.getIntrinsicWidth(),
childRect.bottom);
mShadowRight.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
mShadowRight.draw(canvas);
}
if ((mEdgeFlag & EDGE_BOTTOM) != 0) {
mShadowBottom.setBounds(childRect.left, childRect.bottom,
childRect.right,
childRect.bottom + mShadowBottom.getIntrinsicHeight());
mShadowBottom.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
mShadowBottom.draw(canvas);
}
}
private boolean checkFileExistence(Message message, View view, ViewHolder viewHolder) {
final Rect scrollBounds = new Rect();
view.getHitRect(scrollBounds);
if (message.isFileDeleted() && viewHolder.messageBody.getLocalVisibleRect(scrollBounds)) {
return activity.xmppConnectionService.getFileBackend().getFile(message).exists();
} else {
return false;
}
}
private void drawShadow(Canvas canvas, View child) {
final Rect childRect = mTmpRect;
child.getHitRect(childRect);
if ((mEdgeFlag & EDGE_LEFT) != 0) {
mShadowLeft.setBounds(childRect.left - mShadowLeft.getIntrinsicWidth(), childRect.top,
childRect.left, childRect.bottom);
mShadowLeft.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
mShadowLeft.draw(canvas);
}
if ((mEdgeFlag & EDGE_RIGHT) != 0) {
mShadowRight.setBounds(childRect.right, childRect.top,
childRect.right + mShadowRight.getIntrinsicWidth(), childRect.bottom);
mShadowRight.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
mShadowRight.draw(canvas);
}
if ((mEdgeFlag & EDGE_BOTTOM) != 0) {
mShadowBottom.setBounds(childRect.left, childRect.bottom, childRect.right,
childRect.bottom + mShadowBottom.getIntrinsicHeight());
mShadowBottom.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
mShadowBottom.draw(canvas);
}
if ((mEdgeFlag & EDGE_TOP) != 0) {
mShadowTop.setBounds(childRect.left, childRect.top - mShadowTop.getIntrinsicHeight(),
childRect.right, childRect.top);
mShadowTop.setAlpha((int) (mScrimOpacity * FULL_ALPHA));
mShadowTop.draw(canvas);
}
}
private boolean isPinnedViewTouched(View view, float x, float y) {
view.getHitRect(mTouchRect);
// by taping top or bottom padding, the list performs on click on a border item.
// we don't add top padding here to keep behavior consistent.
mTouchRect.top += mTranslateY;
mTouchRect.bottom += mTranslateY + getPaddingTop();
mTouchRect.left += getPaddingLeft();
mTouchRect.right -= getPaddingRight();
return mTouchRect.contains((int) x, (int) y);
}
private boolean isInIgnoredView(MotionEvent ev) {
Rect rect = new Rect();
for (View v : mIgnoredViews) {
v.getHitRect(rect);
if (rect.contains((int) ev.getX(), (int) ev.getY())) return true;
}
return false;
}
private boolean isPinnedViewTouched(View view, float x, float y) {
view.getHitRect(mTouchRect);
// by taping top or bottom padding, the list performs on click on a border item.
// we don't add top padding here to keep behavior consistent.
mTouchRect.top += mTranslateY;
mTouchRect.bottom += mTranslateY + getPaddingTop();
mTouchRect.left += getPaddingLeft();
mTouchRect.right -= getPaddingRight();
return mTouchRect.contains((int) x, (int) y);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
final int action = event.getAction();
final float x = event.getX();
final float y = event.getY();
final Rect frame = new Rect();
View leftHandle = leftSlider.tab;
leftHandle.getHitRect(frame);
boolean leftHit = frame.contains((int) x, (int) y);
View rightHandle = rightSlider.tab;
rightHandle.getHitRect(frame);
boolean rightHit = frame.contains((int) x, (int) y);
if (!tracking && !(leftHit || rightHit)) {
return false;
}
if (action == MotionEvent.ACTION_DOWN) {
tracking = true;
triggered = false;
vibrate(VIBRATE_SHORT);
if (leftHit) {
currentSlider = leftSlider;
targetZone = TARGET_ZONE;
rightSlider.hide();
} else {
currentSlider = rightSlider;
targetZone = 1.0f - TARGET_ZONE;
leftSlider.hide();
}
currentSlider.setState(Slider.STATE_PRESSED);
currentSlider.showTarget();
}
return true;
}
private boolean isInIgnoredView(MotionEvent ev) {
Rect rect = new Rect();
for (View v : mIgnoredViews) {
v.getHitRect(rect);
if (rect.contains((int)ev.getX(), (int)ev.getY())) return true;
}
return false;
}
/**
* Duplicate touch events to child views.
* We want to dispatch a down motion event and the move events to
* child views, but calling dispatchTouchEvent() causes StackOverflowError.
* Therefore we do it manually.
*
* @param ev Motion event to be passed to children.
* @param pendingEvents Pending events like ACTION_DOWN. This will be passed to the children before ev.
*/
private void duplicateTouchEventForChildren(MotionEvent ev, MotionEvent... pendingEvents) {
if (ev == null) {
return;
}
for (int i = getChildCount() - 1; 0 <= i; i--) {
View childView = getChildAt(i);
if (childView != null) {
Rect childRect = new Rect();
childView.getHitRect(childRect);
MotionEvent event = MotionEvent.obtainNoHistory(ev);
if (!childRect.contains((int) event.getX(), (int) event.getY())) {
continue;
}
float offsetX = -childView.getLeft();
float offsetY = -childView.getTop();
boolean consumed = false;
if (pendingEvents != null) {
for (MotionEvent pe : pendingEvents) {
if (pe != null) {
MotionEvent peAdjusted = MotionEvent.obtainNoHistory(pe);
peAdjusted.offsetLocation(offsetX, offsetY);
consumed |= childView.dispatchTouchEvent(peAdjusted);
}
}
}
event.offsetLocation(offsetX, offsetY);
consumed |= childView.dispatchTouchEvent(event);
if (consumed) {
break;
}
}
}
}
protected boolean isTransformedTouchPointInView(float x, float y, View child, PointF outLocalPoint) {
final Rect frame = new Rect();
child.getHitRect(frame);
return frame.contains((int) x, (int) y);
}
protected boolean isTransformedTouchPointInView(float x, float y, View child, PointF outLocalPoint) {
final Rect frame = new Rect();
child.getHitRect(frame);
return frame.contains((int) x, (int) y);
}
protected void exposeSectionItems(ScrollDirection direction) {
if (!mCanExposeScreen || layoutManager == null || mergeRecyclerAdapter == null || shieldLayoutManager == null) {
return;
}
int firstPosition = shieldLayoutManager.findFirstVisibleItemPosition(false);
int lastPostion = shieldLayoutManager.findLastVisibleItemPosition(false);
int firstCompletePosition = shieldLayoutManager.findFirstVisibleItemPosition(true);
int lastCompletePosition = shieldLayoutManager.findLastVisibleItemPosition(true);
//过滤auto offset的遮挡情况
if (layoutManager instanceof SetAutoOffsetInterface) {
int autoOffset = ((SetAutoOffsetInterface) layoutManager).getAutoOffset();
if (autoOffset > 0) {
for (int index = 0; index < recyclerView.getChildCount(); index++) {
View itemView = recyclerView.getChildAt(index);
if (itemView != null) {
int viewPosition;
if (recyclerView instanceof ShieldRecyclerViewInterface) {
viewPosition = ((ShieldRecyclerViewInterface) recyclerView).getShieldChildAdapterPosition(itemView);
} else {
viewPosition = recyclerView.getChildAdapterPosition(itemView);
}
if (viewPosition >= firstPosition) {
Rect itemRect = new Rect();
itemView.getHitRect(itemRect);
if (itemRect.bottom > autoOffset) {
if (itemRect.top < autoOffset) {
firstPosition = viewPosition;
} else if (itemRect.top > autoOffset) {
firstCompletePosition = viewPosition;
break;
} else {
firstPosition = viewPosition;
firstCompletePosition = viewPosition;
break;
}
}
}
}
}
}
}
ArrayList<ExposedInfo> exposedInfos = new ArrayList<>(lastPostion - firstPosition + 2);
for (int k = firstPosition; k <= lastPostion; k++) {
Pair<Integer, Integer> temSectionInfo = mergeRecyclerAdapter.getSectionIndex(k);
if (temSectionInfo == null) {
continue;
}
MergeSectionDividerAdapter.DetailSectionPositionInfo sectionInfo
= mergeRecyclerAdapter.getDetailSectionPositionInfo(temSectionInfo.first, temSectionInfo.second);
if (sectionInfo == null) {
continue;
}
ExposedInfo exposedInfo = new ExposedInfo();
exposedInfo.owner = mergeRecyclerAdapter.getPieceAdapter(sectionInfo.adapterIndex);
exposedInfo.details = new ExposedDetails();
exposedInfo.details.isComplete = false;
exposedInfo.details.section = sectionInfo.adapterSectionIndex;
exposedInfo.details.row = sectionInfo.adapterSectionPosition;
exposedInfo.details.cellType = exposedInfo.owner.getCellType(exposedInfo.details.section, exposedInfo.details.row);
if (k >= firstCompletePosition && k <= lastCompletePosition) {
exposedInfo.details.isComplete = true;
}
exposedInfos.add(exposedInfo);
}
mExposedEngine.updateExposedSections(exposedInfos, direction);
}
private boolean handleDownEvent(final MotionEvent motionEvent) {
// Find the child view that was touched (perform a hit test)
Rect rect = new Rect();
int childCount = mListView.getChildCount();
int[] listViewCoords = new int[2];
mListView.getLocationOnScreen(listViewCoords);
int x = (int) motionEvent.getRawX() - listViewCoords[0];
int y = (int) motionEvent.getRawY() - listViewCoords[1];
View downView = null;
for (int i = 0; i < childCount && downView == null; i++) {
View child = mListView.getChildAt(i);
child.getHitRect(rect);
if (rect.contains(x, y)) {
downView = child;
}
}
if (downView != null) {
mDownX = motionEvent.getRawX();
mDownY = motionEvent.getRawY();
int downPosition = AdapterViewUtil.getPositionForView(mListView, downView);
if (mDismissableManager != null) {
long downId = mListView.getAdapter().getItemId(downPosition);
if (!mDismissableManager.isDismissable(downId, downPosition)) {
/* Cancel, not dismissable */
return false;
}
}
mCurrentDismissData = createPendingDismissData(downPosition, downView);
if (mPendingDismisses.contains(mCurrentDismissData) || downPosition >= mVirtualListCount) {
// Cancel, we're already processing this position
mCurrentDismissData = null;
return false;
} else {
mTouchChildTouched = !mIsParentHorizontalScrollContainer && mResIdOfTouchChild == 0;
if (mResIdOfTouchChild != 0) {
mIsParentHorizontalScrollContainer = false;
final View childView = downView.findViewById(mResIdOfTouchChild);
if (childView != null) {
final Rect childRect = getChildViewRect(mListView, childView);
if (childRect.contains((int) motionEvent.getX(), (int) motionEvent.getY())) {
mTouchChildTouched = true;
mListView.requestDisallowInterceptTouchEvent(true);
}
}
}
if (mIsParentHorizontalScrollContainer) {
// Do it now and don't wait until the user moves more than
// the slop factor.
mTouchChildTouched = true;
mListView.requestDisallowInterceptTouchEvent(true);
}
mVelocityTracker = VelocityTracker.obtain();
mVelocityTracker.addMovement(motionEvent);
}
}
return true;
}