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(float left, float top, float right, float bottom, @NonNull Paint paint) 

Source Link

Document

Draw the specified Rect using the specified paint.

Usage

From source file:com.pinthecloud.item.view.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;//from w w w. ja va  2 s .  co  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, start 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);
    }

    lineLeft += indicatorPadding;
    lineRight -= indicatorPadding;

    canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);

    // draw underline
    rectPaint.setColor(underlineColor);
    canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, 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:android.support.design.widget.ShadowDrawableWrapper.java

private void drawShadow(Canvas canvas) {
    final int rotateSaved = canvas.save();
    canvas.rotate(mRotation, mContentBounds.centerX(), mContentBounds.centerY());

    final float edgeShadowTop = -mCornerRadius - mShadowSize;
    final float shadowOffset = mCornerRadius;
    final boolean drawHorizontalEdges = mContentBounds.width() - 2 * shadowOffset > 0;
    final boolean drawVerticalEdges = mContentBounds.height() - 2 * shadowOffset > 0;

    final float shadowOffsetTop = mRawShadowSize - (mRawShadowSize * SHADOW_TOP_SCALE);
    final float shadowOffsetHorizontal = mRawShadowSize - (mRawShadowSize * SHADOW_HORIZ_SCALE);
    final float shadowOffsetBottom = mRawShadowSize - (mRawShadowSize * SHADOW_BOTTOM_SCALE);

    final float shadowScaleHorizontal = shadowOffset / (shadowOffset + shadowOffsetHorizontal);
    final float shadowScaleTop = shadowOffset / (shadowOffset + shadowOffsetTop);
    final float shadowScaleBottom = shadowOffset / (shadowOffset + shadowOffsetBottom);

    // LT//from w  ww .  ja v  a 2 s  .c  om
    int saved = canvas.save();
    canvas.translate(mContentBounds.left + shadowOffset, mContentBounds.top + shadowOffset);
    canvas.scale(shadowScaleHorizontal, shadowScaleTop);
    canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
    if (drawHorizontalEdges) {
        // TE
        canvas.scale(1f / shadowScaleHorizontal, 1f);
        canvas.drawRect(0, edgeShadowTop, mContentBounds.width() - 2 * shadowOffset, -mCornerRadius,
                mEdgeShadowPaint);
    }
    canvas.restoreToCount(saved);
    // RB
    saved = canvas.save();
    canvas.translate(mContentBounds.right - shadowOffset, mContentBounds.bottom - shadowOffset);
    canvas.scale(shadowScaleHorizontal, shadowScaleBottom);
    canvas.rotate(180f);
    canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
    if (drawHorizontalEdges) {
        // BE
        canvas.scale(1f / shadowScaleHorizontal, 1f);
        canvas.drawRect(0, edgeShadowTop, mContentBounds.width() - 2 * shadowOffset,
                -mCornerRadius + mShadowSize, mEdgeShadowPaint);
    }
    canvas.restoreToCount(saved);
    // LB
    saved = canvas.save();
    canvas.translate(mContentBounds.left + shadowOffset, mContentBounds.bottom - shadowOffset);
    canvas.scale(shadowScaleHorizontal, shadowScaleBottom);
    canvas.rotate(270f);
    canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
    if (drawVerticalEdges) {
        // LE
        canvas.scale(1f / shadowScaleBottom, 1f);
        canvas.drawRect(0, edgeShadowTop, mContentBounds.height() - 2 * shadowOffset, -mCornerRadius,
                mEdgeShadowPaint);
    }
    canvas.restoreToCount(saved);
    // RT
    saved = canvas.save();
    canvas.translate(mContentBounds.right - shadowOffset, mContentBounds.top + shadowOffset);
    canvas.scale(shadowScaleHorizontal, shadowScaleTop);
    canvas.rotate(90f);
    canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
    if (drawVerticalEdges) {
        // RE
        canvas.scale(1f / shadowScaleTop, 1f);
        canvas.drawRect(0, edgeShadowTop, mContentBounds.height() - 2 * shadowOffset, -mCornerRadius,
                mEdgeShadowPaint);
    }
    canvas.restoreToCount(saved);

    canvas.restoreToCount(rotateSaved);
}

From source file:com.lt.adamlee.aagame.GameView.java

