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:at.ac.uniklu.mobile.sportal.ui.viewpagerindicator.TitlePageIndicator.java

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

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

    //Calculate views bounds
    ArrayList<RectF> bounds = calculateAllBounds(mPaintText);

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

    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 (mCurrentOffset <= halfWidth) {
        offsetPercent = 1.0f * mCurrentOffset / width;
    } else {
        page += 1;
        offsetPercent = 1.0f * (width - mCurrentOffset) / width;
    }
    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
    RectF 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--) {
            RectF bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                float 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
                RectF rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = 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++) {
            RectF bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                float 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
                RectF leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = 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
        RectF 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);
            //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(mTitleProvider.getTitle(i), 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(mTitleProvider.getTitle(i), bound.left, bound.bottom + mTopPadding, mPaintText);
            }
        }
    }

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

    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath = new Path();
        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) {
            break;
        }

        RectF underlineBounds = bounds.get(page);
        mPath = new Path();
        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.skytree.epubtest.BookViewActivity.java

@SuppressLint({ "DrawAllocation", "DrawAllocation" })
@Override/*w ww .j  av a2 s  .c o  m*/
protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();

    float sl, sr, st, sb;
    sl = 0;
    sr = this.getWidth();
    float ah = this.arrowHeight; // arrow Height;
    if (this.isArrowDown) {
        st = 0;
        sb = this.getHeight() - ah;
    } else {
        st = ah - 10;
        sb = this.getHeight() - 10;
    }

    Path boxPath = new Path();
    boxPath.addRoundRect(new RectF(sl, st, sr, sb), 20, 20, Path.Direction.CW);

    if (arrowPosition <= arrowHeight * 1.5f) {
        arrowPosition = arrowHeight * 1.5f;
    } else if (arrowPosition >= this.getWidth() - arrowHeight * 1.5f) {
        arrowPosition = this.getWidth() - arrowHeight * 1.5f;
    }

    Path arrowPath = new Path();
    if (isArrowDown) {
        arrowPath.moveTo(arrowPosition, sb + ah);
        arrowPath.lineTo((float) (arrowPosition - ah * 0.75), sb - 10);
        arrowPath.lineTo((float) (arrowPosition + ah * 0.75), sb - 10);
        arrowPath.close();
    } else {
        arrowPath.moveTo(arrowPosition, 0);
        arrowPath.lineTo((float) (arrowPosition - ah * 0.75), ah + 10);
        arrowPath.lineTo((float) (arrowPosition + ah * 0.75), ah + 10);
        arrowPath.close();
    }

    paint.setColor(this.strokeColor);
    paint.setStyle(Paint.Style.FILL);
    boxPath.addPath(arrowPath);
    canvas.drawPath(boxPath, paint);

    paint.setColor(this.boxColor);
    paint.setStyle(Paint.Style.FILL);
    boxPath.addPath(arrowPath);
    canvas.save();
    float sf = 0.995f;
    float ox = (this.getWidth() - (this.getWidth() * sf)) / 2.0f;
    float oy = ((this.getHeight() - arrowHeight) - ((this.getHeight() - arrowHeight) * sf)) / 2.0f;

    canvas.translate(ox, oy);
    canvas.scale(sf, sf);
    canvas.drawPath(boxPath, paint);
    canvas.restore();

    if (layoutChanged) {
        this.recalcLayout();
        layoutChanged = false;
    }
}

