Example usage for android.graphics Rect set

List of usage examples for android.graphics Rect set

Introduction

In this page you can find the example usage for android.graphics Rect set.

Prototype

public void set(int left, int top, int right, int bottom) 

Source Link

Document

Set the rectangle's coordinates to the specified values.

Usage

From source file:android.support.v7.internal.widget.ListViewCompat.java

protected void positionSelectorCompat(int position, View sel) {
    final Rect selectorRect = mSelectorRect;
    selectorRect.set(sel.getLeft(), sel.getTop(), sel.getRight(), sel.getBottom());

    // Adjust for selection padding.
    selectorRect.left -= mSelectionLeftPadding;
    selectorRect.top -= mSelectionTopPadding;
    selectorRect.right += mSelectionRightPadding;
    selectorRect.bottom += mSelectionBottomPadding;

    try {/*from w w  w  .ja va  2 s.c  o  m*/
        // AbsListView.mIsChildViewEnabled controls the selector's state so we need to
        // modify it's value
        final boolean isChildViewEnabled = mIsChildViewEnabled.getBoolean(this);
        if (sel.isEnabled() != isChildViewEnabled) {
            mIsChildViewEnabled.set(this, !isChildViewEnabled);
            if (position != INVALID_POSITION) {
                refreshDrawableState();
            }
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

From source file:com.mark.app.widget.recycler_view.LLMDivider.java

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int childLayoutPosition = parent.getChildAdapterPosition(view);
    if (!mEnableLastItemDivider && childLayoutPosition == parent.getAdapter().getItemCount() - 1) {
        outRect.set(0, 0, 0, 0);
        return;/*from  w  w  w .  jav  a 2s.c  o  m*/
    }
    if (mOrientation == VERTICAL_LIST) {
        outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    } else {
        outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
    }
}

From source file:me.xiazdong.recyclerviewdemo.demo1.DividerItemDecoration.java

/**
 * divider?outRect/*from w  w w .  ja  v a2  s .  com*/
 * RecyclerView?child?
 */
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (mOrientation == VERTICAL_LIST) {
        int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        int count = parent.getAdapter().getItemCount();
        if (position < count - 1) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        }
    } else {
        outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
    }
}

From source file:com.example.aqing.materialdesign.deleteitem.DividerItemDecoration.java

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    if (mOrientation == VERTICAL_LIST) {
        if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1) {
            outRect.set(0, 0, 0, dividerHeight);
        } else {//from w  ww  . j  a  v a  2 s .  co  m
            outRect.set(0, 0, 0, 0);
        }
    } else {
        if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1) {
            outRect.set(0, 0, dividerHeight, 0);
        } else {
            outRect.set(0, 0, 0, 0);
        }
    }

}

From source file:com.bilibili.magicasakura.utils.GradientDrawableUtils.java

