Example usage for android.graphics Canvas drawPath

List of usage examples for android.graphics Canvas drawPath

Introduction

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

Prototype

public void drawPath(@NonNull Path path, @NonNull Paint paint) 

Source Link

Document

Draw the specified path using the specified paint.

Usage

From source file:me.futuretechnology.util.ui.TitlePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mViewPager == null) {
        return;//from  ww  w . j a  va  2s .  c o m
    }

    int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    // mCurrentPage is -1 on first start and after orientation changed.
    // if so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null) {
        mCurrentPage = mViewPager.getCurrentItem();
    }

    // calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    int boundsSize = bounds.size();

    // make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }

    int left = getLeft();
    float leftClip = left + mClipPadding;
    int width = getWidth();
    int height = getHeight();
    // float halfWidth = width / 2f;
    int right = left + width;
    float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    boolean currentSelected = offsetPercent <= SELECTION_FADE_PERCENTAGE;
    boolean currentBold = offsetPercent <= BOLD_FADE_PERCENTAGE;
    float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    // Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        // Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        // Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    // left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            // if left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                // try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                // except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                // intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    // right views starting from the current position
    if (mCurrentPage < count - 1) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            // if right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                // try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                // except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                // intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }

    // Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        // Get the title
        Rect bound = bounds.get(i);
        // Only if one side is visible
        if (bound.left > left && bound.left < right || bound.right > left && bound.right < right) {
            boolean currentPage = i == page;
            CharSequence pageTitle = getTitle(i);

            // Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

            // Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                // Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }

            // Except if there's an intersection with the right view
            if (i < boundsSize - 1) {
                Rect rightBound = bounds.get(i + 1);
                // Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    int w = bound.right - bound.left;
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }

            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                    mPaintText);

            // If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                        mPaintText);
            }
        }
    }

    // Draw the footer line
    if (mFooterLineHeight > 0) {
        mPath.reset();
        mPath.moveTo(0, height - mFooterLineHeight / 2f);
        mPath.lineTo(width, height - mFooterLineHeight / 2f);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterLine);
    }

    if (mFooterIndicatorHeight > 0) {
        if (!currentSelected || page >= boundsSize) {
            return;
        }

        Rect underlineBounds = bounds.get(page);
        float tempHeight = mFooterIndicatorHeight * selectedPercent
                - ((1 - selectedPercent) * mFooterIndicatorHeight) * 0.5f;

        mPath.reset();
        mPath.moveTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight);
        mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight);
        mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding,
                height - mFooterLineHeight - tempHeight);
        mPath.lineTo(underlineBounds.left - mFooterIndicatorUnderlinePadding,
                height - mFooterLineHeight - tempHeight);
        mPath.close();

        // mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
    }
}

From source file:org.telegram.ui.ArticleViewer.java

private void drawLayoutLink(Canvas canvas, StaticLayout layout) {
    if (canvas == null || pressedLink == null || pressedLinkOwnerLayout != layout) {
        return;//from  www. j a  v  a 2 s  .  co  m
    }
    if (pressedLink != null) {
        canvas.drawPath(urlPath, urlPaint);
    }
}

