List of usage examples for android.graphics Rect contains
public boolean contains(Rect r)
From source file:com.google.android.apps.common.testing.accessibility.framework.ContrastInfoCheck.java
@Override public List<AccessibilityInfoCheckResult> runCheckOnInfoHierarchy(AccessibilityNodeInfo root, Context context, Bundle metadata) {//w w w . ja va2 s. com List<AccessibilityInfoCheckResult> results = new ArrayList<AccessibilityInfoCheckResult>(); Bitmap screenCapture = null; if (metadata != null) { screenCapture = metadata.getParcelable(AccessibilityCheckMetadata.METADATA_KEY_SCREEN_CAPTURE_BITMAP); } if (screenCapture == null) { results.add(new AccessibilityInfoCheckResult(getClass(), AccessibilityCheckResultType.NOT_RUN, "This check did not execute because it was unable to obtain screen capture data.", null)); return results; } AccessibilityNodeInfoCompat rootCompat = new AccessibilityNodeInfoCompat(root); List<AccessibilityNodeInfoCompat> candidates = AccessibilityNodeInfoUtils.searchAllFromBfs(context, rootCompat, FILTER_CONTRAST_EVAL_ELIGIBLE); List<AccessibilityNodeInfoCompat> nonCandidates = AccessibilityNodeInfoUtils.searchAllFromBfs(context, rootCompat, FILTER_CONTRAST_EVAL_INELIGIBLE); // Ineligible nodes all receive NOT_RUN results for (AccessibilityNodeInfoCompat nonCandidate : nonCandidates) { AccessibilityNodeInfo unwrappedNonCandidate = (AccessibilityNodeInfo) nonCandidate.getInfo(); results.add(new AccessibilityInfoCheckResult(getClass(), AccessibilityCheckResultType.NOT_RUN, "This view's contrast was not evaluated because it contains neither text nor an image.", unwrappedNonCandidate)); } Rect screenCaptureBounds = new Rect(0, 0, screenCapture.getWidth() - 1, screenCapture.getHeight() - 1); for (AccessibilityNodeInfoCompat candidate : candidates) { AccessibilityNodeInfo unwrappedCandidate = (AccessibilityNodeInfo) candidate.getInfo(); Rect viewBounds = new Rect(); unwrappedCandidate.getBoundsInScreen(viewBounds); if (!screenCaptureBounds.contains(viewBounds)) { // If an off-screen view reports itself as visible, we shouldn't evaluate it. String message = String.format("View bounds %1$s were not within the screen capture bounds %2$s.", viewBounds, screenCaptureBounds); results.add(new AccessibilityInfoCheckResult(getClass(), AccessibilityCheckResultType.NOT_RUN, message, unwrappedCandidate)); continue; } ContrastSwatch candidateSwatch = new ContrastSwatch( ScreenshotUtils.cropBitmap(screenCapture, viewBounds), viewBounds, unwrappedCandidate.getViewIdResourceName()); double contrastRatio = candidateSwatch.getContrastRatio(); if (AccessibilityNodeInfoUtils.nodeMatchesAnyClassByType(context, candidate, TextView.class)) { if (contrastRatio < ContrastUtils.CONTRAST_RATIO_WCAG_LARGE_TEXT) { String message = String.format( "This view's foreground to background contrast ratio " + "(%1$.2f) is not sufficient.", contrastRatio); results.add(new AccessibilityInfoCheckResult(getClass(), AccessibilityCheckResultType.ERROR, message, unwrappedCandidate)); } else if (contrastRatio < ContrastUtils.CONTRAST_RATIO_WCAG_NORMAL_TEXT) { String message = String.format( "This view's foreground to background contrast ratio " + "(%1$.2f) may not be sufficient unless it contains large text.", contrastRatio); results.add(new AccessibilityInfoCheckResult(getClass(), AccessibilityCheckResultType.WARNING, message, unwrappedCandidate)); } } else if (AccessibilityNodeInfoUtils.nodeMatchesAnyClassByType(context, candidate, ImageView.class)) { // Lower confidence in heuristics for ImageViews, so we'll report only warnings and use // the more permissive threshold ratio since images are generally large. if (contrastRatio < ContrastUtils.CONTRAST_RATIO_WCAG_LARGE_TEXT) { String message = String.format("This image's foreground to background contrast ratio " + "(%1$.2f) is not sufficient. NOTE: This test is experimental and may be less " + "accurate for some images.", contrastRatio); results.add(new AccessibilityInfoCheckResult(getClass(), AccessibilityCheckResultType.WARNING, message, unwrappedCandidate)); } } candidateSwatch.recycle(); } AccessibilityNodeInfoUtils.recycleNodes(candidates); AccessibilityNodeInfoUtils.recycleNodes(nonCandidates); return results; }
From source file:com.smoothsync.smoothsetup.MicroFragmentHostActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.smoothsetup_activity_microfragment_host); mCollapsingToolbar = findViewById(R.id.collapsing_toolbar); setSupportActionBar(mCollapsingToolbar.findViewById(R.id.toolbar)); mActionBar = getSupportActionBar();/*from w ww .j ava 2 s.c o m*/ mMicroFragmentStateDovecote = new ParcelableDovecote<>(this, "hostactivity", this); mBackDovecote = new BooleanDovecote(this, "backresult", aBoolean -> { if (!aBoolean) { finish(); } }); if (savedInstanceState == null) { // load the initial MicroFragment Bundle nestedExtras = getIntent().getBundleExtra("org.dmfs.nestedExtras"); MicroFragment<?> initialMicroFragment = nestedExtras.getParcelable("MicroFragment"); mMicroFragmentHost = new SimpleMicroFragmentFlow(initialMicroFragment, R.id.microfragment_host) .withPigeonCage(mMicroFragmentStateDovecote.cage()).start(this); } else { mMicroFragmentHost = savedInstanceState.getParcelable("microfragmenthost"); } mAppBarLayout = findViewById(R.id.appbar); mFragmentHostView = findViewById(R.id.microfragment_host); mFragmentHostView.getViewTreeObserver().addOnGlobalLayoutListener(() -> { // collapse appbar if the microfragment content doesn't fit on the screen Rect windowRect = new Rect(); mAppBarLayout.getWindowVisibleDisplayFrame(windowRect); FrameLayout bottomView = mFragmentHostView .findViewById(org.dmfs.android.microfragments.R.id.microfragments_host); if (bottomView.getChildCount() > 0) { View childView = bottomView.getChildAt(0); Rect childRect = new Rect(); childView.getGlobalVisibleRect(childRect); if (!"collapsed".equals(childView.getTag()) && !windowRect.contains(childRect)) { mAppBarLayout.postDelayed(() -> mAppBarLayout.setExpanded(false, true), 100); // tag the child as "collapsed" so we don't collapse it again while visible childView.setTag("collapsed"); } } }); }
From source file:com.marlonjones.voidlauncher.CellLayout.java
/** * Find a vacant area that will fit the given bounds nearest the requested * cell location. Uses Euclidean distance to score multiple vacant areas. * * @param pixelX The X location at which you want to search for a vacant area. * @param pixelY The Y location at which you want to search for a vacant area. * @param minSpanX The minimum horizontal span required * @param minSpanY The minimum vertical span required * @param spanX Horizontal span of the object. * @param spanY Vertical span of the object. * @param ignoreOccupied If true, the result can be an occupied cell * @param result Array in which to place the result, or null (in which case a new array will * be allocated)//w ww . jav a 2s .c o m * @return The X, Y cell of a vacant area that can contain this object, * nearest the requested location. */ private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) { lazyInitTempRectStack(); // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds // to the center of the item, but we are searching based on the top-left cell, so // we translate the point over to correspond to the top-left. pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f; pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f; // Keep track of best-scoring drop area final int[] bestXY = result != null ? result : new int[2]; double bestDistance = Double.MAX_VALUE; final Rect bestRect = new Rect(-1, -1, -1, -1); final Stack<Rect> validRegions = new Stack<Rect>(); final int countX = mCountX; final int countY = mCountY; if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 || spanX < minSpanX || spanY < minSpanY) { return bestXY; } for (int y = 0; y < countY - (minSpanY - 1); y++) { inner: for (int x = 0; x < countX - (minSpanX - 1); x++) { int ySize = -1; int xSize = -1; if (ignoreOccupied) { // First, let's see if this thing fits anywhere for (int i = 0; i < minSpanX; i++) { for (int j = 0; j < minSpanY; j++) { if (mOccupied.cells[x + i][y + j]) { continue inner; } } } xSize = minSpanX; ySize = minSpanY; // We know that the item will fit at _some_ acceptable size, now let's see // how big we can make it. We'll alternate between incrementing x and y spans // until we hit a limit. boolean incX = true; boolean hitMaxX = xSize >= spanX; boolean hitMaxY = ySize >= spanY; while (!(hitMaxX && hitMaxY)) { if (incX && !hitMaxX) { for (int j = 0; j < ySize; j++) { if (x + xSize > countX - 1 || mOccupied.cells[x + xSize][y + j]) { // We can't move out horizontally hitMaxX = true; } } if (!hitMaxX) { xSize++; } } else if (!hitMaxY) { for (int i = 0; i < xSize; i++) { if (y + ySize > countY - 1 || mOccupied.cells[x + i][y + ySize]) { // We can't move out vertically hitMaxY = true; } } if (!hitMaxY) { ySize++; } } hitMaxX |= xSize >= spanX; hitMaxY |= ySize >= spanY; incX = !incX; } incX = true; hitMaxX = xSize >= spanX; hitMaxY = ySize >= spanY; } final int[] cellXY = mTmpPoint; cellToCenterPoint(x, y, cellXY); // We verify that the current rect is not a sub-rect of any of our previous // candidates. In this case, the current rect is disqualified in favour of the // containing rect. Rect currentRect = mTempRectStack.pop(); currentRect.set(x, y, x + xSize, y + ySize); boolean contained = false; for (Rect r : validRegions) { if (r.contains(currentRect)) { contained = true; break; } } validRegions.push(currentRect); double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY); if ((distance <= bestDistance && !contained) || currentRect.contains(bestRect)) { bestDistance = distance; bestXY[0] = x; bestXY[1] = y; if (resultSpan != null) { resultSpan[0] = xSize; resultSpan[1] = ySize; } bestRect.set(currentRect); } } } // Return -1, -1 if no suitable location found if (bestDistance == Double.MAX_VALUE) { bestXY[0] = -1; bestXY[1] = -1; } recycleTempRects(validRegions); return bestXY; }
From source file:com.android.launcher3.CellLayout.java
/** * Find a vacant area that will fit the given bounds nearest the requested * cell location. Uses Euclidean distance to score multiple vacant areas. * * @param pixelX The X location at which you want to search for a vacant area. * @param pixelY The Y location at which you want to search for a vacant area. * @param minSpanX The minimum horizontal span required * @param minSpanY The minimum vertical span required * @param spanX Horizontal span of the object. * @param spanY Vertical span of the object. * @param ignoreOccupied If true, the result can be an occupied cell * @param result Array in which to place the result, or null (in which case a new array will * be allocated)/* ww w . j a v a 2 s . c o m*/ * @return The X, Y cell of a vacant area that can contain this object, * nearest the requested location. */ private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) { lazyInitTempRectStack(); // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds // to the center of the item, but we are searching based on the top-left cell, so // we translate the point over to correspond to the top-left. pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f; pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f; // Keep track of best-scoring drop area final int[] bestXY = result != null ? result : new int[2]; double bestDistance = Double.MAX_VALUE; final Rect bestRect = new Rect(-1, -1, -1, -1); final Stack<Rect> validRegions = new Stack<Rect>(); final int countX = mCountX; final int countY = mCountY; if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 || spanX < minSpanX || spanY < minSpanY) { return bestXY; } for (int y = 0; y < countY - (minSpanY - 1); y++) { inner: for (int x = 0; x < countX - (minSpanX - 1); x++) { int ySize = -1; int xSize = -1; if (ignoreOccupied) { // First, let's see if this thing fits anywhere for (int i = 0; i < minSpanX; i++) { for (int j = 0; j < minSpanY; j++) { if (mOccupied[x + i][y + j]) { continue inner; } } } xSize = minSpanX; ySize = minSpanY; // We know that the item will fit at _some_ acceptable size, now let's see // how big we can make it. We'll alternate between incrementing x and y spans // until we hit a limit. boolean incX = true; boolean hitMaxX = xSize >= spanX; boolean hitMaxY = ySize >= spanY; while (!(hitMaxX && hitMaxY)) { if (incX && !hitMaxX) { for (int j = 0; j < ySize; j++) { if (x + xSize > countX - 1 || mOccupied[x + xSize][y + j]) { // We can't move out horizontally hitMaxX = true; } } if (!hitMaxX) { xSize++; } } else if (!hitMaxY) { for (int i = 0; i < xSize; i++) { if (y + ySize > countY - 1 || mOccupied[x + i][y + ySize]) { // We can't move out vertically hitMaxY = true; } } if (!hitMaxY) { ySize++; } } hitMaxX |= xSize >= spanX; hitMaxY |= ySize >= spanY; incX = !incX; } incX = true; hitMaxX = xSize >= spanX; hitMaxY = ySize >= spanY; } final int[] cellXY = mTmpPoint; cellToCenterPoint(x, y, cellXY); // We verify that the current rect is not a sub-rect of any of our previous // candidates. In this case, the current rect is disqualified in favour of the // containing rect. Rect currentRect = mTempRectStack.pop(); currentRect.set(x, y, x + xSize, y + ySize); boolean contained = false; for (Rect r : validRegions) { if (r.contains(currentRect)) { contained = true; break; } } validRegions.push(currentRect); double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY); if ((distance <= bestDistance && !contained) || currentRect.contains(bestRect)) { bestDistance = distance; bestXY[0] = x; bestXY[1] = y; if (resultSpan != null) { resultSpan[0] = xSize; resultSpan[1] = ySize; } bestRect.set(currentRect); } } } // Return -1, -1 if no suitable location found if (bestDistance == Double.MAX_VALUE) { bestXY[0] = -1; bestXY[1] = -1; } recycleTempRects(validRegions); return bestXY; }
From source file:com.android.leanlauncher.CellLayout.java
/** * Find a vacant area that will fit the given bounds nearest the requested * cell location. Uses Euclidean distance to score multiple vacant areas. * * @param pixelX The X location at which you want to search for a vacant area. * @param pixelY The Y location at which you want to search for a vacant area. * @param minSpanX The minimum horizontal span required * @param minSpanY The minimum vertical span required * @param spanX Horizontal span of the object. * @param spanY Vertical span of the object. * @param ignoreOccupied If true, the result can be an occupied cell * @param result Array in which to place the result, or null (in which case a new array will * be allocated)/*from ww w.jav a 2 s .c om*/ * @return The X, Y cell of a vacant area that can contain this object, * nearest the requested location. */ int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY, View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan, boolean[][] occupied) { lazyInitTempRectStack(); // mark space take by ignoreView as available (method checks if ignoreView is null) markCellsAsUnoccupiedForView(ignoreView, occupied); // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds // to the center of the item, but we are searching based on the top-left cell, so // we translate the point over to correspond to the top-left. pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f; pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f; // Keep track of best-scoring drop area final int[] bestXY = result != null ? result : new int[2]; double bestDistance = Double.MAX_VALUE; final Rect bestRect = new Rect(-1, -1, -1, -1); final Stack<Rect> validRegions = new Stack<Rect>(); final int countX = mCountX; final int countY = mCountY; if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 || spanX < minSpanX || spanY < minSpanY) { return bestXY; } for (int y = 0; y < countY - (minSpanY - 1); y++) { inner: for (int x = 0; x < countX - (minSpanX - 1); x++) { int ySize = -1; int xSize = -1; if (ignoreOccupied) { // First, let's see if this thing fits anywhere for (int i = 0; i < minSpanX; i++) { for (int j = 0; j < minSpanY; j++) { if (occupied[x + i][y + j]) { continue inner; } } } xSize = minSpanX; ySize = minSpanY; // We know that the item will fit at _some_ acceptable size, now let's see // how big we can make it. We'll alternate between incrementing x and y spans // until we hit a limit. boolean incX = true; boolean hitMaxX = xSize >= spanX; boolean hitMaxY = ySize >= spanY; while (!(hitMaxX && hitMaxY)) { if (incX && !hitMaxX) { for (int j = 0; j < ySize; j++) { if (x + xSize > countX - 1 || occupied[x + xSize][y + j]) { // We can't move out horizontally hitMaxX = true; } } if (!hitMaxX) { xSize++; } } else if (!hitMaxY) { for (int i = 0; i < xSize; i++) { if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) { // We can't move out vertically hitMaxY = true; } } if (!hitMaxY) { ySize++; } } hitMaxX |= xSize >= spanX; hitMaxY |= ySize >= spanY; incX = !incX; } incX = true; hitMaxX = xSize >= spanX; hitMaxY = ySize >= spanY; } final int[] cellXY = mTmpXY; cellToCenterPoint(x, y, cellXY); // We verify that the current rect is not a sub-rect of any of our previous // candidates. In this case, the current rect is disqualified in favour of the // containing rect. Rect currentRect = mTempRectStack.pop(); currentRect.set(x, y, x + xSize, y + ySize); boolean contained = false; for (Rect r : validRegions) { if (r.contains(currentRect)) { contained = true; break; } } validRegions.push(currentRect); double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2) + Math.pow(cellXY[1] - pixelY, 2)); if ((distance <= bestDistance && !contained) || currentRect.contains(bestRect)) { bestDistance = distance; bestXY[0] = x; bestXY[1] = y; if (resultSpan != null) { resultSpan[0] = xSize; resultSpan[1] = ySize; } bestRect.set(currentRect); } } } // re-mark space taken by ignoreView as occupied markCellsAsOccupiedForView(ignoreView, occupied); // Return -1, -1 if no suitable location found if (bestDistance == Double.MAX_VALUE) { bestXY[0] = -1; bestXY[1] = -1; } recycleTempRects(validRegions); return bestXY; }
From source file:android.support.design.widget.CoordinatorLayout.java
private void offsetChildByInset(final View child, final Rect inset, final int layoutDirection) { if (!ViewCompat.isLaidOut(child)) { // The view has not been laid out yet, so we can't obtain its bounds. return;/* ww w . ja v a 2 s . c o m*/ } if (child.getWidth() <= 0 || child.getHeight() <= 0) { // Bounds are empty so there is nothing to dodge against, skip... return; } final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final Behavior behavior = lp.getBehavior(); final Rect dodgeRect = acquireTempRect(); final Rect bounds = acquireTempRect(); bounds.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom()); if (behavior != null && behavior.getInsetDodgeRect(this, child, dodgeRect)) { // Make sure that the rect is within the view's bounds if (!bounds.contains(dodgeRect)) { throw new IllegalArgumentException("Rect should be within the child's bounds." + " Rect:" + dodgeRect.toShortString() + " | Bounds:" + bounds.toShortString()); } } else { dodgeRect.set(bounds); } // We can release the bounds rect now releaseTempRect(bounds); if (dodgeRect.isEmpty()) { // Rect is empty so there is nothing to dodge against, skip... releaseTempRect(dodgeRect); return; } final int absDodgeInsetEdges = GravityCompat.getAbsoluteGravity(lp.dodgeInsetEdges, layoutDirection); boolean offsetY = false; if ((absDodgeInsetEdges & Gravity.TOP) == Gravity.TOP) { int distance = dodgeRect.top - lp.topMargin - lp.mInsetOffsetY; if (distance < inset.top) { setInsetOffsetY(child, inset.top - distance); offsetY = true; } } if ((absDodgeInsetEdges & Gravity.BOTTOM) == Gravity.BOTTOM) { int distance = getHeight() - dodgeRect.bottom - lp.bottomMargin + lp.mInsetOffsetY; if (distance < inset.bottom) { setInsetOffsetY(child, distance - inset.bottom); offsetY = true; } } if (!offsetY) { setInsetOffsetY(child, 0); } boolean offsetX = false; if ((absDodgeInsetEdges & Gravity.LEFT) == Gravity.LEFT) { int distance = dodgeRect.left - lp.leftMargin - lp.mInsetOffsetX; if (distance < inset.left) { setInsetOffsetX(child, inset.left - distance); offsetX = true; } } if ((absDodgeInsetEdges & Gravity.RIGHT) == Gravity.RIGHT) { int distance = getWidth() - dodgeRect.right - lp.rightMargin + lp.mInsetOffsetX; if (distance < inset.right) { setInsetOffsetX(child, distance - inset.right); offsetX = true; } } if (!offsetX) { setInsetOffsetX(child, 0); } releaseTempRect(dodgeRect); }