Example usage for android.graphics Canvas drawRect

List of usage examples for android.graphics Canvas drawRect

Introduction

In this page you can find the example usage for android.graphics Canvas drawRect.

Prototype

public void drawRect(@NonNull Rect r, @NonNull Paint paint) 

Source Link

Document

Draw the specified Rect using the specified Paint.

Usage

From source file:com.appunite.list.HorizontalListView.java

@Override
protected void dispatchDraw(Canvas canvas) {
    if (mCachingStarted) {
        mCachingActive = true;/*from   w  ww . jav  a  2s  .c  om*/
    }

    // Draw the dividers
    final int dividerWidth = mDividerWidth;
    final Drawable overscrollHeader = mOverScrollHeader;
    final Drawable overscrollFooter = mOverScrollFooter;
    final boolean drawOverscrollHeader = overscrollHeader != null;
    final boolean drawOverscrollFooter = overscrollFooter != null;
    final boolean drawDividers = dividerWidth > 0 && mDivider != null;

    if (drawDividers || drawOverscrollHeader || drawOverscrollFooter) {
        // Only modify the top and bottom in the loop, we set the left and right here
        final Rect bounds = mTempRect;
        bounds.top = getPaddingTop();
        bounds.bottom = getBottom() - getTop() - getPaddingBottom();

        final int count = getChildCount();
        final int headerCount = mHeaderViewInfos.size();
        final int itemCount = mItemCount;
        final int footerLimit = itemCount - mFooterViewInfos.size() - 1;
        final boolean headerDividers = mHeaderDividersEnabled;
        final boolean footerDividers = mFooterDividersEnabled;
        final int first = mFirstPosition;
        final boolean areAllItemsSelectable = mAreAllItemsSelectable;
        final ListAdapter adapter = mAdapter;
        // If the list is opaque *and* the background is not, we want to
        // fill a rect where the dividers would be for non-selectable items
        // If the list is opaque and the background is also opaque, we don't
        // need to draw anything since the background will do it for us
        final boolean fillForMissingDividers = isOpaque() && !super.isOpaque();

        if (fillForMissingDividers && mDividerPaint == null && mIsCacheColorOpaque) {
            mDividerPaint = new Paint();
            mDividerPaint.setColor(getCacheColorHint());
        }
        final Paint paint = mDividerPaint;

        int effectivePaddingLeft = 0;
        int effectivePaddingRight = 0;
        if (mClipToPadding) {
            effectivePaddingLeft = mListPadding.left;
            effectivePaddingRight = mListPadding.right;
        }

        final int viewLeft = getLeft();
        final int viewRight = getRight();
        final int scrollX = getScrollX();

        final int listRight = viewLeft - viewRight - effectivePaddingRight + scrollX;
        if (!mStackFromRight) {
            int right = 0;

            // Draw top divider or header for overscroll
            if (count > 0 && scrollX < 0) {
                if (drawOverscrollHeader) {
                    bounds.right = 0;
                    bounds.left = scrollX;
                    drawOverscrollHeader(canvas, overscrollHeader, bounds);
                } else if (drawDividers) {
                    bounds.right = 0;
                    bounds.left = -dividerWidth;
                    drawDivider(canvas, bounds, -1);
                }
            }

            for (int i = 0; i < count; i++) {
                if ((headerDividers || first + i >= headerCount)
                        && (footerDividers || first + i < footerLimit)) {
                    View child = getChildAt(i);
                    right = child.getRight();
                    // Don't draw dividers next to items that are not enabled

                    if (drawDividers && (right < listRight && !(drawOverscrollFooter && i == count - 1))) {
                        if ((areAllItemsSelectable || (adapter.isEnabled(first + i)
                                && (i == count - 1 || adapter.isEnabled(first + i + 1))))) {
                            bounds.left = right;
                            bounds.right = right + dividerWidth;
                            drawDivider(canvas, bounds, i);
                        } else if (fillForMissingDividers) {
                            bounds.left = right;
                            bounds.right = right + dividerWidth;
                            canvas.drawRect(bounds, paint);
                        }
                    }
                }
            }

            final int overFooterRight = viewLeft + scrollX;
            if (drawOverscrollFooter && first + count == itemCount && overFooterRight > right) {
                bounds.left = right;
                bounds.right = overFooterRight;
                drawOverscrollFooter(canvas, overscrollFooter, bounds);
            }
        } else {
            int left;

            if (count > 0 && drawOverscrollHeader) {
                bounds.left = scrollX;
                bounds.right = getChildAt(0).getLeft();
                drawOverscrollHeader(canvas, overscrollHeader, bounds);
            }

            final int start = drawOverscrollHeader ? 1 : 0;
            for (int i = start; i < count; i++) {
                if ((headerDividers || first + i >= headerCount)
                        && (footerDividers || first + i < footerLimit)) {
                    View child = getChildAt(i);
                    left = child.getLeft();
                    // Don't draw dividers next to items that are not enabled
                    if (left > effectivePaddingLeft) {
                        if ((areAllItemsSelectable || (adapter.isEnabled(first + i)
                                && (i == count - 1 || adapter.isEnabled(first + i + 1))))) {
                            bounds.left = left - dividerWidth;
                            bounds.right = left;
                            // Give the method the child ABOVE the divider, so we
                            // subtract one from our child
                            // position. Give -1 when there is no child above the
                            // divider.
                            drawDivider(canvas, bounds, i - 1);
                        } else if (fillForMissingDividers) {
                            bounds.left = left - dividerWidth;
                            bounds.right = left;
                            canvas.drawRect(bounds, paint);
                        }
                    }
                }
            }

            if (count > 0 && scrollX > 0) {
                if (drawOverscrollFooter) {
                    final int absListRight = viewLeft;
                    bounds.left = absListRight;
                    bounds.right = absListRight + scrollX;
                    drawOverscrollFooter(canvas, overscrollFooter, bounds);
                } else if (drawDividers) {
                    bounds.left = listRight;
                    bounds.right = listRight + dividerWidth;
                    drawDivider(canvas, bounds, -1);
                }
            }
        }
    }

    // Draw the indicators (these should be drawn above the dividers) and children
    super.dispatchDraw(canvas);
}