From source file:com.progekt.webtools.viewpagerindicator.TitlePageIndicator.java

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

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

    //Calculate views bounds
    ArrayList<RectF> 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 (mCurrentOffset <= halfWidth) {
        offsetPercent = 1.0f * mCurrentOffset / width;
    } else {
        page += 1;
        offsetPercent = 1.0f * (width - mCurrentOffset) / width;
    }
    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
    RectF 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--) {
            RectF bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                float 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
                RectF rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = 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++) {
            RectF bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                float 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
                RectF leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = 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
        RectF 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);
            //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(mTitleProvider.getTitle(i), 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(mTitleProvider.getTitle(i), bound.left, bound.bottom + mTopPadding, mPaintText);
            }
        }
    }

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

    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath = new Path();
        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;
        }

        RectF underlineBounds = bounds.get(page);
        mPath = new Path();
        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.android.common.TitlePageIndicator.java

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

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

    //Calculate views bounds
    ArrayList<RectF> 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 (mCurrentOffset <= halfWidth) {
        offsetPercent = 1.0f * mCurrentOffset / width;
    } else {
        page += 1;
        offsetPercent = 1.0f * (width - mCurrentOffset) / width;
    }
    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
    RectF 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--) {
            RectF bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                float 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
                RectF rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = 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++) {
            RectF bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                float 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
                RectF leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = 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
        RectF 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);
            //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(mTitleProvider.GetTitle(i), 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(mTitleProvider.GetTitle(i), bound.left, bound.bottom + mTopPadding, mPaintText);
            }
        }
    }

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

    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath = new Path();
        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;
        }

        RectF underlineBounds = bounds.get(page);
        mPath = new Path();
        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;
    default:
        break;
    }
}

From source file:com.alibaba.akita.widget.TitlePageIndicator.java

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

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

    //Calculate views bounds
    ArrayList<RectF> 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 (mCurrentOffset <= halfWidth) {
        offsetPercent = 1.0f * mCurrentOffset / width;
    } else {
        page += 1;
        offsetPercent = 1.0f * (width - mCurrentOffset) / width;
    }
    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
    RectF 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--) {
            RectF bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                float 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
                RectF rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = 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++) {
            RectF bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                float 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
                RectF leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = 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
        RectF 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);
            //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(mTitleProvider.getTitle(i), 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(mTitleProvider.getTitle(i), bound.left, bound.bottom + mTopPadding, mPaintText);
            }
        }
    }

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

    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath = new Path();
        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;
        }

        RectF underlineBounds = bounds.get(page);
        mPath = new Path();
        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.fanfou.app.opensource.ui.viewpager.TitlePageIndicator.java

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

    // Calculate views bounds
    final ArrayList<RectF> bounds = calculateAllBounds(this.mPaintText);

    final int count = getPageCount();
    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + this.mClipPadding;
    final int width = getWidth();
    final int height = getHeight();
    final int right = left + width;
    final float rightClip = right - this.mClipPadding;
    final float offsetPercent = (1.0f * this.mCurrentOffset) / width;

    int page = this.mCurrentPage;
    if (this.mCurrentOffset > halfWidth) {
        page++;//w ww .j  a v a2  s  .co m
    }
    final boolean currentSelected = (offsetPercent <= TitlePageIndicator.SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= TitlePageIndicator.BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (TitlePageIndicator.SELECTION_FADE_PERCENTAGE - offsetPercent)
            / TitlePageIndicator.SELECTION_FADE_PERCENTAGE;

    // Verify if the current view must be clipped to the screen
    final RectF curPageBound = bounds.get(this.mCurrentPage);

    // fix null pointer exception
    if (curPageBound != null) {
        final 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 (this.mCurrentPage > 0) {
        for (int i = this.mCurrentPage - 1; i >= 0; i--) {
            final RectF bound = bounds.get(i);
            // Is left side is outside the screen
            if (bound.left < leftClip) {
                final float 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
                final RectF rightBound = bounds.get(i + 1);
                // Intersection
                if ((bound.right + this.mTitlePadding) > rightBound.left) {
                    bound.left = rightBound.left - w - this.mTitlePadding;
                    bound.right = bound.left + w;
                }
            }
        }
    }
    // Right views starting from the current position
    if (this.mCurrentPage < countMinusOne) {
        for (int i = this.mCurrentPage + 1; i < count; i++) {
            final RectF bound = bounds.get(i);
            // If right side is outside the screen
            if (bound.right > rightClip) {
                final float 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
                final RectF leftBound = bounds.get(i - 1);
                // Intersection
                if ((bound.left - this.mTitlePadding) < leftBound.right) {
                    bound.left = leftBound.right + this.mTitlePadding;
                    bound.right = bound.left + w;
                }
            }
        }
    }

    // if(App.DEBUG){
    // Log.e(TAG,
    // "===============================================================");
    // Log.e(TAG, "onDraw() mCurrentPage="+mCurrentPage);
    // Log.e(TAG, "onDraw() ");
    // Log.e(TAG, "onDraw() ");
    // Log.e(TAG, "onDraw() ");
    // Log.e(TAG, "onDraw() ");
    // Log.e(TAG, "onDraw() ");
    // Log.e(TAG,
    // "===============================================================");
    // }

    // Now draw views
    for (int i = 0; i < count; i++) {
        // Get the title
        final RectF 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);
            // Only set bold if we are within bounds
            this.mPaintText.setFakeBoldText(currentPage && currentBold && this.mBoldText);

            // Draw text as unselected
            this.mPaintText.setColor(this.mColorText);
            canvas.drawText(this.mTitleProvider.getTitle(i), bound.left, bound.bottom, this.mPaintText);

            // If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                this.mPaintText.setColor(this.mColorSelected);
                this.mPaintText.setAlpha((int) ((this.mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(this.mTitleProvider.getTitle(i), bound.left, bound.bottom, this.mPaintText);
            }
        }
    }

    // Draw the footer line
    // mPath = new Path();
    this.mPath.reset();
    this.mPath.moveTo(0, height - this.mFooterLineHeight);
    this.mPath.lineTo(width, height - this.mFooterLineHeight);
    this.mPath.close();
    canvas.drawPath(this.mPath, this.mPaintFooterLine);

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

    case Underline:
        if (!currentSelected) {
            break;
        }

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

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

From source file:com.ex.english.viewpagerindicator.TitlePageIndicator.java

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

    if (mViewPager == null) {
        return;/* ww  w . 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);

            //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
    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.viewpagerindicator.TitlePageIndicator.java

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

    if (mViewPager == null) {
        return;/*from  w  ww . j a  v  a 2 s.com*/
    }
    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);

            //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
    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;
    case None:
    default:
        break; // To keep the Linter happy
    }
}

