下面列出了android.graphics.Rect#set ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void ensureSrcRect(Bitmap bitmap, Rect src, Canvas canvas) {
if (1.0 * bitmap.getWidth() / bitmap.getHeight()
> 1.0 * canvas.getWidth() / canvas.getHeight()) {
// landscape.
src.set(0,
0,
(int) (bitmap.getHeight() * 1.0 * canvas.getWidth() / canvas.getHeight()),
bitmap.getHeight());
src.right = Math.min(src.right, bitmap.getWidth());
src.offset((bitmap.getWidth() - src.width()) / 2, 0);
src.offset((int) ((bitmap.getWidth() - src.width()) / 2 * horizontalOffset), 0);
} else {
// portrait.
src.set(0,
0,
bitmap.getWidth(),
(int) (bitmap.getWidth() * 1.0 * canvas.getHeight() / canvas.getWidth()));
src.bottom = Math.min(src.bottom, bitmap.getHeight());
src.offset(0, (bitmap.getHeight() - src.height()) / 2);
}
}
@Nullable
@Override
public Object getElementToHighlightAtPosition(ViewGroup element, int x, int y, Rect bounds) {
View hitChild = null;
for (int i = element.getChildCount() - 1; i >= 0; --i) {
final View childView = element.getChildAt(i);
if (isChildVisible(childView) &&
childView.getVisibility() == View.VISIBLE) {
childView.getHitRect(bounds);
if (bounds.contains(x, y)) {
hitChild = childView;
break;
}
}
}
if (hitChild != null) {
return hitChild;
} else {
bounds.set(0, 0, element.getWidth(), element.getHeight());
return element;
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mWidth = w;
mHeight = h;
int perWidth = mWidth/getPerLineCount();
int perHeight = mHeight/getLineCount();
for(int i=0;i<getLineCount();i++){
for(int j=0;j<getPerLineCount();j++){
Rect rect = new Rect();
int l = perWidth * j;
int t = perHeight * i;
rect.set(l,t,l+perWidth,t+perHeight);
mRectList.add(rect);
}
}
}
private void updateRect(){
if(mWidth > 0 && mHeight > 0) {
mRectList.clear();
int perWidth = mWidth / mColumnCount;
int perHeight = mHeight / mLineCount;
for (int i = 0; i < mLineCount; i++) {
for (int j = 0; j < mColumnCount; j++) {
Rect rect = new Rect();
int l = perWidth * j;
int t = perHeight * i;
rect.set(l, t, l + perWidth, t + perHeight);
mRectList.add(rect);
}
}
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
/**
* Find the area of the source file that is currently visible on screen, taking into account the
* current scale, translation, orientation and clipped region. This is a convenience method; see
* {@link #viewToFileRect(Rect, Rect)}.
* @param fRect rectangle instance to which the result will be written. Re-use for efficiency.
*/
public void visibleFileRect(Rect fRect) {
if (vTranslate == null || !readySent) {
return;
}
fRect.set(0, 0, getWidth(), getHeight());
viewToFileRect(fRect, fRect);
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
@Override
public void getLocationBarContentRect(Rect outRect) {
View container = getLocationBar().getContainerView();
outRect.set(container.getPaddingLeft(), container.getPaddingTop(),
container.getWidth() - container.getPaddingRight(),
container.getHeight() - container.getPaddingBottom());
ViewUtils.getRelativeDrawPosition(
this, getLocationBar().getContainerView(), mTempPosition);
outRect.offset(mTempPosition[0], mTempPosition[1]);
}
/**
* Returns the maximum bounds of the ripple relative to the ripple center.
*/
public void getBounds(Rect bounds) {
final int outerX = (int) mOuterX;
final int outerY = (int) mOuterY;
final int r = (int) mOuterRadius + 1;
bounds.set(outerX - r, outerY - r, outerX + r, outerY + r);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
int marginRight = OkUtils.dp2px(parent.getContext(), 7);
if (position == 1 | position == 3) {
outRect.set(0, 0, marginRight, 0);
}
}
@Override
public void getItemOffsets(@NonNull Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
/**
* Draw the View v into the given Canvas.
*
* @param v the view to draw
* @param destCanvas the canvas to draw on
* @param padding the horizontal and vertical padding to use when drawing
*/
private static void drawDragView(View v, Canvas destCanvas, int padding) {
final Rect clipRect = sTempRect;
v.getDrawingRect(clipRect);
boolean textVisible = false;
destCanvas.save();
if (v instanceof TextView) {
Drawable d = getTextViewIcon((TextView) v);
Rect bounds = getDrawableBounds(d);
clipRect.set(0, 0, bounds.width() + padding, bounds.height() + padding);
destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top);
d.draw(destCanvas);
} else {
if (v instanceof FolderIcon) {
// For FolderIcons the text can bleed into the icon area, and so we need to
// hide the text completely (which can't be achieved by clipping).
if (((FolderIcon) v).getTextVisible()) {
((FolderIcon) v).setTextVisible(false);
textVisible = true;
}
}
destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
destCanvas.clipRect(clipRect, Op.REPLACE);
v.draw(destCanvas);
// Restore text visibility of FolderIcon if necessary
if (textVisible) {
((FolderIcon) v).setTextVisible(true);
}
}
destCanvas.restore();
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(0, 0, 0, mDividerHeight);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set(0,0,0,drawable.getIntrinsicHeight());
}
/**
* @param outRect Populated with a {@link Rect} that represents the {@link Tab} specific content
* of this {@link LocationBar}.
*/
public void getContentRect(Rect outRect) {
outRect.set(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(),
getHeight() - getPaddingBottom());
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set(0, 0, 0, divider.getIntrinsicHeight());
}
/**
* Calculates the screen rect for an item.
*
* @param item
* @param coords
* @param reuse
* @return
*/
protected Rect calculateItemRect(Item item, Point coords, Rect reuse) {
final Rect out = reuse != null ? reuse : new Rect();
HotspotPlace hotspot = item.getMarkerHotspot();
if (hotspot == null) {
hotspot = HotspotPlace.BOTTOM_CENTER;
}
final int state = (mDrawFocusedItem && (mFocusedItem == item) ? OverlayItem.ITEM_STATE_FOCUSED_MASK : 0);
final Drawable marker = (item.getMarker(state) == null) ? getDefaultMarker(state) : item.getMarker(state);
int itemWidth = marker.getIntrinsicWidth();
int itemHeight = marker.getIntrinsicHeight();
switch (hotspot) {
case NONE:
out.set(coords.x - itemWidth / 2,
coords.y - itemHeight / 2,
coords.x + itemWidth / 2,
coords.y + itemHeight / 2);
break;
case CENTER:
out.set(coords.x - itemWidth / 2,
coords.y - itemHeight / 2,
coords.x + itemWidth / 2,
coords.y + itemHeight / 2);
break;
case BOTTOM_CENTER:
out.set(coords.x - itemWidth / 2,
coords.y - itemHeight,
coords.x + itemWidth / 2,
coords.y);
break;
case TOP_CENTER:
out.set(coords.x - itemWidth / 2,
coords.y,
coords.x + itemWidth / 2,
coords.y + itemHeight);
break;
case RIGHT_CENTER:
out.set(coords.x - itemWidth,
coords.y - itemHeight / 2,
coords.x ,
coords.y + itemHeight / 2);
break;
case LEFT_CENTER:
out.set(coords.x,
coords.y - itemHeight / 2,
coords.x + itemWidth,
coords.y + itemHeight / 2);
break;
case UPPER_RIGHT_CORNER:
out.set(coords.x - itemWidth,
coords.y,
coords.x ,
coords.y + itemHeight);
break;
case LOWER_RIGHT_CORNER:
out.set(coords.x - itemWidth,
coords.y - itemHeight,
coords.x,
coords.y);
break;
case UPPER_LEFT_CORNER:
out.set(coords.x ,
coords.y,
coords.x + itemWidth,
coords.y + itemHeight);
break;
case LOWER_LEFT_CORNER:
out.set(coords.x ,
coords.y - itemHeight,
coords.x + itemWidth,
coords.y);
break;
}
return out;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set(insets, insets, insets, insets);
}
/**
* 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]);
}
/**
* Retrieve the transformed bounding rect of an arbitrary descendant view.
* This does not need to be a direct child.
*
* @param descendant descendant view to reference
* @param out rect to set to the bounds of the descendant view
*/
static void getDescendantRect(ViewGroup parent, View descendant, Rect out) {
out.set(0, 0, descendant.getWidth(), descendant.getHeight());
offsetDescendantRect(parent, descendant, out);
}