public void about(Canvas c) {
    int textsize = (int) F.hf(20.0f);
    Paint p3 = new Paint();
    p3.setTypeface(tf);// w w  w  . ja  v a2s. c o  m
    p3.setColor(ViewCompat.MEASURED_STATE_MASK);
    p3.setFlags(1);
    p3.setTextSize((float) textsize);
    Paint pagepaint = new Paint();
    pagepaint.setColor(getResources().getColor(R.color.abc_secondary_text_material_light));
    c.drawRect(0.0f, 0.0f, (float) screenW, (float) screenH, pagepaint);
    p3.setTextSize(F.hf(30.0f));
    c.drawText(ctx.getString(R.string.about),
            ((float) (screenW / 2)) - (p3.measureText(ctx.getString(R.string.about)) / 2.0f),
            (float) (screenH / 4), p3);
    p3.setTextSize((float) textsize);
    c.drawText(ctx.getString(R.string.app_name),
            ((float) (screenW / 2)) - (p3.measureText(ctx.getString(R.string.app_name)) / 2.0f),
            (float) (((textsize * 2) + 1) + ((screenH - (textsize * 7)) / 2)), p3);
    c.drawText("License Version 1.7", ((float) (screenW / 2)) - (p3.measureText("License Version 1.7") / 2.0f),
            (float) (((textsize * 3) + 1) + ((screenH - (textsize * 7)) / 2)), p3);
    p3.setTextSize(F.hf(17.0f));
    c.drawText("Powered by Map Game Studio",
            ((float) (screenW / 2)) - (p3.measureText("Powered by Map Game Studio") / 2.0f),
            (float) (((textsize * 4) + 1) + ((screenH - (textsize * 7)) / 2)), p3);
    p3.setTextSize(F.hf(17.0f));
    c.drawText("Email:mapgamestudio@gmail.com",
            ((float) (screenW / 2)) - (p3.measureText("Email:mapgamestudio@gmail.com") / 2.0f),
            (float) (((textsize * 5) + 1) + ((screenH - (textsize * 7)) / 2)), p3);
}

From source file:com.witmoon.xmb.ui.widget.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;/* w w w . j a  v  a  2s. co  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, start 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);
    }

    canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);

    // draw underline

    rectPaint.setColor(underlineColor);
    canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);

    // draw divider

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

From source file:com.example.yh.myapplication.widget.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;/*from   www . ja va  2 s.com*/
    }

    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, start 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);
    }

    int marginH = 60;
    if (tabCount <= 2) {
        marginH = 140;
    }
    canvas.drawRect(lineLeft + marginH, height - indicatorHeight, lineRight - marginH, height, rectPaint);

    // draw underline

    rectPaint.setColor(underlineColor);
    canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, 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:com.xinwei.smallvideo.widget.PagerSlidingTabStrip.java

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

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

    final int height = getHeight();

    // draw indicator line
    rectPaint.setColor(indicatorColor);

    // ??TAB
    View currentTab = tabsContainer.getChildAt(currentPosition);
    // ??TAB???
    float lineLeft = currentTab.getLeft();
    float lineRight = currentTab.getRight();

    // if there is an offset, start 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);
    }

    // 
    canvas.drawRect(lineLeft, 0, lineRight, indicatorHeight, rectPaint);

    // 
    // canvas.drawRect(lineLeft, height - indicatorHeight, lineRight,
    // height,
    // rectPaint);

    // draw underline
    rectPaint.setColor(underlineColor);
    canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);

    // draw divider
    dividerPaint.setColor(dividerColor);
    for (int i = 0; i < tabCount - 1; i++) {
        View tab = tabsContainer.getChildAt(i);
        boolean isSelected = (i == currentPosition);
        tab.setSelected(isSelected);

        canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint);
    }
}

From source file:com.travel.ac.view.UnderlinePageIndicator.java

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

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

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

    final int paddingLeft = getPaddingLeft();
    final float pageWidth = (getWidth() - paddingLeft - getPaddingRight()) / (1f * count);
    final float left = paddingLeft + pageWidth * (mCurrentPage + mPositionOffset);

    final float textleft = paddingLeft + pageWidth;
    final float right = left + pageWidth;
    final float top = getHeight() - getPaddingBottom() - mTabLineHeight;
    final float title_bottom = getHeight() - getPaddingBottom() - mTabLineHeight;
    final float bottom = getHeight() - getPaddingBottom();
    if (mTitles != null && mTitles.length > 0) {
        for (int i = 0; i < mTitles.length; i++) {
            if (mCurrentPage == i) {
                mTextPaint.setColor(Color.RED);
            } else {
                mTextPaint.setColor(Color.BLACK);
            }
            canvas.drawText(mTitles[i], (pageWidth * i) + pageWidth / 3 + 0.5f, (top + mTabLineHeight) / 2,
                    mTextPaint);
        }
    }

    canvas.drawRect(left, top, right, bottom, mPaint);
}