@Override
protected Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs)
        throws XmlPullParserException, IOException {
    GradientDrawable gradientDrawable = new GradientDrawable();
    inflateGradientRootElement(context, attrs, gradientDrawable);

    int type;//  w  ww. jav a  2  s. co m
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }

        if (depth > innerDepth) {
            continue;
        }

        String name = parser.getName();

        if (name.equals("size")) {
            final int width = getAttrDimensionPixelSize(context, attrs, android.R.attr.width);
            final int height = getAttrDimensionPixelSize(context, attrs, android.R.attr.height);
            gradientDrawable.setSize(width, height);
        } else if (name.equals("gradient")
                && Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
            final float centerX = getAttrFloatOrFraction(context, attrs, android.R.attr.centerX, 0.5f, 1.0f,
                    1.0f);
            final float centerY = getAttrFloatOrFraction(context, attrs, android.R.attr.centerY, 0.5f, 1.0f,
                    1.0f);
            gradientDrawable.setGradientCenter(centerX, centerY);
            final boolean useLevel = getAttrBoolean(context, attrs, android.R.attr.useLevel, false);
            gradientDrawable.setUseLevel(useLevel);
            final int gradientType = getAttrInt(context, attrs, android.R.attr.type, 0);
            gradientDrawable.setGradientType(gradientType);
            final int startColor = getAttrColor(context, attrs, android.R.attr.startColor, Color.TRANSPARENT);
            final int centerColor = getAttrColor(context, attrs, android.R.attr.centerColor, Color.TRANSPARENT);
            final int endColor = getAttrColor(context, attrs, android.R.attr.endColor, Color.TRANSPARENT);
            if (!getAttrHasValue(context, attrs, android.R.attr.centerColor)) {
                gradientDrawable.setColors(new int[] { startColor, endColor });
            } else {
                gradientDrawable.setColors(new int[] { startColor, centerColor, endColor });
                setStGradientPositions(gradientDrawable.getConstantState(), 0.0f,
                        centerX != 0.5f ? centerX : centerY, 1f);
            }

            if (gradientType == GradientDrawable.LINEAR_GRADIENT) {
                int angle = (int) getAttrFloat(context, attrs, android.R.attr.angle, 0.0f);
                angle %= 360;

                if (angle % 45 != 0) {
                    throw new XmlPullParserException(
                            "<gradient> tag requires" + "'angle' attribute to " + "be a multiple of 45");
                }

                setStGradientAngle(gradientDrawable.getConstantState(), angle);

                switch (angle) {
                case 0:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT);
                    break;
                case 45:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BL_TR);
                    break;
                case 90:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BOTTOM_TOP);
                    break;
                case 135:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BR_TL);
                    break;
                case 180:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.RIGHT_LEFT);
                    break;
                case 225:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TR_BL);
                    break;
                case 270:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TOP_BOTTOM);
                    break;
                case 315:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TL_BR);
                    break;
                }
            } else {
                setGradientRadius(context, attrs, gradientDrawable, gradientType);
            }
        } else if (name.equals("solid")) {
            int color = getAttrColor(context, attrs, android.R.attr.color, Color.TRANSPARENT);
            gradientDrawable
                    .setColor(getAlphaColor(color, getAttrFloat(context, attrs, android.R.attr.alpha, 1.0f)));
        } else if (name.equals("stroke")) {
            final float alphaMod = getAttrFloat(context, attrs, android.R.attr.alpha, 1.0f);
            final int strokeColor = getAttrColor(context, attrs, android.R.attr.color, Color.TRANSPARENT);
            final int strokeWidth = getAttrDimensionPixelSize(context, attrs, android.R.attr.width);
            final float dashWidth = getAttrDimension(context, attrs, android.R.attr.dashWidth);
            if (dashWidth != 0.0f) {
                final float dashGap = getAttrDimension(context, attrs, android.R.attr.dashGap);
                gradientDrawable.setStroke(strokeWidth, getAlphaColor(strokeColor, alphaMod), dashWidth,
                        dashGap);
            } else {
                gradientDrawable.setStroke(strokeWidth, getAlphaColor(strokeColor, alphaMod));
            }
        } else if (name.equals("corners")) {
            final int radius = getAttrDimensionPixelSize(context, attrs, android.R.attr.radius);
            gradientDrawable.setCornerRadius(radius);

            final int topLeftRadius = getAttrDimensionPixelSize(context, attrs, android.R.attr.topLeftRadius,
                    radius);
            final int topRightRadius = getAttrDimensionPixelSize(context, attrs, android.R.attr.topRightRadius,
                    radius);
            final int bottomLeftRadius = getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.bottomLeftRadius, radius);
            final int bottomRightRadius = getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.bottomRightRadius, radius);
            if (topLeftRadius != radius || topRightRadius != radius || bottomLeftRadius != radius
                    || bottomRightRadius != radius) {
                // The corner radii are specified in clockwise order (see Path.addRoundRect())
                gradientDrawable.setCornerRadii(
                        new float[] { topLeftRadius, topLeftRadius, topRightRadius, topRightRadius,
                                bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius });
            }
        } else if (name.equals("padding")) {
            final int paddingLeft = getAttrDimensionPixelOffset(context, attrs, android.R.attr.left);
            final int paddingTop = getAttrDimensionPixelOffset(context, attrs, android.R.attr.top);
            final int paddingRight = getAttrDimensionPixelOffset(context, attrs, android.R.attr.right);
            final int paddingBottom = getAttrDimensionPixelOffset(context, attrs, android.R.attr.bottom);
            if (paddingLeft != 0 || paddingTop != 0 || paddingRight != 0 || paddingBottom != 0) {
                final Rect pad = new Rect();
                pad.set(paddingLeft, paddingTop, paddingRight, paddingBottom);
                try {
                    if (sPaddingField == null) {
                        sPaddingField = GradientDrawable.class.getDeclaredField("mPadding");
                        sPaddingField.setAccessible(true);
                    }
                    sPaddingField.set(gradientDrawable, pad);
                    if (sStPaddingField == null) {
                        sStPaddingField = Class
                                .forName("android.graphics.drawable.GradientDrawable$GradientState")
                                .getDeclaredField("mPadding");
                        sStPaddingField.setAccessible(true);
                    }
                    sStPaddingField.set(gradientDrawable.getConstantState(), pad);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        } else {
            Log.w("drawable", "Bad element under <shape>: " + name);
        }
    }
    return gradientDrawable;
}