From source file:com.viewpagerindicator.RichTitlePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mViewPager == null) {
        return;//from   www .j  ava 2  s .  co  m
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    // mCurrentPage is -1 on first start and after orientation changed. If so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null)
        mCurrentPage = mViewPager.getCurrentItem();

    //Calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    final int boundsSize = bounds.size();

    //Make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }

    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    final int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    //Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }

    //Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        //Get the title
        Rect bound = bounds.get(i);
        //Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            final CharSequence pageTitle = getTitle(i);

            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

            //Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                //Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }
            //canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding, mPaintText);
            //drawItem(canvas, i, bound, mPaintText, currentPage && currentSelected);

            //If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                //canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding, mPaintText);
                drawItem(canvas, i, bound, mPaintText, true);
            } else {
                if (i == page - 1) {
                    drawLeftItem(canvas, i, bound, mPaintText, false);
                    //drawItem(canvas, i, bound, mPaintText, false);
                } else if (i == page + 1) {
                    drawRightItem(canvas, i, bound, mPaintText, false);
                } else {
                    drawItem(canvas, i, bound, mPaintText, false);
                }
            }
        }
    }

    //Draw the footer line
    mPath.reset();
    mPath.moveTo(0, height - mFooterLineHeight / 2f);
    mPath.lineTo(width, height - mFooterLineHeight / 2f);
    mPath.close();
    canvas.drawPath(mPath, mPaintFooterLine);

    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath.reset();
        mPath.moveTo(halfWidth, height - mFooterLineHeight - mFooterIndicatorHeight);
        mPath.lineTo(halfWidth + mFooterIndicatorHeight, height - mFooterLineHeight);
        mPath.lineTo(halfWidth - mFooterIndicatorHeight, height - mFooterLineHeight);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterIndicator);
        break;

    case Underline:
        if (!currentSelected || page >= boundsSize) {
            break;
        }

        Rect underlineBounds = bounds.get(page);
        mPath.reset();
        mPath.moveTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight);
        mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight);
        mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding,
                height - mFooterLineHeight - mFooterIndicatorHeight);
        mPath.lineTo(underlineBounds.left - mFooterIndicatorUnderlinePadding,
                height - mFooterLineHeight - mFooterIndicatorHeight);
        mPath.close();

        mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
        break;
    }
}

From source file:com.benefit.buy.library.viewpagerindicator.TitlePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mViewPager == null) {
        return;/* ww w.jav  a 2  s .c om*/
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }
    // mCurrentPage is -1 on first start and after orientation changed. If so, retrieve the correct index from viewpager.
    if ((mCurrentPage == -1) && (mViewPager != null)) {
        mCurrentPage = mViewPager.getCurrentItem();
    }
    //Calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    final int boundsSize = bounds.size();
    //Make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }
    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;
    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;
    //Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }
    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if ((bound.right + mTitlePadding) > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                //Intersection
                if ((bound.left - mTitlePadding) < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        //Get the title
        Rect bound = bounds.get(i);
        //Only if one side is visible
        if (((bound.left > left) && (bound.left < right)) || ((bound.right > left) && (bound.right < right))) {
            final boolean currentPage = (i == page);
            final CharSequence pageTitle = getTitle(i);
            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);
            //Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                //Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }
            //Except if there's an intersection with the right view
            if (i < (boundsSize - 1)) {
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if ((bound.right + mTitlePadding) > rightBound.left) {
                    int w = bound.right - bound.left;
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                    mPaintText);
            //If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                        mPaintText);
            }
        }
    }
    //If we want the line on the top change height to zero and invert the line height to trick the drawing code
    float footerLineHeight = mFooterLineHeight;
    float footerIndicatorLineHeight = mFooterIndicatorHeight;
    if (mLinePosition == LinePosition.Top) {
        height = 0;
        footerLineHeight = -footerLineHeight;
        footerIndicatorLineHeight = -footerIndicatorLineHeight;
    }
    //Draw the footer line
    mPath.reset();
    mPath.moveTo(0, height - (footerLineHeight / 2f));
    mPath.lineTo(width, height - (footerLineHeight / 2f));
    mPath.close();
    canvas.drawPath(mPath, mPaintFooterLine);
    float heightMinusLine = height - footerLineHeight;
    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath.reset();
        mPath.moveTo(halfWidth, heightMinusLine - footerIndicatorLineHeight);
        mPath.lineTo(halfWidth + footerIndicatorLineHeight, heightMinusLine);
        mPath.lineTo(halfWidth - footerIndicatorLineHeight, heightMinusLine);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterIndicator);
        break;
    case Underline:
        if (!currentSelected || (page >= boundsSize)) {
            break;
        }
        Rect underlineBounds = bounds.get(page);
        final float rightPlusPadding = underlineBounds.right + mFooterIndicatorUnderlinePadding;
        final float leftMinusPadding = underlineBounds.left - mFooterIndicatorUnderlinePadding;
        final float heightMinusLineMinusIndicator = heightMinusLine - footerIndicatorLineHeight;
        mPath.reset();
        mPath.moveTo(leftMinusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLineMinusIndicator);
        mPath.lineTo(leftMinusPadding, heightMinusLineMinusIndicator);
        mPath.close();
        mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
        break;
    }
}

