下面列出了android.graphics.Rect#setEmpty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (parent.getChildAdapterPosition(view) < mColumns) {
if (mHorizontal) {
if (mView.getMeasuredWidth() <= 0) {
mView.measure(View.MeasureSpec.makeMeasureSpec(parent.getMeasuredWidth(), View.MeasureSpec.AT_MOST),
View.MeasureSpec.makeMeasureSpec(parent.getMeasuredHeight(), View.MeasureSpec.AT_MOST));
}
outRect.set(mView.getMeasuredWidth(), 0, 0, 0);
} else {
if (mView.getMeasuredHeight() <= 0) {
mView.measure(View.MeasureSpec.makeMeasureSpec(parent.getMeasuredWidth(), View.MeasureSpec.AT_MOST),
View.MeasureSpec.makeMeasureSpec(parent.getMeasuredHeight(), View.MeasureSpec.AT_MOST));
}
outRect.set(0, mView.getMeasuredHeight(), 0, 0);
}
} else {
outRect.setEmpty();
}
}
@Override
public void adjustLayout(int startPosition, int endPosition, LayoutManagerHelper helper) {
if (requireLayoutView()) {
View refer = null;
Rect tempRect = new Rect();
final OrientationHelperEx orientationHelper = helper.getMainOrientationHelper();
for (int i = 0; i < helper.getChildCount(); i++) {
refer = helper.getChildAt(i);
int anchorPos = helper.getPosition(refer);
if (getRange().contains(anchorPos)) {
if (refer.getVisibility() == View.GONE) {
tempRect.setEmpty();
} else {
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
refer.getLayoutParams();
if (helper.getOrientation() == VirtualLayoutManager.VERTICAL) {
tempRect.union(helper.getDecoratedLeft(refer) - params.leftMargin,
orientationHelper.getDecoratedStart(refer),
helper.getDecoratedRight(refer) + params.rightMargin,
orientationHelper.getDecoratedEnd(refer));
} else {
tempRect.union(orientationHelper.getDecoratedStart(refer),
helper.getDecoratedTop(refer) - params.topMargin, orientationHelper.getDecoratedEnd(refer),
helper.getDecoratedBottom(refer) + params.bottomMargin);
}
}
}
}
if (!tempRect.isEmpty()) {
mLayoutRegion.set(tempRect.left - mPaddingLeft, tempRect.top - mPaddingTop,
tempRect.right + mPaddingRight, tempRect.bottom + mPaddingBottom);
} else {
mLayoutRegion.setEmpty();
}
if (mLayoutView != null) {
mLayoutView.layout(mLayoutRegion.left, mLayoutRegion.top, mLayoutRegion.right, mLayoutRegion.bottom);
}
}
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view);
int viewType = 0;
try {
viewType = parent.getAdapter() != null ? parent.getAdapter().getItemViewType(position) : 0;
} catch (NullPointerException ignored) {
}
if (viewType != 1) {
outRect.set(0, 0, 0, _heightDp);
} else {
outRect.setEmpty();
}
}
/**
* Get desired popup window width by calculating the maximum text length from Autofill data.
* @param data Autofill suggestion data.
* @return The popup window width in DIP.
*/
private float getDesiredWidth(ArrayList<AutofillSuggestion> data) {
if (mLabelViewPaint == null || mSublabelViewPaint == null) {
LayoutInflater inflater =
(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.autofill_text, null);
TextView labelView = (TextView) layout.findViewById(R.id.autofill_label);
mLabelViewPaint = labelView.getPaint();
TextView sublabelView = (TextView) layout.findViewById(R.id.autofill_sublabel);
mSublabelViewPaint = sublabelView.getPaint();
}
float maxTextWidth = 0;
Rect bounds = new Rect();
for (int i = 0; i < data.size(); ++i) {
bounds.setEmpty();
String label = data.get(i).mLabel;
if (!TextUtils.isEmpty(label)) {
mLabelViewPaint.getTextBounds(label, 0, label.length(), bounds);
}
float labelWidth = bounds.width();
bounds.setEmpty();
String sublabel = data.get(i).mSublabel;
if (!TextUtils.isEmpty(sublabel)) {
mSublabelViewPaint.getTextBounds(sublabel, 0, sublabel.length(), bounds);
}
float localMax = Math.max(labelWidth, bounds.width());
maxTextWidth = Math.max(maxTextWidth, localMax);
}
// Scale it down to make it unscaled by screen density.
maxTextWidth = maxTextWidth / mContext.getResources().getDisplayMetrics().density;
// Adding padding.
return maxTextWidth + TEXT_PADDING_DP;
}
private void processVisibilityOutputs() {
final Rect currentVisibleArea = new Rect();
final boolean visible = getLocalVisibleRect(currentVisibleArea);
if (!visible) {
currentVisibleArea.setEmpty();
}
processVisibilityOutputs(currentVisibleArea);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (hasDividerOnBottom(view, parent, state)) {
outRect.set(0, 0, 0, mHeightDp);
} else {
outRect.setEmpty();
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
// we want to retrieve the position in the list
final int position = params.getViewAdapterPosition();
// and add a separator to any view but the last one
if (position < state.getItemCount()) {
outRect.set(0, 0, 0, (int) mPaint.getStrokeWidth()); // left, top, right, bottom
} else {
outRect.setEmpty(); // 0, 0, 0, 0
}
}
/**
* Calculate the window-space crop rect and fill clipRect.
* @return true if clipRect has been filled otherwise, no window space crop should be applied.
*/
private boolean calculateCrop(Rect clipRect) {
final WindowState w = mWin;
final DisplayContent displayContent = w.getDisplayContent();
clipRect.setEmpty();
if (displayContent == null) {
return false;
}
if (w.inPinnedWindowingMode()) {
return false;
}
// During forced seamless rotation, the surface bounds get updated with the crop in the
// new rotation, which is not compatible with showing the surface in the old rotation.
// To work around that we disable cropping for such windows, as it is not necessary anyways.
if (w.mForceSeamlesslyRotate) {
return false;
}
// If we're animating, the wallpaper should only
// be updated at the end of the animation.
if (w.mAttrs.type == TYPE_WALLPAPER) {
return false;
}
if (DEBUG_WINDOW_CROP) Slog.d(TAG,
"Updating crop win=" + w + " mLastCrop=" + mLastClipRect);
w.calculatePolicyCrop(mSystemDecorRect);
if (DEBUG_WINDOW_CROP) Slog.d(TAG, "Applying decor to crop win=" + w + " mDecorFrame="
+ w.mDecorFrame + " mSystemDecorRect=" + mSystemDecorRect);
final Task task = w.getTask();
final boolean fullscreen = w.fillsDisplay() || (task != null && task.isFullscreen());
final boolean isFreeformResizing =
w.isDragResizing() && w.getResizeMode() == DRAG_RESIZE_MODE_FREEFORM;
// We use the clip rect as provided by the tranformation for non-fullscreen windows to
// avoid premature clipping with the system decor rect.
clipRect.set(mSystemDecorRect);
if (DEBUG_WINDOW_CROP) Slog.d(TAG, "win=" + w + " Initial clip rect: " + clipRect
+ " fullscreen=" + fullscreen);
w.expandForSurfaceInsets(clipRect);
// The clip rect was generated assuming (0,0) as the window origin,
// so we need to translate to match the actual surface coordinates.
clipRect.offset(w.mAttrs.surfaceInsets.left, w.mAttrs.surfaceInsets.top);
if (DEBUG_WINDOW_CROP) Slog.d(TAG,
"win=" + w + " Clip rect after stack adjustment=" + clipRect);
w.transformClipRectFromScreenToSurfaceSpace(clipRect);
return true;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
outRect.setEmpty();
}
public void adjustLayout(int startPosition, int endPosition, LayoutManagerHelper helper) {
if (!isChildrenEmpty()) {
for (Map.Entry<Range<Integer>, T> entry : mChildren.entrySet()) {
RangeStyle rangeStyle = entry.getValue();
rangeStyle.adjustLayout(startPosition, endPosition, helper);
}
}
if (requireLayoutView()) {
View refer = null;
Rect tempRect = new Rect();
final OrientationHelperEx orientationHelper = helper.getMainOrientationHelper();
for (int i = 0; i < helper.getChildCount(); i++) {
refer = helper.getChildAt(i);
int anchorPos = helper.getPosition(refer);
if (getRange().contains(anchorPos)) {
if (refer.getVisibility() == View.GONE) {
tempRect.setEmpty();
} else {
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
refer.getLayoutParams();
if (helper.getOrientation() == VirtualLayoutManager.VERTICAL) {
tempRect.union(helper.getDecoratedLeft(refer) - params.leftMargin,
orientationHelper.getDecoratedStart(refer),
helper.getDecoratedRight(refer) + params.rightMargin,
orientationHelper.getDecoratedEnd(refer));
} else {
tempRect.union(orientationHelper.getDecoratedStart(refer),
helper.getDecoratedTop(refer) - params.topMargin, orientationHelper.getDecoratedEnd(refer),
helper.getDecoratedBottom(refer) + params.bottomMargin);
}
}
}
}
if (!tempRect.isEmpty()) {
mLayoutRegion.set(tempRect.left - mPaddingLeft, tempRect.top - mPaddingTop,
tempRect.right + mPaddingRight, tempRect.bottom + mPaddingBottom);
} else {
mLayoutRegion.setEmpty();
}
if (mLayoutView != null) {
mLayoutView.layout(mLayoutRegion.left, mLayoutRegion.top, mLayoutRegion.right, mLayoutRegion.bottom);
}
}
}
/**
* Find the largest sub-rectangle that doesn't intersect a specified one. <strong>Note:</strong>
* {@code rectToModify} and {@code otherRect} will be sorted after operation.
*
* @param rectToModify The rect that may be modified to avoid intersections
* @param otherRect The rect that should be avoided
*/
public static void adjustRectToAvoidIntersection(Rect rectToModify, Rect otherRect) {
/*
* Some rectangles are flipped around (left > right). Make sure we have two Rects free of
* such pathologies.
*/
rectToModify.sort();
otherRect.sort();
if (rectToModify.contains(otherRect) || !Rect.intersects(rectToModify, otherRect)) {
return;
}
/*
* Intersect rectToModify with four rects that represent cuts of the entire space along
* lines defined by the otherRect's edges
*/
Rect[] cuts = {
new Rect(rectToModify.left, rectToModify.top, otherRect.left, rectToModify.bottom),
new Rect(rectToModify.left, rectToModify.top, rectToModify.right, otherRect.top),
new Rect(otherRect.right, rectToModify.top, rectToModify.right, rectToModify.bottom),
new Rect(rectToModify.left, otherRect.bottom, rectToModify.right, rectToModify.bottom)
};
int maxIntersectingRectArea = 0;
int indexOfLargestIntersection = -1;
for (int i = 0; i < cuts.length; i++) {
if (!isSorted(cuts[i])) {
continue;
}
if (Rect.intersects(cuts[i], rectToModify)) {
/* Reassign this cut to its intersection with rectToModify */
int visibleRectArea = cuts[i].width() * cuts[i].height();
if (visibleRectArea > maxIntersectingRectArea) {
maxIntersectingRectArea = visibleRectArea;
indexOfLargestIntersection = i;
}
}
}
if (maxIntersectingRectArea <= 0) {
// The rectToModify isn't within any of our cuts, so it's entirely occuled by otherRect.
rectToModify.setEmpty();
return;
}
rectToModify.set(cuts[indexOfLargestIntersection]);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
final Rect padding = mTempRect;
int thumbWidth;
int thumbHeight;
if (mThumbDrawable != null)
{
mThumbDrawable.getPadding(padding);
thumbWidth = mThumbDrawable.getIntrinsicWidth() - padding.left - padding.right;
thumbHeight = mThumbDrawable.getIntrinsicHeight();
}
else
{
thumbWidth = 0;
thumbHeight = 0;
}
mThumbWidth = thumbWidth;
int trackHeight;
if (mTrackDrawable != null)
{
mTrackDrawable.getPadding(padding);
trackHeight = mTrackDrawable.getIntrinsicHeight();
}
else
{
padding.setEmpty();
trackHeight = 0;
}
int paddingLeft = padding.left;
int paddingRight = padding.right;
if (mThumbDrawable != null)
{
final Insets inset = Insets.NONE;
paddingLeft = Math.max(paddingLeft, inset.left);
paddingRight = Math.max(paddingRight, inset.right);
}
final int switchWidth = Math.max(mSwitchMinWidth, 2 * mThumbWidth + paddingLeft + paddingRight);
final int switchHeight = Math.max(trackHeight, thumbHeight);
mSwitchWidth = switchWidth;
mSwitchHeight = switchHeight;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int measuredHeight = getMeasuredHeight();
if (measuredHeight < switchHeight)
{
setMeasuredDimension(switchWidth, switchHeight);
}
}
@Override public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
@NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
outRect.setEmpty();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
super.onLayout(changed, left, top, right, bottom);
wasLayout = true;
int opticalInsetLeft = 0;
int opticalInsetRight = 0;
if (mThumbDrawable != null)
{
final Rect trackPadding = mTempRect;
if (mTrackDrawable != null)
{
mTrackDrawable.getPadding(trackPadding);
}
else
{
trackPadding.setEmpty();
}
final Insets insets = Insets.NONE;
opticalInsetLeft = Math.max(0, insets.left - trackPadding.left);
opticalInsetRight = Math.max(0, insets.right - trackPadding.right);
}
final int switchRight;
final int switchLeft;
if (LocaleController.isRTL)
{
switchLeft = getPaddingLeft() + opticalInsetLeft;
switchRight = switchLeft + mSwitchWidth - opticalInsetLeft - opticalInsetRight;
}
else
{
switchRight = getWidth() - getPaddingRight() - opticalInsetRight;
switchLeft = switchRight - mSwitchWidth + opticalInsetLeft + opticalInsetRight;
}
final int switchTop;
final int switchBottom;
switch (getGravity() & Gravity.VERTICAL_GRAVITY_MASK)
{
default:
case Gravity.TOP:
switchTop = getPaddingTop();
switchBottom = switchTop + mSwitchHeight;
break;
case Gravity.CENTER_VERTICAL:
switchTop = (getPaddingTop() + getHeight() - getPaddingBottom()) / 2 - mSwitchHeight / 2;
switchBottom = switchTop + mSwitchHeight;
break;
case Gravity.BOTTOM:
switchBottom = getHeight() - getPaddingBottom();
switchTop = switchBottom - mSwitchHeight;
break;
}
mSwitchLeft = switchLeft;
mSwitchTop = switchTop;
mSwitchBottom = switchBottom;
mSwitchRight = switchRight;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mShowText) {
if (mOnLayout == null) {
mOnLayout = makeLayout(mTextOn);
}
if (mOffLayout == null) {
mOffLayout = makeLayout(mTextOff);
}
}
final Rect padding = mTempRect;
final int thumbWidth;
final int thumbHeight;
if (mThumbDrawable != null) {
// Cached thumb width does not include padding.
mThumbDrawable.getPadding(padding);
thumbWidth = mThumbDrawable.getIntrinsicWidth() - padding.left - padding.right;
thumbHeight = mThumbDrawable.getIntrinsicHeight();
} else {
thumbWidth = 0;
thumbHeight = 0;
}
final int maxTextWidth;
if (mShowText) {
maxTextWidth = Math.max(mOnLayout.getWidth(), mOffLayout.getWidth())
+ mThumbTextPadding * 2;
} else {
maxTextWidth = 0;
}
mThumbWidth = Math.max(maxTextWidth, thumbWidth);
final int trackHeight;
if (mTrackDrawable != null) {
mTrackDrawable.getPadding(padding);
trackHeight = mTrackDrawable.getIntrinsicHeight();
} else {
padding.setEmpty();
trackHeight = 0;
}
// Adjust left and right padding to ensure there's enough room for the
// thumb's padding (when present).
int paddingLeft = padding.left;
int paddingRight = padding.right;
if (mThumbDrawable != null) {
final Insets inset = mThumbDrawable.getOpticalInsets();
paddingLeft = Math.max(paddingLeft, inset.left);
paddingRight = Math.max(paddingRight, inset.right);
}
final int switchWidth = Math.max(mSwitchMinWidth,
2 * mThumbWidth + paddingLeft + paddingRight);
final int switchHeight = Math.max(trackHeight, thumbHeight);
mSwitchWidth = switchWidth;
mSwitchHeight = switchHeight;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int measuredHeight = getMeasuredHeight();
if (measuredHeight < switchHeight) {
setMeasuredDimension(getMeasuredWidthAndState(), switchHeight);
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
outRect.setEmpty();
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
outRect.setEmpty();
}
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
outRect.setEmpty();
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
final Rect padding = mTempRect;
int thumbWidth;
int thumbHeight;
if (mThumbDrawable != null)
{
mThumbDrawable.getPadding(padding);
thumbWidth = mThumbDrawable.getIntrinsicWidth() - padding.left - padding.right;
thumbHeight = mThumbDrawable.getIntrinsicHeight();
}
else
{
thumbWidth = 0;
thumbHeight = 0;
}
mThumbWidth = thumbWidth;
int trackHeight;
if (mTrackDrawable != null)
{
mTrackDrawable.getPadding(padding);
trackHeight = mTrackDrawable.getIntrinsicHeight();
}
else
{
padding.setEmpty();
trackHeight = 0;
}
int paddingLeft = padding.left;
int paddingRight = padding.right;
if (mThumbDrawable != null)
{
final Insets inset = Insets.NONE;
paddingLeft = Math.max(paddingLeft, inset.left);
paddingRight = Math.max(paddingRight, inset.right);
}
final int switchWidth = Math.max(mSwitchMinWidth, 2 * mThumbWidth + paddingLeft + paddingRight);
final int switchHeight = Math.max(trackHeight, thumbHeight);
mSwitchWidth = switchWidth;
mSwitchHeight = switchHeight;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int measuredHeight = getMeasuredHeight();
if (measuredHeight < switchHeight)
{
setMeasuredDimension(switchWidth, switchHeight);
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
super.onLayout(changed, left, top, right, bottom);
wasLayout = true;
int opticalInsetLeft = 0;
int opticalInsetRight = 0;
if (mThumbDrawable != null)
{
final Rect trackPadding = mTempRect;
if (mTrackDrawable != null)
{
mTrackDrawable.getPadding(trackPadding);
}
else
{
trackPadding.setEmpty();
}
final Insets insets = Insets.NONE;
opticalInsetLeft = Math.max(0, insets.left - trackPadding.left);
opticalInsetRight = Math.max(0, insets.right - trackPadding.right);
}
final int switchRight;
final int switchLeft;
if (LocaleController.isRTL)
{
switchLeft = getPaddingLeft() + opticalInsetLeft;
switchRight = switchLeft + mSwitchWidth - opticalInsetLeft - opticalInsetRight;
}
else
{
switchRight = getWidth() - getPaddingRight() - opticalInsetRight;
switchLeft = switchRight - mSwitchWidth + opticalInsetLeft + opticalInsetRight;
}
final int switchTop;
final int switchBottom;
switch (getGravity() & Gravity.VERTICAL_GRAVITY_MASK)
{
default:
case Gravity.TOP:
switchTop = getPaddingTop();
switchBottom = switchTop + mSwitchHeight;
break;
case Gravity.CENTER_VERTICAL:
switchTop = (getPaddingTop() + getHeight() - getPaddingBottom()) / 2 - mSwitchHeight / 2;
switchBottom = switchTop + mSwitchHeight;
break;
case Gravity.BOTTOM:
switchBottom = getHeight() - getPaddingBottom();
switchTop = switchBottom - mSwitchHeight;
break;
}
mSwitchLeft = switchLeft;
mSwitchTop = switchTop;
mSwitchBottom = switchBottom;
mSwitchRight = switchRight;
}