Example usage for android.graphics Canvas drawRect

List of usage examples for android.graphics Canvas drawRect

Introduction

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

Prototype

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

Source Link

Document

Draw the specified Rect using the specified Paint.

Usage

From source file:com.ruesga.timelinechart.TimelineChartView.java

/** {@inheritDoc} */
@Override//from   www . j  a  va  2s. c  o  m
protected void onDraw(Canvas c) {
    // 1.- Clip to padding
    c.clipRect(mViewArea);

    // 2.- Draw the backgrounds areas
    c.drawRect(mGraphArea, mGraphAreaBgPaint);
    if (mShowFooter) {
        c.drawRect(mFooterArea, mFooterAreaBgPaint);
    }

    final LongSparseArray<Pair<double[], int[]>> data;
    final double maxValue;
    synchronized (mLock) {
        data = mData;
        maxValue = mMaxValue;
    }
    boolean hasData = data.size() > 0;
    if (hasData && mIsDataComputed) {
        // 3.- Compute viewport and draw the data
        computeItemsOnScreen(data);
        drawBarItems(c, data, maxValue);

        // 4.- Draw tick labels and current position
        if (mShowFooter) {
            drawTickLabels(c, data);
            c.drawPath(mCurrentPositionPath, mFooterAreaBgPaint);
        }
    }

    // Draw the edge scrolling effects
    drawEdgeEffects(c);
}

From source file:com.akshay.protocol10.asplayer.widget.SlidingUpPanelLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    if (isSlidingEnabled() && mSlideableView != child) {
        // Clip against the slider; no sense drawing what will immediately
        // be covered,
        // Unless the panel is set to overlay content
        if (!mOverlayContent) {
            canvas.getClipBounds(mTmpRect);
            if (mIsSlidingUp) {
                mTmpRect.bottom = Math.min(mTmpRect.bottom, mSlideableView.getTop());
            } else {
                mTmpRect.top = Math.max(mTmpRect.top, mSlideableView.getBottom());
            }/*w  w w  . j  av a2  s .  com*/
            canvas.clipRect(mTmpRect);
        }
    }

    result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(save);

    if (mCoveredFadeColor != 0 && mSlideOffset > 0) {
        final int baseAlpha = (mCoveredFadeColor & 0xff000000) >>> 24;
        final int imag = (int) (baseAlpha * mSlideOffset);
        final int color = imag << 24 | (mCoveredFadeColor & 0xffffff);
        mCoveredFadePaint.setColor(color);
        canvas.drawRect(mTmpRect, mCoveredFadePaint);
    }

    return result;
}

From source file:com.sflib.CustomView.baseview.RoundRecPagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;//from   w  w  w.  j a  v a  2s  .c o  m
    }

    final int height = getHeight();

    // draw indicator line

    rectPaint.setColor(indicatorColor);

    // default: line below current tab
    View currentTab = tabsContainer.getChildAt(currentPosition);
    float lineLeft = currentTab.getLeft();
    float lineRight = currentTab.getRight();

    // if there is an offset, doRefresh interpolating left and right coordinates between current and next tab
    if (currentPositionOffset > 0f && currentPosition < tabCount - 1) {

        View nextTab = tabsContainer.getChildAt(currentPosition + 1);
        final float nextTabLeft = nextTab.getLeft();
        final float nextTabRight = nextTab.getRight();

        lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft);
        lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight);
    }
    float indicator_top = getPaddingTop();
    float indicator_botom = currentTab.getHeight() + indicator_top;
    float indicator_left = lineLeft + getPaddingLeft();
    //        L.info(this,"indicator_left: "+indicator_left+" lineLeft: "+lineLeft+" padding: "+currentTab.getPaddingLeft()+" currentPositionOffset: "+currentPositionOffset);
    RectF indicatorRec = new RectF(indicator_left - indicatorLeftRightPadding + tabPadding,
            indicator_top - indicatorTopBottomPadding, lineRight + indicatorLeftRightPadding - tabPadding,
            indicator_botom + indicatorTopBottomPadding);
    canvas.drawRoundRect(indicatorRec, indicatorRectCorner, indicatorRectCorner, rectPaint);

    // draw underline
    rectPaint.setColor(underlineColor);
    RectF rect = new RectF(0, height - underlineHeight, tabsContainer.getWidth(), height);
    canvas.drawRect(rect, rectPaint);
    // draw divider

    dividerPaint.setColor(dividerColor);
    for (int i = 0; i < tabCount - 1; i++) {
        View tab = tabsContainer.getChildAt(i);
        canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint);
    }
}

From source file:co.codecrunch.musicplayerlite.slidinguppanelhelper.SlidingUpPanelLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    if (mSlideableView != child) { // if main view
        // Clip against the slider; no sense drawing what will immediately
        // be covered,
        // Unless the panel is set to overlay content
        canvas.getClipBounds(mTmpRect);/*  w ww.jav a  2  s  .  c o  m*/
        if (!mOverlayContent) {
            if (mIsSlidingUp) {
                mTmpRect.bottom = Math.min(mTmpRect.bottom, mSlideableView.getTop());
            } else {
                mTmpRect.top = Math.max(mTmpRect.top, mSlideableView.getBottom());
            }
        }
        if (mClipPanel) {
            canvas.clipRect(mTmpRect);
        }

        result = super.drawChild(canvas, child, drawingTime);

        if (mCoveredFadeColor != 0 && mSlideOffset > 0) {
            final int baseAlpha = (mCoveredFadeColor & 0xff000000) >>> 24;
            final int imag = (int) (baseAlpha * mSlideOffset);
            final int color = imag << 24 | (mCoveredFadeColor & 0xffffff);
            mCoveredFadePaint.setColor(color);
            canvas.drawRect(mTmpRect, mCoveredFadePaint);
        }
    } else {
        result = super.drawChild(canvas, child, drawingTime);
    }

    canvas.restoreToCount(save);

    return result;
}