From source file:com.lovebridge.library.view.viewpagerindicator.TitlePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mViewPager == null) {
        return;/*from  w  w  w. j  a  v  a2s.  c  om*/
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }
    // mCurrentPage is -1 on first start and after orientation changed. If
    // so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null) {
        mCurrentPage = mViewPager.getCurrentItem();
    }
    // Calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    final int boundsSize = bounds.size();
    // Make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }
    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;
    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;
    // Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        // Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        // Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }
    // Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            // Is left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                // Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                // Except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                // Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    // Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            // If right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                // Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                // Except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                // Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    // Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        // Get the title
        Rect bound = bounds.get(i);
        // Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            final CharSequence pageTitle = getTitle(i);
            // Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);
            // Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                // Fade out/in unselected text as the selected text fades
                // in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }
            // Except if there's an intersection with the right view
            if (i < boundsSize - 1) {
                Rect rightBound = bounds.get(i + 1);
                // Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    int w = bound.right - bound.left;
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                    mPaintText);
            // If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                        mPaintText);
            }
        }
    }
    // If we want the line on the top change height to zero and invert the
    // line height to trick the drawing code
    float footerLineHeight = mFooterLineHeight;
    float footerIndicatorLineHeight = mFooterIndicatorHeight;
    if (mLinePosition == LinePosition.Top) {
        height = 0;
        footerLineHeight = -footerLineHeight;
        footerIndicatorLineHeight = -footerIndicatorLineHeight;
    }
    // Draw the footer line
    mPath.reset();
    mPath.moveTo(0, height - footerLineHeight / 2f);
    mPath.lineTo(width, height - footerLineHeight / 2f);
    mPath.close();
    canvas.drawPath(mPath, mPaintFooterLine);
    float heightMinusLine = height - footerLineHeight;
    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath.reset();
        mPath.moveTo(halfWidth, heightMinusLine - footerIndicatorLineHeight);
        mPath.lineTo(halfWidth + footerIndicatorLineHeight, heightMinusLine);
        mPath.lineTo(halfWidth - footerIndicatorLineHeight, heightMinusLine);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterIndicator);
        break;
    case Underline:
        if (!currentSelected || page >= boundsSize) {
            break;
        }
        Rect underlineBounds = bounds.get(page);
        final float rightPlusPadding = underlineBounds.right + mFooterIndicatorUnderlinePadding;
        final float leftMinusPadding = underlineBounds.left - mFooterIndicatorUnderlinePadding;
        final float heightMinusLineMinusIndicator = heightMinusLine - footerIndicatorLineHeight;
        mPath.reset();
        mPath.moveTo(leftMinusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLineMinusIndicator);
        mPath.lineTo(leftMinusPadding, heightMinusLineMinusIndicator);
        mPath.close();
        mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
        break;
    }
}

