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.astuetz.PagerSlidingTabStripMy.java

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

    if (isInEditMode() || tabCount == 0) {
        return;//from   w w w.j a  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);
    }

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

    // draw underline

    //
    Path path = new Path();
    float len = 30;

    //????
    float x1 = lineLeft + (lineRight - lineLeft) / 2;
    float y1 = height - indicatorHeight;
    float x2 = x1 + len / 2;
    float y2 = height;
    float x3 = x1 - len / 2;
    float y3 = height;

    path.moveTo(x1, y1);
    path.lineTo(x2, y3);
    path.lineTo(x3, y3);
    path.lineTo(x1, y1);
    canvas.drawPath(path, rectPaint);

    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.zhongsou.souyue.ui.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;//from w w  w  .j  av  a2s  . 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();

    for (int i = 0; i < tabCount; i++) {
        View v = tabsContainer.getChildAt(i);
        v.setBackgroundResource(tabBackgroundResId);
        if (i == currentPosition) {
            if (v instanceof TextView) {
                TextView tab = (TextView) v;
                tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
                tab.setTextColor(textColor);
            }
        } else {
            if (v instanceof TextView) {
                TextView tab = (TextView) v;
                tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSize);
                tab.setTextColor(tabTextColor);
            }
        }
    }

    // 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);
        canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint);
    }
}

From source file:com.example.helloandroid.animation.PagerSlidingTabStrip.java

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

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

    final int height = getHeight();

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

    Utility.logd("onDraw : currentPosition = " + currentPosition);
    // 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
    Utility.logd("currentPositionOffset = " + currentPositionOffset);
    Utility.logd("tabCount = " + tabCount);

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

    Utility.logd("lineLeft = " + lineLeft + ", indicatorHeight = " + indicatorHeight + ", " + "lineRight = "
            + lineRight + ", height=" + height);

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

    // draw underline
    Utility.logd("height=" + height + ", underlineHeight = " + underlineHeight + ", "
            + "tabsContainer.getWidth() = " + tabsContainer.getWidth());

    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.xiaosu.lib.base.widget.drawerLayout.DrawerLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final boolean drawingContent = child == mContentView;
    int clipBottom = mDrawerView.getTop();

    final int restoreCount = canvas.save();
    if (drawingContent) {
        canvas.clipRect(0, 0, getWidth(), clipBottom);
    }// www  .jav a  2 s  .c o m
    final boolean result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(restoreCount);

    if (mScrimOpacity > 0 && drawingContent) {
        final int baseAlpha = (mScrimColor & 0xff000000) >>> 24;
        final int imag = (int) (baseAlpha * mScrimOpacity);
        final int color = imag << 24 | (mScrimColor & 0xffffff);
        mScrimPaint.setColor(color);

        canvas.drawRect(0, 0, getWidth(), clipBottom, mScrimPaint);
    }
    return result;
}

From source file:com.astuetz.PagerSlidingTabStripExtends.java

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

    if (isInEditMode() || tabCount == 0) {
        return;// w  ww .j av a2 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);
    }
    //indicator
    //        canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);
    Path path = new Path();
    float x1 = (lineRight + lineLeft) / 2;
    float y1 = height - indicatorHeight;
    //
    int triangleWidth = (int) ((lineRight - lineLeft) * 0.40f);
    float x2 = x1 + triangleWidth / 2;
    float y2 = height;
    float x3 = x1 - triangleWidth / 2;
    float y3 = height;

    path.moveTo(x1, y1);
    path.lineTo(x2, y2);
    path.lineTo(x3, y3);
    path.lineTo(x1, y1);
    canvas.drawPath(path, 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.astuetz.PagerSlidingTriangleStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;/*from  w ww .j av a2s . 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, 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);

    //
    float x1 = (lineRight + lineLeft) / 2;
    float y1 = height - indicatorHeight;//
    float x2 = x1 - triangleWidth / 2;
    float y2 = height;//
    float x3 = x1 + triangleWidth / 2;
    float y3 = height;//

    Path path = new Path();
    path.moveTo(x1, y1);
    path.lineTo(x2, y2);
    path.lineTo(x3, y3);
    path.lineTo(x1, y1);

    //
    //        rectPaint.setStyle(Style.STROKE);
    //        rectPaint.setStrokeWidth(2);

    canvas.drawPath(path, 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.viewpagerindicator.SquarePageIndicator.java

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

    if (mViewPager == null) {
        return;//from  www  .  j  av a2  s .  c o m
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        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 * 3;
    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.drawRect(dX, dY, dX + 2 * pageFillRadius, dY + 2 * pageFillRadius, mPaintPageFill);
        }

        // Only paint stroke if a stroke width was non-zero
        if (pageFillRadius != mRadius) {
            canvas.drawRect(dX, dY, dX + 2 * mRadius, dY + 2 * mRadius, 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(dX, dY, dX + 2 * mRadius, dY + 2 * mRadius, mPaintFill);
}

From source file:com.gx.appstore.lib.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;/*from w  w  w  . j  a va2s. 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, 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);
    }

    // 
    // 
    /**
     x1 = lineLeft+(lineRight-lineLeft)/2;
     y1 = height - indicatorHeight;
            
     x2 = x1 + triangleWidth/2;
     y2 = height;
            
     x3 = x1 - triangleWidth/2;
     y3 = height
     */
    Path path = new Path();
    float x1 = lineLeft + (lineRight - lineLeft) / 2;
    float y1 = height - indicatorHeight;

    int triangleWidth = 30;
    float x2 = x1 + triangleWidth / 2;
    float y2 = height;

    float x3 = x1 - triangleWidth / 2;
    float y3 = height;

    path.moveTo(x1, y1);
    path.lineTo(x2, y2);
    path.lineTo(x3, y3);
    path.lineTo(x1, y1);

    //        canvas.drawPath(path, 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);
        canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint);
    }
}