From source file:com.quran.labs.androidquran.widgets.SlidingUpPanelLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    boolean drawScrim = false;

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately be covered,
        // Unless the panel is set to overlay content
        if (!mOverlayContent) {
            canvas.getClipBounds(mTmpRect);
            if (mIsSlidingUp) {
                mTmpRect.bottom = Math.min(mTmpRect.bottom, mSlideableView.getTop());
            } else {
                mTmpRect.top = Math.max(mTmpRect.top, mSlideableView.getBottom());
            }/*  w  ww.jav  a2  s  . c  o m*/
            canvas.clipRect(mTmpRect);
        }
        if (mSlideOffset < 1) {
            drawScrim = true;
        }
    }

    result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(save);

    if (drawScrim) {
        final int baseAlpha = (mCoveredFadeColor & 0xff000000) >>> 24;
        final int imag = (int) (baseAlpha * (1 - mSlideOffset));
        final int color = imag << 24 | (mCoveredFadeColor & 0xffffff);
        mCoveredFadePaint.setColor(color);
        canvas.drawRect(mTmpRect, mCoveredFadePaint);
    }

    return result;
}

From source file:com.secondhand.view.CirclePageIndicator.java

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

    if (mViewPager == null) {
        return;//from w w w . j  a v a 2  s  .c  om
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 1) {
        return;
    }

    if (mCurrentPage >= count) {
        setCurrentItem(count - 1);
        return;
    }

    int longSize;
    int longPaddingBefore;
    int longPaddingAfter;
    int shortPaddingBefore;
    if (mOrientation == HORIZONTAL) {
        longSize = getWidth();
        longPaddingBefore = getPaddingLeft();
        longPaddingAfter = getPaddingRight();
        shortPaddingBefore = getPaddingTop();
    } else {
        longSize = getHeight();
        longPaddingBefore = getPaddingTop();
        longPaddingAfter = getPaddingBottom();
        shortPaddingBefore = getPaddingLeft();
    }

    final float threeRadius = mRadius * 2;
    final float shortOffset = shortPaddingBefore + mRadius;
    float longOffset = longPaddingBefore + mRadius;
    if (mCentered) {
        longOffset += ((longSize - longPaddingBefore - longPaddingAfter) / 2.0f)
                - ((count * threeRadius) / 2.0f);
    }

    float dX;
    float dY;

    float pageFillRadius = mRadius;
    if (mPaintStroke.getStrokeWidth() > 0) {
        pageFillRadius -= mPaintStroke.getStrokeWidth() / 2.0f;
    }

    // Draw stroked circles
    for (int iLoop = 0; iLoop < count; iLoop++) {
        float drawLong = longOffset + (iLoop * threeRadius);
        if (mOrientation == HORIZONTAL) {
            dX = drawLong;
            dY = shortOffset;
        } else {
            dX = shortOffset;
            dY = drawLong;
        }
        // Only paint fill if not completely transparent
        if (mPaintPageFill.getAlpha() > 0) {
            canvas.drawCircle(dX, dY, pageFillRadius, mPaintPageFill);
        }

        // Only paint stroke if a stroke width was non-zero
        if (pageFillRadius != mRadius) {
            canvas.drawRect(new RectF(dX - mRadius / 2, dY - mRadius / 2, dX + mRadius / 2, dY + mRadius / 2),
                    mPaintStroke);
        }
    }

    // Draw the filled circle according to the current scroll
    float cx = (mSnap ? mSnapPage : mCurrentPage) * threeRadius;
    if (!mSnap) {
        cx += mPageOffset * threeRadius;
    }
    if (mOrientation == HORIZONTAL) {
        dX = longOffset + cx;
        dY = shortOffset;
    } else {
        dX = shortOffset;
        dY = longOffset + cx;
    }
    canvas.drawRect(new RectF(dX - mRadius / 2, dY - mRadius / 2, dX + mRadius / 2, dY + mRadius / 2),
            mPaintFill);
}

From source file:com.dwdesign.tweetings.util.Utils.java

public static Bitmap getColorPreviewBitmap(final Context context, final int color) {
    if (context == null)
        return null;
    final float density = context.getResources().getDisplayMetrics().density;
    final int width = (int) (32 * density), height = (int) (32 * density);

    final Bitmap bm = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    final Canvas canvas = new Canvas(bm);

    final int rectrangle_size = (int) (density * 5);
    final int numRectanglesHorizontal = (int) Math.ceil(width / rectrangle_size);
    final int numRectanglesVertical = (int) Math.ceil(height / rectrangle_size);
    final Rect r = new Rect();
    boolean verticalStartWhite = true;
    for (int i = 0; i <= numRectanglesVertical; i++) {

        boolean isWhite = verticalStartWhite;
        for (int j = 0; j <= numRectanglesHorizontal; j++) {

            r.top = i * rectrangle_size;
            r.left = j * rectrangle_size;
            r.bottom = r.top + rectrangle_size;
            r.right = r.left + rectrangle_size;
            final Paint paint = new Paint();
            paint.setColor(isWhite ? Color.WHITE : Color.GRAY);

            canvas.drawRect(r, paint);

            isWhite = !isWhite;/* w w  w  .j  av a 2s.  co  m*/
        }

        verticalStartWhite = !verticalStartWhite;

    }
    canvas.drawColor(color);
    final Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    paint.setStrokeWidth(2.0f);
    final float[] points = new float[] { 0, 0, width, 0, 0, 0, 0, height, width, 0, width, height, 0, height,
            width, height };
    canvas.drawLines(points, paint);

    return bm;
}