From source file:android_hddl_framework.viewpagerindicator.TitlePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mViewPager == null) {
        return;/*  w w w  . ja v a  2 s.c o  m*/
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    // mCurrentPage is -1 on first start and after orientation changed. If so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null) {
        mCurrentPage = mViewPager.getCurrentItem();
    }

    //Calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    final int boundsSize = bounds.size();

    //Make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }

    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    //Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }

    //Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        //Get the title
        Rect bound = bounds.get(i);
        //Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            final CharSequence pageTitle = getTitle(i);

            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

            //Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                //Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }

            //Except if there's an intersection with the right view
            if (i < boundsSize - 1) {
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    int w = bound.right - bound.left;
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                    mPaintText);

            //If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                        mPaintText);
            }
        }
    }

    //If we want the line on the top change height to zero and invert the line height to trick the drawing code
    float footerLineHeight = mFooterLineHeight;
    float footerIndicatorLineHeight = mFooterIndicatorHeight;
    if (mLinePosition == LinePosition.Top) {
        height = 0;
        footerLineHeight = -footerLineHeight;
        footerIndicatorLineHeight = -footerIndicatorLineHeight;
    }

    //Draw the footer line
    mPath.reset();
    mPath.moveTo(0, height - footerLineHeight / 2f);
    mPath.lineTo(width, height - footerLineHeight / 2f);
    mPath.close();
    canvas.drawPath(mPath, mPaintFooterLine);

    float heightMinusLine = height - footerLineHeight;
    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath.reset();
        mPath.moveTo(halfWidth, heightMinusLine - footerIndicatorLineHeight);
        mPath.lineTo(halfWidth + footerIndicatorLineHeight, heightMinusLine);
        mPath.lineTo(halfWidth - footerIndicatorLineHeight, heightMinusLine);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterIndicator);
        break;

    case Underline:
        if (!currentSelected || page >= boundsSize) {
            break;
        }

        Rect underlineBounds = bounds.get(page);
        final float rightPlusPadding = underlineBounds.right + mFooterIndicatorUnderlinePadding;
        final float leftMinusPadding = underlineBounds.left - mFooterIndicatorUnderlinePadding;
        final float heightMinusLineMinusIndicator = heightMinusLine - footerIndicatorLineHeight;

        mPath.reset();
        mPath.moveTo(leftMinusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLineMinusIndicator);
        mPath.lineTo(leftMinusPadding, heightMinusLineMinusIndicator);
        mPath.close();

        mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
        break;
    }
}

From source file:com.jcertif.mdomotique.ihm.adabter.TitlePageIndicator.java

@SuppressWarnings("incomplete-switch")
@Override/*from  w  w w.  j  a  va 2  s . c  o m*/
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mViewPager == null) {
        return;
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    // mCurrentPage is -1 on first start and after orientation changed. If so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null) {
        mCurrentPage = mViewPager.getCurrentItem();
    }

    //Calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    final int boundsSize = bounds.size();

    //Make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }

    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    //Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }

    //Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        //Get the title
        Rect bound = bounds.get(i);
        //Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            final CharSequence pageTitle = getTitle(i);

            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

            //Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                //Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }

            //Except if there's an intersection with the right view
            if (i < boundsSize - 1) {
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    int w = bound.right - bound.left;
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                    mPaintText);

            //If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                        mPaintText);
            }
        }
    }

    //If we want the line on the top change height to zero and invert the line height to trick the drawing code
    float footerLineHeight = mFooterLineHeight;
    float footerIndicatorLineHeight = mFooterIndicatorHeight;
    if (mLinePosition == LinePosition.Top) {
        height = 0;
        footerLineHeight = -footerLineHeight;
        footerIndicatorLineHeight = -footerIndicatorLineHeight;
    }

    //Draw the footer line
    mPath.reset();
    mPath.moveTo(0, height - footerLineHeight / 2f);
    mPath.lineTo(width, height - footerLineHeight / 2f);
    mPath.close();
    canvas.drawPath(mPath, mPaintFooterLine);

    float heightMinusLine = height - footerLineHeight;
    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath.reset();
        mPath.moveTo(halfWidth, heightMinusLine - footerIndicatorLineHeight);
        mPath.lineTo(halfWidth + footerIndicatorLineHeight, heightMinusLine);
        mPath.lineTo(halfWidth - footerIndicatorLineHeight, heightMinusLine);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterIndicator);
        break;

    case Underline:
        if (!currentSelected || page >= boundsSize) {
            break;
        }

        Rect underlineBounds = bounds.get(page);
        final float rightPlusPadding = underlineBounds.right + mFooterIndicatorUnderlinePadding;
        final float leftMinusPadding = underlineBounds.left - mFooterIndicatorUnderlinePadding;
        final float heightMinusLineMinusIndicator = heightMinusLine - footerIndicatorLineHeight;

        mPath.reset();
        mPath.moveTo(leftMinusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLineMinusIndicator);
        mPath.lineTo(leftMinusPadding, heightMinusLineMinusIndicator);
        mPath.close();

        mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
        break;
    }
}

