下面列出了android.graphics.RectF#setEmpty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Calculates the chip icon's ChipDrawable-absolute bounds (top-left is <code>
* [ChipDrawable.getBounds().left, ChipDrawable.getBounds().top]</code>).
*/
private void calculateChipIconBounds(@NonNull Rect bounds, @NonNull RectF outBounds) {
outBounds.setEmpty();
if (showsChipIcon() || showsCheckedIcon()) {
float offsetFromStart = chipStartPadding + iconStartPadding;
float chipWidth = getCurrentChipIconWidth();
if (DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR) {
outBounds.left = bounds.left + offsetFromStart;
outBounds.right = outBounds.left + chipWidth;
} else {
outBounds.right = bounds.right - offsetFromStart;
outBounds.left = outBounds.right - chipWidth;
}
float chipHeight = getCurrentChipIconHeight();
outBounds.top = bounds.exactCenterY() - chipHeight / 2f;
outBounds.bottom = outBounds.top + chipHeight;
}
}
/**
* Calculates the chip text's ChipDrawable-absolute bounds (top-left is <code>
* [ChipDrawable.getBounds().left, ChipDrawable.getBounds().top]</code>).
*/
private void calculateTextBounds(@NonNull Rect bounds, @NonNull RectF outBounds) {
outBounds.setEmpty();
if (text != null) {
float offsetFromStart = chipStartPadding + calculateChipIconWidth() + textStartPadding;
float offsetFromEnd = chipEndPadding + calculateCloseIconWidth() + textEndPadding;
if (DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR) {
outBounds.left = bounds.left + offsetFromStart;
outBounds.right = bounds.right - offsetFromEnd;
} else {
outBounds.left = bounds.left + offsetFromEnd;
outBounds.right = bounds.right - offsetFromStart;
}
// Top and bottom included for completion. Don't position the chip text vertically based on
// these bounds. Instead, use #calculateTextOriginAndAlignment().
outBounds.top = bounds.top;
outBounds.bottom = bounds.bottom;
}
}
/**
* Calculates the close icon's ChipDrawable-absolute bounds (top-left is <code>
* [ChipDrawable.getBounds().left, ChipDrawable.getBounds().top]</code>).
*/
private void calculateCloseIconBounds(@NonNull Rect bounds, @NonNull RectF outBounds) {
outBounds.setEmpty();
if (showsCloseIcon()) {
float offsetFromEnd = chipEndPadding + closeIconEndPadding;
if (DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR) {
outBounds.right = bounds.right - offsetFromEnd;
outBounds.left = outBounds.right - closeIconSize;
} else {
outBounds.left = bounds.left + offsetFromEnd;
outBounds.right = outBounds.left + closeIconSize;
}
outBounds.top = bounds.exactCenterY() - closeIconSize / 2f;
outBounds.bottom = outBounds.top + closeIconSize;
}
}
private void calculateCloseIconTouchBounds(@NonNull Rect bounds, @NonNull RectF outBounds) {
outBounds.setEmpty();
if (showsCloseIcon()) {
float offsetFromEnd =
chipEndPadding
+ closeIconEndPadding
+ closeIconSize
+ closeIconStartPadding
+ textEndPadding;
if (DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR) {
outBounds.right = bounds.right;
outBounds.left = outBounds.right - offsetFromEnd;
} else {
outBounds.left = bounds.left;
outBounds.right = bounds.left + offsetFromEnd;
}
outBounds.top = bounds.top;
outBounds.bottom = bounds.bottom;
}
}
@Override
public void getTouchBound(ScrollbarRecyclerView view, RectF bound, int slop) {
if (mAnimatorValue == 0 && !mAlwaysTouchable) {
bound.setEmpty();
return;
}
if (mTouchStartOnBar) {
bound.set(mSliderBound.left - slop,
view.getPaddingTop() + mPaddingStart - slop,
mSliderBound.right + slop,
view.getHeight() - view.getPaddingBottom() - mPaddingEnd
+ slop);
} else {
bound.set(mSliderBound.left - slop,
mSliderBound.top - slop,
mSliderBound.right + slop,
mSliderBound.bottom + slop);
}
}
@Override
public void getTouchBound(ScrollbarRecyclerView view, RectF bound, int slop) {
if (mAnimatorValue == 0 && !mAlwaysTouchable) {
bound.setEmpty();
return;
}
if (mTouchStartOnBar) {
bound.set(view.getPaddingLeft() + mPaddingStart - slop,
mSliderBound.top - slop,
view.getWidth() - view.getPaddingRight() - mPaddingEnd + slop,
mSliderBound.bottom + slop);
} else {
bound.set(mSliderBound.left - slop,
mSliderBound.top - slop,
mSliderBound.right + slop,
mSliderBound.bottom + slop);
}
}
private void updateDrawableBounds(DrawableInfo drawableInfo) {
final Drawable drawable = drawableInfo.mDrawable;
final float radius = mSteinerCircleRadius;
if (radius <= 0) {
drawable.setBounds(0, 0, 0, 0);
return;
}
final int dWidth = drawable.getIntrinsicWidth();
final int dHeight = drawable.getIntrinsicHeight();
final RectF bounds = mTempBounds;
bounds.setEmpty();
if (dWidth <= 0 || dHeight <= 0 || dWidth == dHeight || FitType.FIT == mFitType) {
bounds.inset(-radius, -radius);
} else {
float scale;
if (dWidth > dHeight) {
scale = radius / (float) dHeight;
} else {
scale = radius / (float) dWidth;
}
bounds.inset(-dWidth * scale, -dHeight * scale);
if (FitType.START == mFitType || FitType.END == mFitType) {
int dir = FitType.START == mFitType ? 1 : -1;
bounds.offset((bounds.width() * 0.5f - radius) * dir,
(bounds.height() * 0.5f - radius) * dir);
}
}
bounds.offset(drawableInfo.mCenterX, drawableInfo.mCenterY);
drawable.setBounds((int) bounds.left, (int) bounds.top,
Math.round(bounds.right), Math.round(bounds.bottom));
}
/**
* 获取绘制区域
*/
void getDrawRect(RectF rectF) {
if (!imageZoomer.isWorking()) {
if (SLog.isLoggable(SLog.LEVEL_VERBOSE | SLog.TYPE_ZOOM)) {
SLog.v(NAME, "not working. getDrawRect");
}
rectF.setEmpty();
return;
}
Size drawableSize = imageZoomer.getDrawableSize();
rectF.set(0, 0, drawableSize.getWidth(), drawableSize.getHeight());
getDrawMatrix().mapRect(rectF);
}
/**
* Merges a {@link RectF} into an existing list of any objects which contain a rectangle.
* While merging, this method makes sure that:
*
* <ol>
* <li>No rectangle is redundant (contained within a bigger rectangle)</li>
* <li>Rectangles of the same height and vertical position that intersect get merged</li>
* </ol>
*
* @param list the list of rectangles (or other rectangle containers) to merge the new
* rectangle into
* @param candidate the {@link RectF} to merge into the list
* @param extractor a function that can extract a {@link RectF} from an element of the given
* list
* @param packer a function that can wrap the resulting {@link RectF} into an element that
* the list contains
* @hide
*/
@VisibleForTesting
public static <T> void mergeRectangleIntoList(final List<T> list,
final RectF candidate, final Function<T, RectF> extractor,
final Function<RectF, T> packer) {
if (candidate.isEmpty()) {
return;
}
final int elementCount = list.size();
for (int index = 0; index < elementCount; ++index) {
final RectF existingRectangle = extractor.apply(list.get(index));
if (existingRectangle.contains(candidate)) {
return;
}
if (candidate.contains(existingRectangle)) {
existingRectangle.setEmpty();
continue;
}
final boolean rectanglesContinueEachOther = candidate.left == existingRectangle.right
|| candidate.right == existingRectangle.left;
final boolean canMerge = candidate.top == existingRectangle.top
&& candidate.bottom == existingRectangle.bottom
&& (RectF.intersects(candidate, existingRectangle)
|| rectanglesContinueEachOther);
if (canMerge) {
candidate.union(existingRectangle);
existingRectangle.setEmpty();
}
}
for (int index = elementCount - 1; index >= 0; --index) {
final RectF rectangle = extractor.apply(list.get(index));
if (rectangle.isEmpty()) {
list.remove(index);
}
}
list.add(packer.apply(candidate));
}
@Override
protected RectF resetInstance(RectF obj) {
obj.setEmpty();
return obj;
}
@Override
protected RectF resetInstance(RectF obj) {
obj.setEmpty();
return obj;
}
@Override
protected RectF resetInstance(RectF obj) {
obj.setEmpty();
return obj;
}
@Override
protected RectF resetInstance(RectF obj) {
obj.setEmpty();
return obj;
}
@Override
protected RectF resetInstance(RectF obj) {
obj.setEmpty();
return obj;
}
@Override
protected RectF resetInstance(RectF obj) {
obj.setEmpty();
return obj;
}
@Override
protected RectF resetInstance(RectF obj) {
obj.setEmpty();
return obj;
}
@Override
protected RectF resetInstance(RectF obj) {
obj.setEmpty();
return obj;
}
void onDraw(@NonNull Canvas canvas) {
final RectF drawRectF = tempDisplayRectF;
imageZoomer.getDrawRect(drawRectF);
if (drawRectF.isEmpty()) {
if (SLog.isLoggable(SLog.LEVEL_DEBUG | SLog.TYPE_ZOOM)) {
SLog.d(ImageZoomer.NAME, "displayRectF is empty. drawScrollBar. drawRectF=%s", drawRectF.toString());
}
return;
}
Size viewSize = imageZoomer.getViewSize();
final int viewWidth = viewSize.getWidth();
final int viewHeight = viewSize.getHeight();
final float displayWidth = drawRectF.width();
final float displayHeight = drawRectF.height();
if (viewWidth <= 0 || viewHeight <= 0 || displayWidth == 0 || displayHeight == 0) {
if (SLog.isLoggable(SLog.LEVEL_DEBUG | SLog.TYPE_ZOOM)) {
SLog.d(ImageZoomer.NAME, "size is 0. drawScrollBar. viewSize=%dx%d, displaySize=%sx%s",
viewWidth, viewHeight, displayWidth, displayHeight);
}
return;
}
ImageView imageView = imageZoomer.getImageView();
final int finalViewWidth = viewWidth - (scrollBarMargin * 2);
final int finalViewHeight = viewHeight - (scrollBarMargin * 2);
if ((int) displayWidth > viewWidth) {
final float widthScale = (float) viewWidth / displayWidth;
final int horScrollBarWidth = (int) (finalViewWidth * widthScale);
RectF horScrollBarRectF = scrollBarRectF;
horScrollBarRectF.setEmpty();
horScrollBarRectF.left = imageView.getPaddingLeft() + scrollBarMargin + (drawRectF.left < 0 ? (int) ((Math.abs(drawRectF.left) / drawRectF.width()) * finalViewWidth) : 0);
horScrollBarRectF.top = imageView.getPaddingTop() + scrollBarMargin + finalViewHeight - scrollBarSize;
horScrollBarRectF.right = horScrollBarRectF.left + horScrollBarWidth;
horScrollBarRectF.bottom = horScrollBarRectF.top + scrollBarSize;
canvas.drawRoundRect(horScrollBarRectF, scrollBarRadius, scrollBarRadius, scrollBarPaint);
}
if ((int) displayHeight > viewHeight) {
final float heightScale = (float) viewHeight / displayHeight;
final int verScrollBarHeight = (int) (finalViewHeight * heightScale);
RectF verScrollBarRectF = scrollBarRectF;
verScrollBarRectF.setEmpty();
verScrollBarRectF.left = imageView.getPaddingLeft() + scrollBarMargin + finalViewWidth - scrollBarSize;
verScrollBarRectF.top = imageView.getPaddingTop() + scrollBarMargin + (drawRectF.top < 0 ? (int) ((Math.abs(drawRectF.top) / drawRectF.height()) * finalViewHeight) : 0);
verScrollBarRectF.right = verScrollBarRectF.left + scrollBarSize;
verScrollBarRectF.bottom = verScrollBarRectF.top + verScrollBarHeight;
canvas.drawRoundRect(verScrollBarRectF, scrollBarRadius, scrollBarRadius, scrollBarPaint);
}
}