From source file:com.appunite.list.ListView.java

@Override
protected void dispatchDraw(Canvas canvas) {
    if (mCachingStarted) {
        mCachingActive = true;/*  ww  w. jav  a  2 s.com*/
    }

    // Draw the dividers
    final int dividerHeight = mDividerHeight;
    final Drawable overscrollHeader = mOverScrollHeader;
    final Drawable overscrollFooter = mOverScrollFooter;
    final boolean drawOverscrollHeader = overscrollHeader != null;
    final boolean drawOverscrollFooter = overscrollFooter != null;
    final boolean drawDividers = dividerHeight > 0 && mDivider != null;

    if (drawDividers || drawOverscrollHeader || drawOverscrollFooter) {
        // Only modify the top and bottom in the loop, we set the left and right here
        final Rect bounds = mTempRect;
        bounds.left = getPaddingLeft();
        bounds.right = getRight() - getLeft() - getPaddingRight();

        final int count = getChildCount();
        final int headerCount = mHeaderViewInfos.size();
        final int itemCount = mItemCount;
        final int footerLimit = itemCount - mFooterViewInfos.size() - 1;
        final boolean headerDividers = mHeaderDividersEnabled;
        final boolean footerDividers = mFooterDividersEnabled;
        final int first = mFirstPosition;
        final boolean areAllItemsSelectable = mAreAllItemsSelectable;
        final ListAdapter adapter = mAdapter;
        // If the list is opaque *and* the background is not, we want to
        // fill a rect where the dividers would be for non-selectable items
        // If the list is opaque and the background is also opaque, we don't
        // need to draw anything since the background will do it for us
        final boolean fillForMissingDividers = isOpaque() && !super.isOpaque();

        if (fillForMissingDividers && mDividerPaint == null && mIsCacheColorOpaque) {
            mDividerPaint = new Paint();
            mDividerPaint.setColor(getCacheColorHint());
        }
        final Paint paint = mDividerPaint;

        int effectivePaddingTop = 0;
        int effectivePaddingBottom = 0;
        if (mClipToPadding) {
            effectivePaddingTop = mListPadding.top;
            effectivePaddingBottom = mListPadding.bottom;
        }

        final int viewBottom = getBottom();
        final int viewTop = getTop();
        final int scrollY = getScrollY();

        final int listBottom = viewBottom - viewTop - effectivePaddingBottom + scrollY;
        if (!mStackFromBottom) {
            int bottom = 0;

            // Draw top divider or header for overscroll
            if (count > 0 && scrollY < 0) {
                if (drawOverscrollHeader) {
                    bounds.bottom = 0;
                    bounds.top = scrollY;
                    drawOverscrollHeader(canvas, overscrollHeader, bounds);
                } else if (drawDividers) {
                    bounds.bottom = 0;
                    bounds.top = -dividerHeight;
                    drawDivider(canvas, bounds, -1);
                }
            }

            for (int i = 0; i < count; i++) {
                if ((headerDividers || first + i >= headerCount)
                        && (footerDividers || first + i < footerLimit)) {
                    View child = getChildAt(i);
                    bottom = child.getBottom();
                    // Don't draw dividers next to items that are not enabled

                    if (drawDividers && (bottom < listBottom && !(drawOverscrollFooter && i == count - 1))) {
                        if ((areAllItemsSelectable || (adapter.isEnabled(first + i)
                                && (i == count - 1 || adapter.isEnabled(first + i + 1))))) {
                            bounds.top = bottom;
                            bounds.bottom = bottom + dividerHeight;
                            drawDivider(canvas, bounds, i);
                        } else if (fillForMissingDividers) {
                            bounds.top = bottom;
                            bounds.bottom = bottom + dividerHeight;
                            canvas.drawRect(bounds, paint);
                        }
                    }
                }
            }

            final int overFooterBottom = viewBottom + scrollY;
            if (drawOverscrollFooter && first + count == itemCount && overFooterBottom > bottom) {
                bounds.top = bottom;
                bounds.bottom = overFooterBottom;
                drawOverscrollFooter(canvas, overscrollFooter, bounds);
            }
        } else {
            int top;

            if (count > 0 && drawOverscrollHeader) {
                bounds.top = scrollY;
                bounds.bottom = getChildAt(0).getTop();
                drawOverscrollHeader(canvas, overscrollHeader, bounds);
            }

            final int start = drawOverscrollHeader ? 1 : 0;
            for (int i = start; i < count; i++) {
                if ((headerDividers || first + i >= headerCount)
                        && (footerDividers || first + i < footerLimit)) {
                    View child = getChildAt(i);
                    top = child.getTop();
                    // Don't draw dividers next to items that are not enabled
                    if (top > effectivePaddingTop) {
                        if ((areAllItemsSelectable || (adapter.isEnabled(first + i)
                                && (i == count - 1 || adapter.isEnabled(first + i + 1))))) {
                            bounds.top = top - dividerHeight;
                            bounds.bottom = top;
                            // Give the method the child ABOVE the divider, so we
                            // subtract one from our child
                            // position. Give -1 when there is no child above the
                            // divider.
                            drawDivider(canvas, bounds, i - 1);
                        } else if (fillForMissingDividers) {
                            bounds.top = top - dividerHeight;
                            bounds.bottom = top;
                            canvas.drawRect(bounds, paint);
                        }
                    }
                }
            }

            if (count > 0 && scrollY > 0) {
                if (drawOverscrollFooter) {
                    final int absListBottom = viewBottom;
                    bounds.top = absListBottom;
                    bounds.bottom = absListBottom + scrollY;
                    drawOverscrollFooter(canvas, overscrollFooter, bounds);
                } else if (drawDividers) {
                    bounds.top = listBottom;
                    bounds.bottom = listBottom + dividerHeight;
                    drawDivider(canvas, bounds, -1);
                }
            }
        }
    }

    // Draw the indicators (these should be drawn above the dividers) and children
    super.dispatchDraw(canvas);
}