From source file:com.android.core.view.indicator.TitlePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mViewPager == null) {
        return;/*from   ww  w  .j a  v  a  2 s. c  o m*/
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    // mCurrentPage is -1 on first start and after orientation changed. If so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null) {
        mCurrentPage = mViewPager.getCurrentItem();
    }

    //Calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    final int boundsSize = bounds.size();

    //Make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }

    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    //Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }

    //Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        //Get the title
        Rect bound = bounds.get(i);
        //Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            final CharSequence pageTitle = getTitle(i);

            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

            //Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                //Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }

            //Except if there's an intersection with the right view
            if (i < boundsSize - 1) {
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    int w = bound.right - bound.left;
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                    mPaintText);

            //If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                        mPaintText);
            }
        }
    }

    //If we want the line on the top change height to zero and invert the line height to trick the drawing code
    float footerLineHeight = mFooterLineHeight;
    float footerIndicatorLineHeight = mFooterIndicatorHeight;
    if (mLinePosition == LinePosition.Top) {
        height = 0;
        footerLineHeight = -footerLineHeight;
        footerIndicatorLineHeight = -footerIndicatorLineHeight;
    }

    //Draw the footer line
    mPath.reset();
    mPath.moveTo(0, height - footerLineHeight / 2f);
    mPath.lineTo(width, height - footerLineHeight / 2f);
    mPath.close();
    canvas.drawPath(mPath, mPaintFooterLine);

    float heightMinusLine = height - footerLineHeight;
    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath.reset();
        mPath.moveTo(halfWidth, heightMinusLine - footerIndicatorLineHeight);
        mPath.lineTo(halfWidth + footerIndicatorLineHeight, heightMinusLine);
        mPath.lineTo(halfWidth - footerIndicatorLineHeight, heightMinusLine);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterIndicator);
        break;
    case Underline:
    default:
        if (!currentSelected || page >= boundsSize) {
            break;
        }

        Rect underlineBounds = bounds.get(page);
        final float rightPlusPadding = underlineBounds.right + mFooterIndicatorUnderlinePadding;
        final float leftMinusPadding = underlineBounds.left - mFooterIndicatorUnderlinePadding;
        final float heightMinusLineMinusIndicator = heightMinusLine - footerIndicatorLineHeight;

        mPath.reset();
        mPath.moveTo(leftMinusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLineMinusIndicator);
        mPath.lineTo(leftMinusPadding, heightMinusLineMinusIndicator);
        mPath.close();

        mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
        break;
    }
}