From source file:com.leeon.blank.widget.RangeBarNew.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    final int length = mTextArray.length;

    /*** Draw text marks ***/
    mPaint.setTextSize(mTextSize);//from   w  w w . ja v a  2  s .  c o  m
    mPaint.setColor(mTextColorNormal);
    for (int i = 0; i < length; i++) {
        final String text2draw = mTextArray[i] == -1 ? "" : mTextArray[i] + "";
        final float textWidth = mTextWidthArray[i];
        float textDrawLeft;
        // The last text mark's draw location should be adjust.
        if (i == length - 1) {
            textDrawLeft = mRangeBarRect.right - textWidth / 2;
        } else {
            textDrawLeft = mRangeBarRect.left + i * mPartLength - textWidth / 2;
        }
        canvas.drawText(text2draw, textDrawLeft, mRangeBarRect.top - 24 - mMarginBetween, mPaint);

        //draw lines
        float lineDrawLeft = mRangeBarRect.left + i * mPartLength;
        canvas.drawLine(lineDrawLeft, mRangeBarRect.top, lineDrawLeft, mRangeBarRect.top - 24, mPaint);

        //draw short lines
        if (i != length - 1) {
            for (int j = 1; j < 5; j++) {
                float lineDrawLeft_short = lineDrawLeft + j * mTinyPartLength;
                canvas.drawLine(lineDrawLeft_short, mRangeBarRect.top, lineDrawLeft_short,
                        mRangeBarRect.top - 18, mPaint);
            }
        }
    }

    /*** Draw seekBar ***/
    final float radius = mRangeBarHeight / 4f;
    mRangeBarRectSelected.left = mRangeBarRect.left + mTinyPartLength * mLeftCursorIndex;
    mRangeBarRectSelected.right = mRangeBarRect.left + mTinyPartLength * mRightCursorIndex;
    // If whole of seekbar is selected, just draw seekbar with selectedcolor.
    if (mLeftCursorIndex == 0 && mRightCursorIndex == (length - 1) * 5) {
        mPaint.setColor(mRangeBarColorSelected);
        canvas.drawRoundRect(mRangeBarRect, radius, radius, mPaint);
    } else {
        // Draw background first.
        mPaint.setColor(mRangeBarColorNormal);
        canvas.drawRoundRect(mRangeBarRect, radius, radius, mPaint);

        // Draw selected part.
        mPaint.setColor(mRangeBarColorSelected);
        // Can draw rounded rectangle, but original rectangle is enough.
        // Because edges of selected part will be covered by cursors.
        canvas.drawRect(mRangeBarRectSelected, mPaint);
    }

    /*** Draw cursors ***/
    // left cursor first
    final int leftWidth = mLeftCursorBG.getIntrinsicWidth();
    final int leftHeight = mLeftCursorBG.getIntrinsicHeight();
    final int leftLeft = (int) (mRangeBarRectSelected.left - leftWidth / 2f);
    final int leftTop = (int) (mRangeBarRect.bottom + mMarginBetween);
    mLeftCursorRect.left = leftLeft;
    mLeftCursorRect.top = leftTop;
    mLeftCursorRect.right = leftLeft + leftWidth;
    mLeftCursorRect.bottom = leftTop + leftHeight;
    mLeftCursorBG.setBounds(mLeftCursorRect);
    mLeftCursorBG.draw(canvas);

    // right cursor second
    final int rightWidth = mRightCursorBG.getIntrinsicWidth();
    final int rightHeight = mRightCursorBG.getIntrinsicHeight();
    final int rightLeft = (int) (mRangeBarRectSelected.right - rightWidth / 2f);
    final int rightTop = (int) (mRangeBarRect.bottom + mMarginBetween);
    mRightCursorRect.left = rightLeft;
    mRightCursorRect.top = rightTop;
    mRightCursorRect.right = rightLeft + rightWidth;
    mRightCursorRect.bottom = rightTop + rightHeight;
    mRightCursorBG.setBounds(mRightCursorRect);
    mRightCursorBG.draw(canvas);

    //Draw indicators
    final int indicatorWidth = Math.max(indicatorDrawable.getIntrinsicWidth(), (int) mPaint.measureText("888"));
    if (mLeftHit && mLeftPointerID != -1) {
        final int indicatorLeft = (int) (mRangeBarRectSelected.left - (float) indicatorWidth / 2);
        final int indicatorTop = mPaddingRect.top;
        mLeftIndicatorRect.left = indicatorLeft;
        mLeftIndicatorRect.top = indicatorTop;
        mLeftIndicatorRect.right = indicatorLeft + indicatorWidth;
        mLeftIndicatorRect.bottom = indicatorTop + indicatorHeight;
        indicatorDrawable.setBounds(mLeftIndicatorRect);
        indicatorDrawable.draw(canvas);
        mPaint.setColor(Color.WHITE);
        mValueLeft = getValueByIndex(mLeftCursorIndex);
        canvas.drawText(mValueLeft + "", mLeftIndicatorRect.centerX() - mPaint.measureText(mValueLeft + "") / 2,
                mLeftIndicatorRect.centerY() + mTextSize / 4, mPaint);
    }

    if (mRightHit && mRightPointerID != -1) {
        final int indicatorRightLeft = (int) (mRangeBarRectSelected.right - (float) rightWidth / 2);
        final int indicatorRightTop = mPaddingRect.top;
        mRightIndicatorRect.left = indicatorRightLeft;
        mRightIndicatorRect.top = indicatorRightTop;
        mRightIndicatorRect.right = indicatorRightLeft + indicatorWidth;
        mRightIndicatorRect.bottom = indicatorRightTop + indicatorHeight;
        indicatorDrawable.setBounds(mRightIndicatorRect);
        indicatorDrawable.draw(canvas);
        mPaint.setColor(Color.WHITE);
        mValueRight = getValueByIndex(mRightCursorIndex);
        if (mValueRight == -1) {
            canvas.drawText("", mRightIndicatorRect.centerX() - mPaint.measureText("") / 2,
                    mRightIndicatorRect.centerY() + mTextSize / 4, mPaint);
        } else {
            canvas.drawText(mValueRight + "",
                    mRightIndicatorRect.centerX() - mPaint.measureText(mValueRight + "") / 2,
                    mRightIndicatorRect.centerY() + mTextSize / 4, mPaint);
        }
    }

    triggerCallback();
}