From source file:com.mad.splitlist.util.DividerItemDecoration.java

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

    // Position in the list.
    final int position = params.getViewAdapterPosition();

    // Add space for the separator except the last.
    if (position < state.getItemCount()) {
        outRect.set(0, 0, 0, (int) mPaint.getStrokeWidth());
    } else {//from   w  ww .  ja v a2 s . c o m
        outRect.setEmpty();
    }
}

From source file:com.xycode.xylibrary.uiKit.recyclerview.FloatingBarItemDecoration.java

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewAdapterPosition();
    outRect.set(0, headerList.containsKey(position) ? mTitleHeight : 0, 0, 0);
}

From source file:Main.java

public static Rect getBitmapRectFromImageView(ImageView imageView) {
    Drawable drawable = imageView.getDrawable();
    Bitmap bitmap = null;//from   w  w  w .j a  va 2 s.co m
    if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    }

    Rect rect = new Rect();
    boolean isVisible = imageView.getGlobalVisibleRect(rect);
    if (!isVisible) {
        int[] location = new int[2];
        imageView.getLocationOnScreen(location);

        rect.left = location[0];
        rect.top = location[1];
        rect.right = rect.left + imageView.getWidth();
        rect.bottom = rect.top + imageView.getHeight();
    }

    if (bitmap != null) {

        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();

        int imageViewWidth = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
        int imageviewHeight = imageView.getHeight() - imageView.getPaddingTop() - imageView.getPaddingBottom();

        float startScale;
        if ((float) imageViewWidth / bitmapWidth > (float) imageviewHeight / bitmapHeight) {
            // Extend start bounds horizontally
            startScale = (float) imageviewHeight / bitmapHeight;
        } else {
            startScale = (float) imageViewWidth / bitmapWidth;
        }

        bitmapHeight = (int) (bitmapHeight * startScale);
        bitmapWidth = (int) (bitmapWidth * startScale);

        int deltaX = (imageViewWidth - bitmapWidth) / 2;
        int deltaY = (imageviewHeight - bitmapHeight) / 2;

        rect.set(rect.left + deltaX, rect.top + deltaY, rect.right - deltaX, rect.bottom - deltaY);

        return rect;
    } else {
        return null;
    }
}

From source file:com.bilibili.magicasakura.utils.GradientDrawableInflateImpl.java