From source file:com.chrynan.guitarchords.view.GuitarChordView.java

@Override
protected void onDraw(Canvas canvas) {
    //This method handles a lot of work, possibly too much for an onDraw method, but since I'm not animating
    //or updating the view often, it should be capable of performing the tasks without too much effort.
    //I can optimize this later.

    //First draw the strings and fret markers
    //Fret markers; not worrying about whether or not to show the bridge nut now (since that wasn't calculated
    //in to the drawing size)
    //TODO need to take into account whether or not to show bridgeNut
    int fretCount = chord.getFretCount();
    fretCount = (fretCount < 4) ? 4 : fretCount;
    for (int i = 0; i < fretCount + 1; i++) {
        canvas.drawLine(drawingBounds.left + fretNumberBounds.width(),
                (drawingBounds.top + stringMarkerBounds.height()) + (i * fretSize) + (i * fretMarkerSize),
                drawingBounds.right - stringSize,
                (drawingBounds.top + stringMarkerBounds.height()) + (i * fretSize) + (i * fretMarkerSize),
                fretMarkerPaint);/* www. jav a2s . c o  m*/
    }
    //Strings
    for (int i = 0; i < stringCount; i++) {
        canvas.drawLine((stringMarkerBounds.left) + (i * stringDistance) + (i * stringSize),
                drawingBounds.top + stringMarkerBounds.height(),
                (stringMarkerBounds.left) + (i * stringDistance) + (i * stringSize),
                drawingBounds.bottom - fretMarkerSize, stringPaint);
    }
    //Next draw the fret numbers and string markers
    //Fret numbers; check if we are showing them or not
    if (showFretNumbers) {
        for (int i = 0; i < fretCount; i++) {
            canvas.drawText(String.valueOf(i + 1), drawingBounds.left + fretNumberBounds.width() / 2,
                    getVerticalCenterTextPosition(
                            stringMarkerBounds.bottom + (i * fretMarkerSize) + (i * fretSize) + (fretSize / 2),
                            String.valueOf(i + 1), fretNumberPaint),
                    fretNumberPaint);
        }
    }
    //String markers
    for (int i : chord.getMutedStrings()) {
        canvas.drawText(mutedText,
                (drawingBounds.left + fretNumberBounds.width()) + ((stringCount - i) * stringDistance)
                        + ((stringCount - i) * stringSize),
                getVerticalCenterTextPosition(drawingBounds.top + (stringMarkerBounds.height() / 2), mutedText,
                        stringMarkerPaint),
                stringMarkerPaint);
    }
    for (int i : chord.getOpenStrings()) {
        canvas.drawText(openStringText,
                (drawingBounds.left + fretNumberBounds.width()) + ((stringCount - i) * stringDistance)
                        + ((stringCount - i) * stringSize),
                getVerticalCenterTextPosition(drawingBounds.top + (stringMarkerBounds.height() / 2),
                        openStringText, stringMarkerPaint),
                stringMarkerPaint);
    }
    //Finally, draw all the notes and the note text
    //Bars
    float startCenterX;
    float startCenterY;
    for (ChordMarker cm : chord.getBars()) {
        startCenterX = (drawingBounds.left + fretNumberBounds.width())
                + ((stringCount - cm.getStartString()) * stringDistance)
                + ((stringCount - cm.getStartString()) * stringSize);
        startCenterY = stringMarkerBounds.bottom
                + (((cm.getFret() * fretSize) + (cm.getFret() * fretMarkerSize)) - (fretSize / 2));
        float endCenterX = (drawingBounds.left + fretNumberBounds.width())
                + ((stringCount - cm.getEndString()) * stringDistance)
                + ((stringCount - cm.getEndString()) * stringSize);
        float endCenterY = startCenterY;
        canvas.drawCircle(startCenterX, startCenterY, noteSize / 2, notePaint);
        canvas.drawCircle(endCenterX, endCenterY, noteSize / 2, notePaint);
        if (showFingerNumbers) {
            canvas.drawText(String.valueOf(cm.getFinger()), startCenterX, getVerticalCenterTextPosition(
                    startCenterY, String.valueOf(cm.getFinger()), noteNumberPaint), noteNumberPaint);
            canvas.drawText(String.valueOf(cm.getFinger()), endCenterX,
                    getVerticalCenterTextPosition(endCenterY, String.valueOf(cm.getFinger()), noteNumberPaint),
                    noteNumberPaint);
        }
        barLinePath = new Path();
        barLinePath.moveTo(startCenterX, startCenterY - (noteSize / 2));
        barLinePath.quadTo((startCenterX + endCenterX) / 2, (startCenterY + endCenterY - (noteSize / 2)) / 4,
                endCenterX, endCenterY - (noteSize / 2));
        canvas.drawPath(barLinePath, barLinePaint);
    }
    //Individual notes
    for (ChordMarker cm : chord.getIndividualNotes()) {
        startCenterX = (drawingBounds.left + fretNumberBounds.width())
                + ((stringCount - cm.getStartString()) * stringDistance)
                + ((stringCount - cm.getStartString()) * stringSize);
        startCenterY = stringMarkerBounds.bottom
                + (((cm.getFret() * fretSize) + (cm.getFret() * fretMarkerSize)) - (fretSize / 2));
        canvas.drawCircle(startCenterX, startCenterY, noteSize / 2, notePaint);
        if (showFingerNumbers) {
            canvas.drawText(String.valueOf(cm.getFinger()), startCenterX, getVerticalCenterTextPosition(
                    startCenterY, String.valueOf(cm.getFinger()), noteNumberPaint), noteNumberPaint);
        }
    }
}

From source file:indrora.atomic.indicator.ConversationTitlePageIndicator.java

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

    if (mViewPager == null) {
        return;/*from w w w  . java  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(currentPage ? mColorText : mStateProvider.getColorAt(i));
            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);

            //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
    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;

    case None:
        // Nothing to do here.
        break;
    }
}