From source file:cn.oddcloud.www.navigationtabbar.ntb.NavigationTabBar.java

@SuppressWarnings("ConstantConditions")
@Override/*from  w ww . j a v  a2  s.c o m*/
protected void onDraw(final Canvas canvas) {
    // Get height of NTB with badge on nor
    final int mBadgedHeight = (int) (mBounds.height() + mBadgeMargin);

    // Set main canvas
    if (mBitmap == null || mBitmap.isRecycled()) {
        mBitmap = Bitmap.createBitmap((int) mBounds.width(), mBadgedHeight, Bitmap.Config.ARGB_8888);
        mCanvas.setBitmap(mBitmap);
    }
    // Set pointer canvas
    if (mPointerBitmap == null || mPointerBitmap.isRecycled()) {
        mPointerBitmap = Bitmap.createBitmap((int) mBounds.width(), mBadgedHeight, Bitmap.Config.ARGB_8888);
        mPointerCanvas.setBitmap(mPointerBitmap);
    }
    // Set icons canvas
    if (mIconsBitmap == null || mIconsBitmap.isRecycled()) {
        mIconsBitmap = Bitmap.createBitmap((int) mBounds.width(), mBadgedHeight, Bitmap.Config.ARGB_8888);
        mIconsCanvas.setBitmap(mIconsBitmap);
    }
    // Set titles canvas
    if (mIsTitled) {
        if (mTitlesBitmap == null || mTitlesBitmap.isRecycled()) {
            mTitlesBitmap = Bitmap.createBitmap((int) mBounds.width(), mBadgedHeight, Bitmap.Config.ARGB_8888);
            mTitlesCanvas.setBitmap(mTitlesBitmap);
        }
    } else
        mTitlesBitmap = null;

    // Reset and clear canvases
    mCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
    mPointerCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
    mIconsCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
    if (mIsTitled)
        mTitlesCanvas.drawColor(0, PorterDuff.Mode.CLEAR);

    if (mCornersRadius == 0)
        canvas.drawRect(mBgBounds, mBgPaint);
    else
        canvas.drawRoundRect(mBgBounds, mCornersRadius, mCornersRadius, mBgPaint);

    // Get pointer badge margin for gravity
    final float barBadgeMargin = mBadgeGravity == BadgeGravity.TOP ? mBadgeMargin : 0.0F;

    // Draw our model colors
    for (int i = 0; i < mModels.size(); i++) {
        mPaint.setColor(mModels.get(i).getColor());

        if (mIsHorizontalOrientation) {
            final float left = mModelSize * i;
            final float right = left + mModelSize;
            mCanvas.drawRect(left, barBadgeMargin, right, mBounds.height() + barBadgeMargin, mPaint);
        } else {
            final float top = mModelSize * i;
            final float bottom = top + mModelSize;
            mCanvas.drawRect(0.0F, top, mBounds.width(), bottom, mPaint);
        }
    }

    // Set bound of pointer
    if (mIsHorizontalOrientation)
        mPointerBounds.set(mPointerLeftTop, barBadgeMargin, mPointerRightBottom,
                mBounds.height() + barBadgeMargin);
    else
        mPointerBounds.set(0.0F, mPointerLeftTop, mBounds.width(), mPointerRightBottom);

    // Draw pointer for model colors
    if (mCornersRadius == 0)
        mPointerCanvas.drawRect(mPointerBounds, mPaint);
    else
        mPointerCanvas.drawRoundRect(mPointerBounds, mCornersRadius, mCornersRadius, mPaint);

    // Draw pointer into main canvas
    mCanvas.drawBitmap(mPointerBitmap, 0.0F, 0.0F, mPointerPaint);

    // Set vars for icon when model with title or without
    final float iconMarginTitleHeight = mIconSize + mTitleMargin + mModelTitleSize;

    // Draw model icons
    for (int i = 0; i < mModels.size(); i++) {
        final Model model = mModels.get(i);

        // Variables to center our icons
        final float leftOffset;
        final float topOffset;
        final float matrixCenterX;
        final float matrixCenterY;

        // Set offset to titles
        final float leftTitleOffset = (mModelSize * i) + (mModelSize * 0.5F);
        final float topTitleOffset = mBounds.height() - (mBounds.height() - iconMarginTitleHeight) * 0.5F;

        if (mIsHorizontalOrientation) {
            leftOffset = (mModelSize * i) + (mModelSize - model.mIcon.getWidth()) * 0.5F;
            topOffset = (mBounds.height() - model.mIcon.getHeight()) * 0.5F;
        } else {
            leftOffset = (mBounds.width() - (float) model.mIcon.getWidth()) * 0.5F;
            topOffset = (mModelSize * i) + (mModelSize - (float) model.mIcon.getHeight()) * 0.5F;
        }

        matrixCenterX = leftOffset + (float) model.mIcon.getWidth() * 0.5F;
        matrixCenterY = topOffset + (float) model.mIcon.getHeight() * 0.5F;

        // Title translate position
        final float titleTranslate = topOffset - model.mIcon.getHeight() * TITLE_MARGIN_SCALE_FRACTION;

        // Translate icon to model center
        model.mIconMatrix.setTranslate(leftOffset,
                (mIsTitled && mTitleMode == TitleMode.ALL) ? titleTranslate : topOffset);

        // Get interpolated fraction for left last and current models
        final float interpolation = mResizeInterpolator.getResizeInterpolation(mFraction, true);
        final float lastInterpolation = mResizeInterpolator.getResizeInterpolation(mFraction, false);

        // Scale value relative to interpolation
        final float matrixScale = model.mActiveIconScaleBy * (mIsScaled ? interpolation : NON_SCALED_FRACTION);
        final float matrixLastScale = model.mActiveIconScaleBy
                * (mIsScaled ? lastInterpolation : (MAX_FRACTION - NON_SCALED_FRACTION));

        // Get title alpha relative to interpolation
        final int titleAlpha = (int) (MAX_ALPHA * interpolation);
        final int titleLastAlpha = MAX_ALPHA - (int) (MAX_ALPHA * lastInterpolation);
        // Get title scale relative to interpolation
        final float titleScale = MAX_FRACTION
                + ((mIsScaled ? interpolation : NON_SCALED_FRACTION) * TITLE_ACTIVE_SCALE_BY);
        final float titleLastScale = mIsScaled
                ? (MAX_FRACTION + TITLE_ACTIVE_SCALE_BY) - (lastInterpolation * TITLE_ACTIVE_SCALE_BY)
                : titleScale;

        mIconPaint.setAlpha(MAX_ALPHA);
        if (model.mSelectedIcon != null)
            mSelectedIconPaint.setAlpha(MAX_ALPHA);

        // Check if we handle models from touch on NTB or from ViewPager
        // There is a strange logic
        // of ViewPager onPageScrolled method, so it is
        if (mIsSetIndexFromTabBar) {
            if (mIndex == i)
                updateCurrentModel(model, leftOffset, topOffset, titleTranslate, interpolation, matrixCenterX,
                        matrixCenterY, matrixScale, titleScale, titleAlpha);
            else if (mLastIndex == i)
                updateLastModel(model, leftOffset, topOffset, titleTranslate, lastInterpolation, matrixCenterX,
                        matrixCenterY, matrixLastScale, titleLastScale, titleLastAlpha);
            else
                updateInactiveModel(model, leftOffset, topOffset, titleScale, matrixScale, matrixCenterX,
                        matrixCenterY);
        } else {
            if (i == mIndex + 1)
                updateCurrentModel(model, leftOffset, topOffset, titleTranslate, interpolation, matrixCenterX,
                        matrixCenterY, matrixScale, titleScale, titleAlpha);
            else if (i == mIndex)
                updateLastModel(model, leftOffset, topOffset, titleTranslate, lastInterpolation, matrixCenterX,
                        matrixCenterY, matrixLastScale, titleLastScale, titleLastAlpha);
            else
                updateInactiveModel(model, leftOffset, topOffset, titleScale, matrixScale, matrixCenterX,
                        matrixCenterY);
        }

        // Draw original model icon
        if (model.mSelectedIcon == null) {
            if (model.mIcon != null && !model.mIcon.isRecycled())
                mIconsCanvas.drawBitmap(model.mIcon, model.mIconMatrix, mIconPaint);
        } else {
            if (mIconPaint.getAlpha() != MIN_ALPHA && model.mIcon != null && !model.mIcon.isRecycled())
                // Draw original icon when is visible
                mIconsCanvas.drawBitmap(model.mIcon, model.mIconMatrix, mIconPaint);
        }
        // Draw selected icon when exist and visible
        if (mSelectedIconPaint.getAlpha() != MIN_ALPHA && model.mSelectedIcon != null
                && !model.mSelectedIcon.isRecycled())
            mIconsCanvas.drawBitmap(model.mSelectedIcon, model.mIconMatrix, mSelectedIconPaint);

        if (mIsTitled)
            mTitlesCanvas.drawText(isInEditMode() ? PREVIEW_TITLE : model.getTitle(), leftTitleOffset,
                    topTitleOffset, mModelTitlePaint);
    }

    // Reset pointer bounds for icons and titles
    if (mIsHorizontalOrientation)
        mPointerBounds.set(mPointerLeftTop, 0.0F, mPointerRightBottom, mBounds.height());
    if (mCornersRadius == 0) {
        if (mIsTinted)
            mIconsCanvas.drawRect(mPointerBounds, mIconPointerPaint);
        if (mIsTitled)
            mTitlesCanvas.drawRect(mPointerBounds, mIconPointerPaint);
    } else {
        if (mIsTinted)
            mIconsCanvas.drawRoundRect(mPointerBounds, mCornersRadius, mCornersRadius, mIconPointerPaint);
        if (mIsTitled)
            mTitlesCanvas.drawRoundRect(mPointerBounds, mCornersRadius, mCornersRadius, mIconPointerPaint);
    }

    // Draw general bitmap
    canvas.drawBitmap(mBitmap, 0.0F, 0.0F, null);
    // Draw icons bitmap on top
    canvas.drawBitmap(mIconsBitmap, 0.0F, barBadgeMargin, null);
    // Draw titles bitmap on top
    if (mIsTitled)
        canvas.drawBitmap(mTitlesBitmap, 0.0F, barBadgeMargin, null);

    // If is not badged, exit
    if (!mIsBadged)
        return;

    // Model badge margin and offset relative to gravity mode
    final float modelBadgeMargin = mBadgeGravity == BadgeGravity.TOP ? mBadgeMargin : mBounds.height();
    final float modelBadgeOffset = mBadgeGravity == BadgeGravity.TOP ? 0.0F : mBounds.height() - mBadgeMargin;

    for (int i = 0; i < mModels.size(); i++) {
        final Model model = mModels.get(i);

        // Set preview badge title
        if (isInEditMode() || TextUtils.isEmpty(model.getBadgeTitle()))
            model.setBadgeTitle(PREVIEW_BADGE);

        // Set badge title bounds
        mBadgePaint.setTextSize(mBadgeTitleSize * model.mBadgeFraction);
        mBadgePaint.getTextBounds(model.getBadgeTitle(), 0, model.getBadgeTitle().length(), mBadgeBounds);

        // Get horizontal and vertical padding for bg
        final float horizontalPadding = mBadgeTitleSize * BADGE_HORIZONTAL_FRACTION;
        final float verticalPadding = horizontalPadding * BADGE_VERTICAL_FRACTION;

        // Set horizontal badge offset
        final float badgeBoundsHorizontalOffset = (mModelSize * i)
                + (mModelSize * mBadgePosition.mPositionFraction);

        // If is badge title only one char, so create circle else round rect
        final float badgeMargin = mBadgeMargin * model.mBadgeFraction;
        if (model.getBadgeTitle().length() == 1) {
            mBgBadgeBounds.set(badgeBoundsHorizontalOffset - badgeMargin, modelBadgeMargin - badgeMargin,
                    badgeBoundsHorizontalOffset + badgeMargin, modelBadgeMargin + badgeMargin);
        } else
            mBgBadgeBounds.set(
                    badgeBoundsHorizontalOffset
                            - Math.max(badgeMargin, mBadgeBounds.centerX() + horizontalPadding),
                    modelBadgeMargin - badgeMargin,
                    badgeBoundsHorizontalOffset
                            + Math.max(badgeMargin, mBadgeBounds.centerX() + horizontalPadding),
                    modelBadgeOffset + (verticalPadding * 2.0F) + mBadgeBounds.height());

        // Set color and alpha for badge bg
        if (model.mBadgeFraction == MIN_FRACTION)
            mBadgePaint.setColor(Color.TRANSPARENT);
        else
            mBadgePaint.setColor(mBadgeBgColor == AUTO_COLOR ? mActiveColor : mBadgeBgColor);
        mBadgePaint.setAlpha((int) (MAX_ALPHA * model.mBadgeFraction));

        // Set corners to round rect for badge bg and draw
        final float cornerRadius = mBgBadgeBounds.height() * 0.5F;
        canvas.drawRoundRect(mBgBadgeBounds, cornerRadius, cornerRadius, mBadgePaint);

        // Set color and alpha for badge title
        if (model.mBadgeFraction == MIN_FRACTION)
            mBadgePaint.setColor(Color.TRANSPARENT);
        else //noinspection ResourceAsColor
            mBadgePaint.setColor(mBadgeTitleColor == AUTO_COLOR ? model.getColor() : mBadgeTitleColor);
        mBadgePaint.setAlpha((int) (MAX_ALPHA * model.mBadgeFraction));

        // Set badge title center position and draw title
        final float badgeHalfHeight = mBadgeBounds.height() * 0.5F;
        float badgeVerticalOffset = (mBgBadgeBounds.height() * 0.5F) + badgeHalfHeight - mBadgeBounds.bottom
                + modelBadgeOffset;
        canvas.drawText(model.getBadgeTitle(), badgeBoundsHorizontalOffset,
                badgeVerticalOffset + mBadgeBounds.height() - (mBadgeBounds.height() * model.mBadgeFraction),
                mBadgePaint);
    }
}