From source file:com.example.view.astuetz.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;//  w  w w  .j  a v  a 2s  . co  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, start 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);
    }

    // draw indicator line
    if (showUnderline == 0)
        canvas.drawRect(lineLeft, 0, lineRight, indicatorHeight, rectPaint);
    else
        canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);

    // draw underline

    rectPaint.setColor(underlineColor);
    if (showUnderline == 0)
        canvas.drawRect(0, 0, tabsContainer.getWidth(), underlineHeight, rectPaint);
    else
        canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, 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:com.vgaw.androidtest.view.SlidingTabStrip.java

@Override
protected void onDraw(Canvas canvas) {
    final int height = getHeight();
    final int childCount = getChildCount();
    final int dividerHeightPx = (int) (Math.min(Math.max(0f, mDividerHeight), 1f) * height);
    final TabColorizer tabColorizer = mCustomTabColorizer != null ? mCustomTabColorizer : mDefaultTabColorizer;

    // Thick colored underline below the current selection
    if (childCount > 0) {
        View selectedTitle = getChildAt(mSelectedPosition);
        int left = selectedTitle.getLeft();
        int right = selectedTitle.getRight();
        int color = tabColorizer.getIndicatorColor(mSelectedPosition);

        if (mSelectionOffset > 0f && mSelectedPosition < (getChildCount() - 1)) {
            int nextColor = tabColorizer.getIndicatorColor(mSelectedPosition + 1);
            if (color != nextColor) {
                color = blendColors(nextColor, color, mSelectionOffset);
            }//from  ww  w.j a v  a 2s  . c o m

            // Draw the selection partway between the tabs
            View nextTitle = getChildAt(mSelectedPosition + 1);
            left = (int) (mSelectionOffset * nextTitle.getLeft() + (1.0f - mSelectionOffset) * left);
            right = (int) (mSelectionOffset * nextTitle.getRight() + (1.0f - mSelectionOffset) * right);
        }

        mSelectedIndicatorPaint.setColor(color);

        canvas.drawRect(left, height - mSelectedIndicatorThickness, right, height, mSelectedIndicatorPaint);
    }

    // Thin underline along the entire bottom edge
    canvas.drawRect(0, height - mBottomBorderThickness, getWidth(), height, mBottomBorderPaint);

    // Vertical separators between the titles
    int separatorTop = (height - dividerHeightPx) / 2;
    for (int i = 0; i < childCount - 1; i++) {
        View child = getChildAt(i);
        mDividerPaint.setColor(tabColorizer.getDividerColor(i));
        canvas.drawLine(child.getRight(), separatorTop, child.getRight(), separatorTop + dividerHeightPx,
                mDividerPaint);
    }
}

From source file:com.robert.library.view.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;/*from   w w  w . ja  v  a  2 s  . c  om*/
    }

    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, start 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);
    }

    // 1. ??
    //      canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);
    // ??
    canvas.drawRect(lineLeft, 3 * underlineHeight, lineRight, height - 3 * underlineHeight, rectPaint);

    // draw underline

    rectPaint.setColor(underlineColor);
    //      canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);
    // 2. ???
    canvas.drawRect(0, 0, tabsContainer.getWidth(), 3 * underlineHeight, rectPaint);
    canvas.drawRect(0, height - 3 * underlineHeight, tabsContainer.getWidth(), height, rectPaint);

    // 3. draw divider??
    dividerPaint.setColor(dividerColor);
    //        dividerPaint.setColor(R.color.red);
    for (int i = 0; i < tabCount - 1; i++) {
        View tab = tabsContainer.getChildAt(i);
        canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint);
        //            canvas.drawRect(tab.getRight(), 3 * underlineHeight, tab.getRight(), height - 3 * underlineHeight, dividerPaint);
    }
}