From source file:com.maedi.user.godok.v1.viewpagerindicator.TitlePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mViewPager == null) {
        return;// w ww  .jav a 2 s. co  m
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    // mCurrentPage is -1 on first start and after orientation changed. If so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null) {
        mCurrentPage = mViewPager.getCurrentItem();
    }

    //Calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    final int boundsSize = bounds.size();

    //Make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }

    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    //Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }

    //Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        //Get the title
        Rect bound = bounds.get(i);
        //Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            final CharSequence pageTitle = getTitle(i);

            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

            //Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                //Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }

            //Except if there's an intersection with the right view
            if (i < boundsSize - 1) {
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    int w = bound.right - bound.left;
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                    mPaintText);

            //If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                        mPaintText);
            }
        }
    }

    //If we want the line on the top change height to zero and invert the line height to trick the drawing code
    float footerLineHeight = mFooterLineHeight;
    float footerIndicatorLineHeight = mFooterIndicatorHeight;
    if (mLinePosition == LinePosition.Top) {
        height = 0;
        footerLineHeight = -footerLineHeight;
        footerIndicatorLineHeight = -footerIndicatorLineHeight;
    }

    //Draw the footer line
    mPath.reset();
    mPath.moveTo(0, height - footerLineHeight / 2f);
    mPath.lineTo(width, height - footerLineHeight / 2f);
    mPath.close();
    canvas.drawPath(mPath, mPaintFooterLine);

    float heightMinusLine = height - footerLineHeight;
    switch (mFooterIndicatorStyle) {
    case None:
        break;
    case Triangle:
        mPath.reset();
        mPath.moveTo(halfWidth, heightMinusLine - footerIndicatorLineHeight);
        mPath.lineTo(halfWidth + footerIndicatorLineHeight, heightMinusLine);
        mPath.lineTo(halfWidth - footerIndicatorLineHeight, heightMinusLine);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterIndicator);
        break;

    case Underline:
        if (!currentSelected || page >= boundsSize) {
            break;
        }

        Rect underlineBounds = bounds.get(page);
        final float rightPlusPadding = underlineBounds.right + mFooterIndicatorUnderlinePadding;
        final float leftMinusPadding = underlineBounds.left - mFooterIndicatorUnderlinePadding;
        final float heightMinusLineMinusIndicator = heightMinusLine - footerIndicatorLineHeight;

        mPath.reset();
        mPath.moveTo(leftMinusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLineMinusIndicator);
        mPath.lineTo(leftMinusPadding, heightMinusLineMinusIndicator);
        mPath.close();

        mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
        break;
    }
}

From source file:com.sflib.CustomView.indicator.TitlePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mViewPager == null) {
        return;//from   w  w w .j  av a2  s.  co m
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    // mCurrentPage is -1 on first doRefresh and after orientation changed. If so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null) {
        mCurrentPage = mViewPager.getCurrentItem();
    }

    //Calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    final int boundsSize = bounds.size();

    //Make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }

    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    //Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }

    //Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        //Get the title
        Rect bound = bounds.get(i);
        //Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            final CharSequence pageTitle = getTitle(i);

            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

            //Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                //Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }

            //Except if there's an intersection with the right view
            if (i < boundsSize - 1) {
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    int w = bound.right - bound.left;
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                    mPaintText);

            //If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                        mPaintText);
            }
        }
    }

    //If we want the line on the top change height to zero and invert the line height to trick the drawing code
    float footerLineHeight = mFooterLineHeight;
    float footerIndicatorLineHeight = mFooterIndicatorHeight;
    if (mLinePosition == LinePosition.Top) {
        height = 0;
        footerLineHeight = -footerLineHeight;
        footerIndicatorLineHeight = -footerIndicatorLineHeight;
    }

    //Draw the footer line
    mPath.reset();
    mPath.moveTo(0, height - footerLineHeight / 2f);
    mPath.lineTo(width, height - footerLineHeight / 2f);
    mPath.close();
    canvas.drawPath(mPath, mPaintFooterLine);

    float heightMinusLine = height - footerLineHeight;
    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath.reset();
        mPath.moveTo(halfWidth, heightMinusLine - footerIndicatorLineHeight);
        mPath.lineTo(halfWidth + footerIndicatorLineHeight, heightMinusLine);
        mPath.lineTo(halfWidth - footerIndicatorLineHeight, heightMinusLine);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterIndicator);
        break;

    case Underline:
        if (!currentSelected || page >= boundsSize) {
            break;
        }

        Rect underlineBounds = bounds.get(page);
        final float rightPlusPadding = underlineBounds.right + mFooterIndicatorUnderlinePadding;
        final float leftMinusPadding = underlineBounds.left - mFooterIndicatorUnderlinePadding;
        final float heightMinusLineMinusIndicator = heightMinusLine - footerIndicatorLineHeight;

        mPath.reset();
        mPath.moveTo(leftMinusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLineMinusIndicator);
        mPath.lineTo(leftMinusPadding, heightMinusLineMinusIndicator);
        mPath.close();

        mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
        break;
    }
}