From source file:com.astuetz.PagerSlidingTrilateralStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;//w  w  w . j av  a 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);
    }
    /*modify by HJ begin ??*/
    //
    //canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);
    // ->
    float x1 = (lineLeft + lineRight) / 2;
    float y1 = height - indicatorHeight;

    float x2 = x1 - triangleWidth / 2;
    float y2 = height;

    float x3 = x1 + triangleWidth / 2;
    float y3 = height;

    Path path = new Path();
    path.moveTo(x1, y1);//
    path.lineTo(x2, y2);//2
    path.lineTo(x3, y3);//3
    path.lineTo(x1, y1);//1

    canvas.drawPath(path, rectPaint);

    /*modify by HJ end ??*/
    // 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.ring.widget.PagerSlidingTabStrip.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (disableViewPager)
        return;/*from ww  w .ja  va 2  s.  co m*/

    /* ? */
    ViewGroup tabsLayout = getTabsLayout();
    int tabCount = tabsLayout.getChildCount();
    if (tabsLayout == null || tabCount == 0) {
        return;
    }

    View currentTab = tabsLayout.getChildAt(currentPosition);
    if (currentTab == null) {
        return;
    }
    int height = getHeight();
    float slidingBlockLeft = currentTab.getLeft();
    float slidingBlockRight = currentTab.getRight();
    if (currentPositionOffset > 0f && currentPosition < tabsLayout.getChildCount() - 1) {
        View nextTab = tabsLayout.getChildAt(currentPosition + 1);
        if (nextTab != null) {
            final float nextTabLeft = nextTab.getLeft();
            final float nextTabRight = nextTab.getRight();
            slidingBlockLeft = (currentPositionOffset * nextTabLeft
                    + (1f - currentPositionOffset) * slidingBlockLeft);
            slidingBlockRight = (currentPositionOffset * nextTabRight
                    + (1f - currentPositionOffset) * slidingBlockRight);
        }
    }
    if (slidingBlockDrawable != null) {
        // ?
        if (disableTensileSlidingBlock) {
            int center = (int) (slidingBlockLeft + (slidingBlockRight - slidingBlockLeft) / 2);
            slidingBlockLeft = center - slidingBlockDrawable.getIntrinsicWidth() / 2;
            slidingBlockRight = center + slidingBlockDrawable.getIntrinsicWidth() / 2;
        }

        slidingBlockDrawable.setBounds((int) slidingBlockLeft,
                getHeight() - slidingBlockDrawable.getIntrinsicHeight(), (int) slidingBlockRight, getHeight());
        slidingBlockDrawable.draw(canvas);
    } else {

        rectPaint.setColor(barColor);

        canvas.drawRect(slidingBlockLeft, height - barHeight, slidingBlockRight, height, rectPaint);
        rectPaint.setColor(underlineColor);
        canvas.drawRect(0, height - underLineHeight, tabsLayout.getWidth(), height, rectPaint);

    }
    if (isAllowDivider) {
        // draw divider

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