下面列出了android.graphics.Region#obtain ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns a list of {@code Rect}s, each of which is the bounding rectangle for a non-functional
* area on the display.
*
* There will be at most one non-functional area per short edge of the device, and none on
* the long edges.
*
* @return a list of bounding {@code Rect}s, one for each display cutout area.
*/
public List<Rect> getBoundingRects() {
List<Rect> result = new ArrayList<>();
Region bounds = Region.obtain();
// top
bounds.set(mBounds);
bounds.op(0, 0, Integer.MAX_VALUE, getSafeInsetTop(), Region.Op.INTERSECT);
if (!bounds.isEmpty()) {
result.add(bounds.getBounds());
}
// left
bounds.set(mBounds);
bounds.op(0, 0, getSafeInsetLeft(), Integer.MAX_VALUE, Region.Op.INTERSECT);
if (!bounds.isEmpty()) {
result.add(bounds.getBounds());
}
// right & bottom
bounds.set(mBounds);
bounds.op(getSafeInsetLeft() + 1, getSafeInsetTop() + 1,
Integer.MAX_VALUE, Integer.MAX_VALUE, Region.Op.INTERSECT);
if (!bounds.isEmpty()) {
result.add(bounds.getBounds());
}
bounds.recycle();
return result;
}
private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
DisplayCutout cutout, int rotation) {
if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
return WmDisplayCutout.NO_CUTOUT;
}
if (rotation == ROTATION_0) {
return WmDisplayCutout.computeSafeInsets(
cutout, mInitialDisplayWidth, mInitialDisplayHeight);
}
final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
final List<Rect> bounds = WmDisplayCutout.computeSafeInsets(
cutout, mInitialDisplayWidth, mInitialDisplayHeight)
.getDisplayCutout().getBoundingRects();
transformPhysicalToLogicalCoordinates(rotation, mInitialDisplayWidth, mInitialDisplayHeight,
mTmpMatrix);
final Region region = Region.obtain();
for (int i = 0; i < bounds.size(); i++) {
final Rect rect = bounds.get(i);
final RectF rectF = new RectF(bounds.get(i));
mTmpMatrix.mapRect(rectF);
rectF.round(rect);
region.op(rect, Op.UNION);
}
return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(region),
rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
}
/**
* Creates a DisplayCutout instance.
*
* @param copyArguments if true, create a copy of the arguments. If false, the passed arguments
* are not copied and MUST remain unchanged forever.
*/
private DisplayCutout(Rect safeInsets, Region bounds, boolean copyArguments) {
mSafeInsets = safeInsets == null ? ZERO_RECT :
(copyArguments ? new Rect(safeInsets) : safeInsets);
mBounds = bounds == null ? Region.obtain() :
(copyArguments ? Region.obtain(bounds) : bounds);
}
/**
* Insets the reference frame of the cutout in the given directions.
*
* @return a copy of this instance which has been inset
* @hide
*/
public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) {
if (mBounds.isEmpty()
|| insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0) {
return this;
}
Rect safeInsets = new Rect(mSafeInsets);
Region bounds = Region.obtain(mBounds);
// Note: it's not really well defined what happens when the inset is negative, because we
// don't know if the safe inset needs to expand in general.
if (insetTop > 0 || safeInsets.top > 0) {
safeInsets.top = atLeastZero(safeInsets.top - insetTop);
}
if (insetBottom > 0 || safeInsets.bottom > 0) {
safeInsets.bottom = atLeastZero(safeInsets.bottom - insetBottom);
}
if (insetLeft > 0 || safeInsets.left > 0) {
safeInsets.left = atLeastZero(safeInsets.left - insetLeft);
}
if (insetRight > 0 || safeInsets.right > 0) {
safeInsets.right = atLeastZero(safeInsets.right - insetRight);
}
bounds.translate(-insetLeft, -insetTop);
return new DisplayCutout(safeInsets, bounds, false /* copyArguments */);
}
/**
* Creates an instance from a bounding rect.
*
* @hide
*/
@VisibleForTesting
public static DisplayCutout fromBoundingRect(int left, int top, int right, int bottom) {
Region r = Region.obtain();
r.set(left, top, right, bottom);
return fromBounds(r);
}
private static Region boundingRectsToRegion(List<Rect> rects) {
Region result = Region.obtain();
if (rects != null) {
for (Rect r : rects) {
result.op(r, Region.Op.UNION);
}
}
return result;
}
/**
* Returns the region of the screen currently active for magnification. Changes to
* magnification scale and center only affect this portion of the screen. The rest of the
* screen, for example input methods, cannot be magnified. This region is relative to the
* unscaled screen and is independent of the scale and center point.
* <p>
* The returned region will be empty if magnification is not active. Magnification is active
* if magnification gestures are enabled or if a service is running that can control
* magnification.
* <p>
* <strong>Note:</strong> If the service is not yet connected (e.g.
* {@link AccessibilityService#onServiceConnected()} has not yet been
* called) or the service has been disconnected, this method will
* return an empty region.
*
* @return the region of the screen currently active for magnification, or an empty region
* if magnification is not active.
*/
@NonNull
public Region getMagnificationRegion() {
final IAccessibilityServiceConnection connection =
AccessibilityInteractionClient.getInstance().getConnection(
mService.mConnectionId);
if (connection != null) {
try {
return connection.getMagnificationRegion();
} catch (RemoteException re) {
Log.w(LOG_TAG, "Failed to obtain magnified region", re);
re.rethrowFromSystemServer();
}
}
return Region.obtain();
}
/**
* Returns the bounding region of the cutout.
*
* <p>
* <strong>Note:</strong> There may be more than one cutout, in which case the returned
* {@code Region} will be non-contiguous and its bounding rect will be meaningless without
* intersecting it first.
*
* Example:
* <pre>
* // Getting the bounding rectangle of the top display cutout
* Region bounds = displayCutout.getBounds();
* bounds.op(0, 0, Integer.MAX_VALUE, displayCutout.getSafeInsetTop(), Region.Op.INTERSECT);
* Rect topDisplayCutout = bounds.getBoundingRect();
* </pre>
*
* @return the bounding region of the cutout. Coordinates are relative
* to the top-left corner of the content view and in pixel units.
* @hide
*/
public Region getBounds() {
return Region.obtain(mBounds);
}