@Override
public Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs)
        throws XmlPullParserException, IOException {
    GradientDrawable gradientDrawable = new GradientDrawable();
    inflateGradientRootElement(context, attrs, gradientDrawable);

    int type;//from   ww w.  j  ava 2s  .c  om
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }

        if (depth > innerDepth) {
            continue;
        }

        String name = parser.getName();

        if (name.equals("size")) {
            final int width = DrawableUtils.getAttrDimensionPixelSize(context, attrs, android.R.attr.width);
            final int height = DrawableUtils.getAttrDimensionPixelSize(context, attrs, android.R.attr.height);
            gradientDrawable.setSize(width, height);
        } else if (name.equals("gradient")
                && Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
            final float centerX = getAttrFloatOrFraction(context, attrs, android.R.attr.centerX, 0.5f, 1.0f,
                    1.0f);
            final float centerY = getAttrFloatOrFraction(context, attrs, android.R.attr.centerY, 0.5f, 1.0f,
                    1.0f);
            gradientDrawable.setGradientCenter(centerX, centerY);
            final boolean useLevel = DrawableUtils.getAttrBoolean(context, attrs, android.R.attr.useLevel,
                    false);
            gradientDrawable.setUseLevel(useLevel);
            final int gradientType = DrawableUtils.getAttrInt(context, attrs, android.R.attr.type, 0);
            gradientDrawable.setGradientType(gradientType);
            final int startColor = DrawableUtils.getAttrColor(context, attrs, android.R.attr.startColor,
                    Color.TRANSPARENT);
            final int centerColor = DrawableUtils.getAttrColor(context, attrs, android.R.attr.centerColor,
                    Color.TRANSPARENT);
            final int endColor = DrawableUtils.getAttrColor(context, attrs, android.R.attr.endColor,
                    Color.TRANSPARENT);
            if (!DrawableUtils.getAttrHasValue(context, attrs, android.R.attr.centerColor)) {
                gradientDrawable.setColors(new int[] { startColor, endColor });
            } else {
                gradientDrawable.setColors(new int[] { startColor, centerColor, endColor });
                setStGradientPositions(gradientDrawable.getConstantState(), 0.0f,
                        centerX != 0.5f ? centerX : centerY, 1f);
            }

            if (gradientType == GradientDrawable.LINEAR_GRADIENT) {
                int angle = (int) DrawableUtils.getAttrFloat(context, attrs, android.R.attr.angle, 0.0f);
                angle %= 360;

                if (angle % 45 != 0) {
                    throw new XmlPullParserException(
                            "<gradient> tag requires" + "'angle' attribute to " + "be a multiple of 45");
                }

                setStGradientAngle(gradientDrawable.getConstantState(), angle);

                switch (angle) {
                case 0:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT);
                    break;
                case 45:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BL_TR);
                    break;
                case 90:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BOTTOM_TOP);
                    break;
                case 135:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BR_TL);
                    break;
                case 180:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.RIGHT_LEFT);
                    break;
                case 225:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TR_BL);
                    break;
                case 270:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TOP_BOTTOM);
                    break;
                case 315:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TL_BR);
                    break;
                }
            } else {
                setGradientRadius(context, attrs, gradientDrawable, gradientType);
            }
        } else if (name.equals("solid")) {
            int color = DrawableUtils.getAttrColor(context, attrs, android.R.attr.color, Color.TRANSPARENT);
            gradientDrawable.setColor(getAlphaColor(color,
                    DrawableUtils.getAttrFloat(context, attrs, android.R.attr.alpha, 1.0f)));
        } else if (name.equals("stroke")) {
            final float alphaMod = DrawableUtils.getAttrFloat(context, attrs, android.R.attr.alpha, 1.0f);
            final int strokeColor = DrawableUtils.getAttrColor(context, attrs, android.R.attr.color,
                    Color.TRANSPARENT);
            final int strokeWidth = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.width);
            final float dashWidth = DrawableUtils.getAttrDimension(context, attrs, android.R.attr.dashWidth);
            if (dashWidth != 0.0f) {
                final float dashGap = DrawableUtils.getAttrDimension(context, attrs, android.R.attr.dashGap);
                gradientDrawable.setStroke(strokeWidth, getAlphaColor(strokeColor, alphaMod), dashWidth,
                        dashGap);
            } else {
                gradientDrawable.setStroke(strokeWidth, getAlphaColor(strokeColor, alphaMod));
            }
        } else if (name.equals("corners")) {
            final int radius = DrawableUtils.getAttrDimensionPixelSize(context, attrs, android.R.attr.radius);
            gradientDrawable.setCornerRadius(radius);

            final int topLeftRadius = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.topLeftRadius, radius);
            final int topRightRadius = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.topRightRadius, radius);
            final int bottomLeftRadius = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.bottomLeftRadius, radius);
            final int bottomRightRadius = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.bottomRightRadius, radius);
            if (topLeftRadius != radius || topRightRadius != radius || bottomLeftRadius != radius
                    || bottomRightRadius != radius) {
                // The corner radii are specified in clockwise order (see Path.addRoundRect())
                gradientDrawable.setCornerRadii(
                        new float[] { topLeftRadius, topLeftRadius, topRightRadius, topRightRadius,
                                bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius });
            }
        } else if (name.equals("padding")) {
            final int paddingLeft = DrawableUtils.getAttrDimensionPixelOffset(context, attrs,
                    android.R.attr.left);
            final int paddingTop = DrawableUtils.getAttrDimensionPixelOffset(context, attrs,
                    android.R.attr.top);
            final int paddingRight = DrawableUtils.getAttrDimensionPixelOffset(context, attrs,
                    android.R.attr.right);
            final int paddingBottom = DrawableUtils.getAttrDimensionPixelOffset(context, attrs,
                    android.R.attr.bottom);
            if (paddingLeft != 0 || paddingTop != 0 || paddingRight != 0 || paddingBottom != 0) {
                final Rect pad = new Rect();
                pad.set(paddingLeft, paddingTop, paddingRight, paddingBottom);
                try {
                    if (sPaddingField == null) {
                        sPaddingField = GradientDrawable.class.getDeclaredField("mPadding");
                        sPaddingField.setAccessible(true);
                    }
                    sPaddingField.set(gradientDrawable, pad);
                    if (sStPaddingField == null) {
                        sStPaddingField = Class
                                .forName("android.graphics.drawable.GradientDrawable$GradientState")
                                .getDeclaredField("mPadding");
                        sStPaddingField.setAccessible(true);
                    }
                    sStPaddingField.set(gradientDrawable.getConstantState(), pad);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        } else {
            Log.w("drawable", "Bad element under <shape>: " + name);
        }
    }
    return gradientDrawable;
}

From source file:com.merlin.view.recycler.RecyclerListDecoration.java

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (parent.getAdapter().getItemCount() == mOffsetStart + mOffsetEnd) {
        //?outRect.set(0, 0, 0, 0)?
        if (mOrientation == GridLayoutManager.HORIZONTAL) {
            outRect.set(0, 0, 1, 0);
        } else {//from   w w w  .j  a va2s.com
            outRect.set(0, 0, 0, 1);
        }
        return;
    }
    if (mOrientation == LinearLayoutManager.HORIZONTAL) {
        if (isHideDivider(parent, parent.getChildLayoutPosition(view), parent.getAdapter().getItemCount())) {
            outRect.set(0, 0, 0, 0);
        } else {
            outRect.set(0, 0, mHeight, 0);
        }
    } else if (mOrientation == LinearLayoutManager.VERTICAL) {
        if (isHideDivider(parent, parent.getChildLayoutPosition(view), parent.getAdapter().getItemCount())) {
            outRect.set(0, 0, 0, 0);
        } else {
            outRect.set(0, 0, 0, mHeight);
        }
    }
}