From source file:com.simon.dribbble.widget.navigationbar.NavigationTabBar.java

@SuppressWarnings("ConstantConditions")
@Override/*w w w.j a v  a  2 s.c o  m*/
protected void onDraw(final Canvas canvas) {
    // Get height of NTB with badge on nor
    final int mBadgedHeight = (int) (mBounds.height() + mBadgeMargin);

    // Set home canvas
    if (mBitmap == null || mBitmap.isRecycled()) {
        mBitmap = Bitmap.createBitmap((int) mBounds.width(), mBadgedHeight, Bitmap.Config.ARGB_8888);
        mCanvas.setBitmap(mBitmap);
    }
    // Set pointer canvas
    if (mPointerBitmap == null || mPointerBitmap.isRecycled()) {
        mPointerBitmap = Bitmap.createBitmap((int) mBounds.width(), mBadgedHeight, Bitmap.Config.ARGB_8888);
        mPointerCanvas.setBitmap(mPointerBitmap);
    }
    // Set icons canvas
    if (mIconsBitmap == null || mIconsBitmap.isRecycled()) {
        mIconsBitmap = Bitmap.createBitmap((int) mBounds.width(), mBadgedHeight, Bitmap.Config.ARGB_8888);
        mIconsCanvas.setBitmap(mIconsBitmap);
    }
    // Set titles canvas
    if (mIsTitled) {
        if (mTitlesBitmap == null || mTitlesBitmap.isRecycled()) {
            mTitlesBitmap = Bitmap.createBitmap((int) mBounds.width(), mBadgedHeight, Bitmap.Config.ARGB_8888);
            mTitlesCanvas.setBitmap(mTitlesBitmap);
        }
    } else
        mTitlesBitmap = null;

    // Reset and clear canvases
    mCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
    mPointerCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
    mIconsCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
    if (mIsTitled)
        mTitlesCanvas.drawColor(0, PorterDuff.Mode.CLEAR);

    if (mCornersRadius == 0)
        canvas.drawRect(mBgBounds, mBgPaint);
    else
        canvas.drawRoundRect(mBgBounds, mCornersRadius, mCornersRadius, mBgPaint);

    // Get pointer badge margin for gravity
    final float barBadgeMargin = mBadgeGravity == BadgeGravity.TOP ? mBadgeMargin : 0.0F;

    // Draw our model colors
    for (int i = 0; i < mModels.size(); i++) {
        mPaint.setColor(mModels.get(i).getColor());

        if (mIsHorizontalOrientation) {
            final float left = mModelSize * i;
            final float right = left + mModelSize;
            mCanvas.drawRect(left, barBadgeMargin, right, mBounds.height() + barBadgeMargin, mPaint);
        } else {
            final float top = mModelSize * i;
            final float bottom = top + mModelSize;
            mCanvas.drawRect(0.0F, top, mBounds.width(), bottom, mPaint);
        }
    }

    // Set bound of pointer
    if (mIsHorizontalOrientation)
        mPointerBounds.set(mPointerLeftTop, barBadgeMargin, mPointerRightBottom,
                mBounds.height() + barBadgeMargin);
    else
        mPointerBounds.set(0.0F, mPointerLeftTop, mBounds.width(), mPointerRightBottom);

    // Draw pointer for model colors
    if (mCornersRadius == 0)
        mPointerCanvas.drawRect(mPointerBounds, mPaint);
    else
        mPointerCanvas.drawRoundRect(mPointerBounds, mCornersRadius, mCornersRadius, mPaint);

    // Draw pointer into home canvas
    mCanvas.drawBitmap(mPointerBitmap, 0.0F, 0.0F, mPointerPaint);

    // Set vars for icon when model with title or without
    final float iconMarginTitleHeight = mIconSize + mTitleMargin + mModelTitleSize;

    // Draw model icons
    for (int i = 0; i < mModels.size(); i++) {
        final Model model = mModels.get(i);

        // Variables to center our icons
        final float leftOffset;
        final float topOffset;
        final float matrixCenterX;
        final float matrixCenterY;

        // Set offset to titles
        final float leftTitleOffset = (mModelSize * i) + (mModelSize * 0.5F);
        final float topTitleOffset = mBounds.height() - (mBounds.height() - iconMarginTitleHeight) * 0.5F;

        if (mIsHorizontalOrientation) {
            leftOffset = (mModelSize * i) + (mModelSize - model.mIcon.getWidth()) * 0.5F;
            topOffset = (mBounds.height() - model.mIcon.getHeight()) * 0.5F;
        } else {
            leftOffset = (mBounds.width() - (float) model.mIcon.getWidth()) * 0.5F;
            topOffset = (mModelSize * i) + (mModelSize - (float) model.mIcon.getHeight()) * 0.5F;
        }

        matrixCenterX = leftOffset + (float) model.mIcon.getWidth() * 0.5F;
        matrixCenterY = topOffset + (float) model.mIcon.getHeight() * 0.5F;

        // Title translate position
        final float titleTranslate = topOffset - model.mIcon.getHeight() * TITLE_MARGIN_SCALE_FRACTION;

        // Translate icon to model center
        model.mIconMatrix.setTranslate(leftOffset,
                (mIsTitled && mTitleMode == TitleMode.ALL) ? titleTranslate : topOffset);

        // Get interpolated fraction for left last and current models
        final float interpolation = mResizeInterpolator.getResizeInterpolation(mFraction, true);
        final float lastInterpolation = mResizeInterpolator.getResizeInterpolation(mFraction, false);

        // Scale value relative to interpolation
        final float matrixScale = model.mActiveIconScaleBy * (mIsScaled ? interpolation : NON_SCALED_FRACTION);
        final float matrixLastScale = model.mActiveIconScaleBy
                * (mIsScaled ? lastInterpolation : (MAX_FRACTION - NON_SCALED_FRACTION));

        // Get title alpha relative to interpolation
        final int titleAlpha = (int) (MAX_ALPHA * interpolation);
        final int titleLastAlpha = MAX_ALPHA - (int) (MAX_ALPHA * lastInterpolation);
        // Get title scale relative to interpolation
        final float titleScale = MAX_FRACTION
                + ((mIsScaled ? interpolation : NON_SCALED_FRACTION) * TITLE_ACTIVE_SCALE_BY);
        final float titleLastScale = mIsScaled
                ? (MAX_FRACTION + TITLE_ACTIVE_SCALE_BY) - (lastInterpolation * TITLE_ACTIVE_SCALE_BY)
                : titleScale;

        mIconPaint.setAlpha(MAX_ALPHA);
        if (model.mSelectedIcon != null)
            mSelectedIconPaint.setAlpha(MAX_ALPHA);

        // Check if we handle models from touch on NTB or from ViewPager
        // There is a strange logic
        // of ViewPager onPageScrolled method, so it is
        if (mIsSetIndexFromTabBar) {
            if (mIndex == i)
                updateCurrentModel(model, leftOffset, topOffset, titleTranslate, interpolation, matrixCenterX,
                        matrixCenterY, matrixScale, titleScale, titleAlpha);
            else if (mLastIndex == i)
                updateLastModel(model, leftOffset, topOffset, titleTranslate, lastInterpolation, matrixCenterX,
                        matrixCenterY, matrixLastScale, titleLastScale, titleLastAlpha);
            else
                updateInactiveModel(model, leftOffset, topOffset, titleScale, matrixScale, matrixCenterX,
                        matrixCenterY);
        } else {
            if (i == mIndex + 1)
                updateCurrentModel(model, leftOffset, topOffset, titleTranslate, interpolation, matrixCenterX,
                        matrixCenterY, matrixScale, titleScale, titleAlpha);
            else if (i == mIndex)
                updateLastModel(model, leftOffset, topOffset, titleTranslate, lastInterpolation, matrixCenterX,
                        matrixCenterY, matrixLastScale, titleLastScale, titleLastAlpha);
            else
                updateInactiveModel(model, leftOffset, topOffset, titleScale, matrixScale, matrixCenterX,
                        matrixCenterY);
        }

        // Draw original model icon
        if (model.mSelectedIcon == null) {
            if (model.mIcon != null && !model.mIcon.isRecycled())
                mIconsCanvas.drawBitmap(model.mIcon, model.mIconMatrix, mIconPaint);
        } else {
            if (mIconPaint.getAlpha() != MIN_ALPHA && model.mIcon != null && !model.mIcon.isRecycled())
                // Draw original icon when is visible
                mIconsCanvas.drawBitmap(model.mIcon, model.mIconMatrix, mIconPaint);
        }
        // Draw selected icon when exist and visible
        if (mSelectedIconPaint.getAlpha() != MIN_ALPHA && model.mSelectedIcon != null
                && !model.mSelectedIcon.isRecycled())
            mIconsCanvas.drawBitmap(model.mSelectedIcon, model.mIconMatrix, mSelectedIconPaint);

        if (mIsTitled)
            mTitlesCanvas.drawText(isInEditMode() ? PREVIEW_TITLE : model.getTitle(), leftTitleOffset,
                    topTitleOffset, mModelTitlePaint);
    }

    // Reset pointer bounds for icons and titles
    if (mIsHorizontalOrientation)
        mPointerBounds.set(mPointerLeftTop, 0.0F, mPointerRightBottom, mBounds.height());
    if (mCornersRadius == 0) {
        if (mIsTinted)
            mIconsCanvas.drawRect(mPointerBounds, mIconPointerPaint);
        if (mIsTitled)
            mTitlesCanvas.drawRect(mPointerBounds, mIconPointerPaint);
    } else {
        if (mIsTinted)
            mIconsCanvas.drawRoundRect(mPointerBounds, mCornersRadius, mCornersRadius, mIconPointerPaint);
        if (mIsTitled)
            mTitlesCanvas.drawRoundRect(mPointerBounds, mCornersRadius, mCornersRadius, mIconPointerPaint);
    }

    // Draw general bitmap
    canvas.drawBitmap(mBitmap, 0.0F, 0.0F, null);
    // Draw icons bitmap on top
    canvas.drawBitmap(mIconsBitmap, 0.0F, barBadgeMargin, null);
    // Draw titles bitmap on top
    if (mIsTitled)
        canvas.drawBitmap(mTitlesBitmap, 0.0F, barBadgeMargin, null);

    // If is not badged, exit
    if (!mIsBadged)
        return;

    // Model badge margin and offset relative to gravity mode
    final float modelBadgeMargin = mBadgeGravity == BadgeGravity.TOP ? mBadgeMargin : mBounds.height();
    final float modelBadgeOffset = mBadgeGravity == BadgeGravity.TOP ? 0.0F : mBounds.height() - mBadgeMargin;

    for (int i = 0; i < mModels.size(); i++) {
        final Model model = mModels.get(i);

        // Set preview badge title
        if (isInEditMode() || TextUtils.isEmpty(model.getBadgeTitle()))
            model.setBadgeTitle(PREVIEW_BADGE);

        // Set badge title bounds
        mBadgePaint.setTextSize(mBadgeTitleSize * model.mBadgeFraction);
        mBadgePaint.getTextBounds(model.getBadgeTitle(), 0, model.getBadgeTitle().length(), mBadgeBounds);

        // Get horizontal and vertical padding for bg
        final float horizontalPadding = mBadgeTitleSize * BADGE_HORIZONTAL_FRACTION;
        final float verticalPadding = horizontalPadding * BADGE_VERTICAL_FRACTION;

        // Set horizontal badge offset
        final float badgeBoundsHorizontalOffset = (mModelSize * i)
                + (mModelSize * mBadgePosition.mPositionFraction);

        // If is badge title only one char, so create circle else round rect
        final float badgeMargin = mBadgeMargin * model.mBadgeFraction;
        if (model.getBadgeTitle().length() == 1) {
            mBgBadgeBounds.set(badgeBoundsHorizontalOffset - badgeMargin, modelBadgeMargin - badgeMargin,
                    badgeBoundsHorizontalOffset + badgeMargin, modelBadgeMargin + badgeMargin);
        } else
            mBgBadgeBounds.set(
                    badgeBoundsHorizontalOffset
                            - Math.max(badgeMargin, mBadgeBounds.centerX() + horizontalPadding),
                    modelBadgeMargin - badgeMargin,
                    badgeBoundsHorizontalOffset
                            + Math.max(badgeMargin, mBadgeBounds.centerX() + horizontalPadding),
                    modelBadgeOffset + (verticalPadding * 2.0F) + mBadgeBounds.height());

        // Set color and alpha for badge bg
        if (model.mBadgeFraction == MIN_FRACTION)
            mBadgePaint.setColor(Color.TRANSPARENT);
        else
            mBadgePaint.setColor(mBadgeBgColor == AUTO_COLOR ? mActiveColor : mBadgeBgColor);
        mBadgePaint.setAlpha((int) (MAX_ALPHA * model.mBadgeFraction));

        // Set corners to round rect for badge bg and draw
        final float cornerRadius = mBgBadgeBounds.height() * 0.5F;
        canvas.drawRoundRect(mBgBadgeBounds, cornerRadius, cornerRadius, mBadgePaint);

        // Set color and alpha for badge title
        if (model.mBadgeFraction == MIN_FRACTION)
            mBadgePaint.setColor(Color.TRANSPARENT);
        else //noinspection ResourceAsColor
            mBadgePaint.setColor(mBadgeTitleColor == AUTO_COLOR ? model.getColor() : mBadgeTitleColor);
        mBadgePaint.setAlpha((int) (MAX_ALPHA * model.mBadgeFraction));

        // Set badge title center position and draw title
        final float badgeHalfHeight = mBadgeBounds.height() * 0.5F;
        float badgeVerticalOffset = (mBgBadgeBounds.height() * 0.5F) + badgeHalfHeight - mBadgeBounds.bottom
                + modelBadgeOffset;
        canvas.drawText(model.getBadgeTitle(), badgeBoundsHorizontalOffset,
                badgeVerticalOffset + mBadgeBounds.height() - (mBadgeBounds.height() * model.mBadgeFraction),
                mBadgePaint);
    }
}