Example usage for android.graphics Canvas drawLine

List of usage examples for android.graphics Canvas drawLine

Introduction

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

Prototype

public void drawLine(float startX, float startY, float stopX, float stopY, @NonNull Paint paint) 

Source Link

Document

Draw a line segment with the specified start and stop x,y coordinates, using the specified paint.

Usage

From source file:com.wangbb.naruto.app.view.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;/*w w w  . j  a  va  2s .  co  m*/
    }

    final int height = getHeight();

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

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

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

    // 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() + iLinePadding;
        final float nextTabRight = nextTab.getRight() - iLinePadding;

        lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft);
        lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight);
    }

    canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, 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.base.indicator.PagerIndicator.java

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

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

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

    if (shouldSameWidth) {
        canvas.drawRect(lineLeft + tabPadding, height - indicatorHeight, lineRight - tabPadding, height,
                rectPaint);
    } else {
        canvas.drawRect(lineLeft + tabPadding / 2, height - indicatorHeight, lineRight - tabPadding / 2, 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.yktx.check.widget.OldPagerSlidingTabStrip.java

@SuppressLint("DrawAllocation")
@Override//  w ww .j  a  v  a2 s.com
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (isInEditMode() || tabCount == 0) {
        return;
    }

    final int height = getHeight();

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

    // 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);
    // Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
    // R.drawable.xinjian_tuijian_huakuai);
    // canvas.drawBitmap(bitmap, lineLeft, height - indicatorHeight,
    // 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.witmoon.xmb.ui.widget.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;//from  w  w w. j av  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, 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:za.jamie.soundstage.widgets.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;//w  w w . j a va 2 s  . co m
    }

    final int height = getHeight();

    // default: line below current tab
    TextView currentTab = (TextView) 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) {
        TextView nextTab = (TextView) 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 currentTabColor = (Integer) tabTextColorEvaluator.evaluate(currentPositionOffset,
                tabSelectedTextColor, tabTextColor);
        currentTab.setTextColor(currentTabColor);

        int nextTabColor = (Integer) tabTextColorEvaluator.evaluate(currentPositionOffset, tabTextColor,
                tabSelectedTextColor);
        nextTab.setTextColor(nextTabColor);
    }

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

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

    // draw divider
    for (int i = 0; i < tabCount - 1; i++) {
        final int tabRight = tabsContainer.getChildAt(i).getRight();
        canvas.drawLine(tabRight, dividerPadding, tabRight, height - dividerPadding, dividerPaint);
    }
}

From source file:net.droidsolutions.droidcharts.core.renderer.AbstractCategoryItemRenderer.java

/**
 * Draws a marker for the range axis./*from   w w  w. jav a  2s.co  m*/
 * 
 * @param g2
 *            the graphics device (not <code>null</code>).
 * @param plot
 *            the plot (not <code>null</code>).
 * @param axis
 *            the range axis (not <code>null</code>).
 * @param marker
 *            the marker to be drawn (not <code>null</code>).
 * @param dataArea
 *            the area inside the axes (not <code>null</code>).
 * 
 * @see #drawDomainMarker(Graphics2D, CategoryPlot, CategoryAxis,
 *      CategoryMarker, Rectangle2D)
 */
public void drawRangeMarker(Canvas g2, CategoryPlot plot, ValueAxis axis, Marker marker, Rectangle2D dataArea) {

    if (marker instanceof ValueMarker) {
        ValueMarker vm = (ValueMarker) marker;
        double value = vm.getValue();
        Range range = axis.getRange();

        if (!range.contains(value)) {
            return;
        }

        PlotOrientation orientation = plot.getOrientation();
        double v = axis.valueToJava2D(value, dataArea, plot.getRangeAxisEdge());
        Line2D line = null;
        if (orientation == PlotOrientation.HORIZONTAL) {
            line = new Line2D.Double(v, dataArea.getMinY(), v, dataArea.getMaxY());
        } else if (orientation == PlotOrientation.VERTICAL) {
            line = new Line2D.Double(dataArea.getMinX(), v, dataArea.getMaxX(), v);
        }

        Paint p = marker.getPaint();
        p.setAlpha(vm.getAlpha());
        p.setStrokeWidth(marker.getStroke());
        g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(), (float) line.getY2(), p);

        String label = marker.getLabel();
        RectangleAnchor anchor = marker.getLabelAnchor();
        if (label != null) {
            Font labelFont = marker.getLabelFont();
            // g2.setFont(labelFont);

            Point2D coordinates = calculateRangeMarkerTextAnchorPoint(g2, orientation, dataArea,
                    line.getBounds2D(), marker.getLabelOffset(), LengthAdjustmentType.EXPAND, anchor);
            TextUtilities.drawAlignedString(label, g2, (float) coordinates.getX(), (float) coordinates.getY(),
                    marker.getLabelTextAnchor(), marker.getLabelPaint());
        }
        // g2.setComposite(savedComposite);
        p.setAlpha(255);
    } else if (marker instanceof IntervalMarker) {
        IntervalMarker im = (IntervalMarker) marker;
        double start = im.getStartValue();
        double end = im.getEndValue();
        Range range = axis.getRange();
        if (!(range.intersects(start, end))) {
            return;
        }

        double start2d = axis.valueToJava2D(start, dataArea, plot.getRangeAxisEdge());
        double end2d = axis.valueToJava2D(end, dataArea, plot.getRangeAxisEdge());
        double low = Math.min(start2d, end2d);
        double high = Math.max(start2d, end2d);

        PlotOrientation orientation = plot.getOrientation();
        Rectangle2D rect = null;
        if (orientation == PlotOrientation.HORIZONTAL) {
            // clip left and right bounds to data area
            low = Math.max(low, dataArea.getMinX());
            high = Math.min(high, dataArea.getMaxX());
            rect = new Rectangle2D.Double(low, dataArea.getMinY(), high - low, dataArea.getHeight());
        } else if (orientation == PlotOrientation.VERTICAL) {
            // clip top and bottom bounds to data area
            low = Math.max(low, dataArea.getMinY());
            high = Math.min(high, dataArea.getMaxY());
            rect = new Rectangle2D.Double(dataArea.getMinX(), low, dataArea.getWidth(), high - low);
        }
        Paint p = marker.getPaint();
        p.setAlpha((int) marker.getAlpha() * 100);// TODO ALPHA
        p.setStyle(Paint.Style.FILL);

        g2.drawRect((float) rect.getMinX(), (float) rect.getMinY(), (float) rect.getMaxX(),
                (float) rect.getMinY(), p);

        // now draw the outlines, if visible...
        if (im.getOutlinePaint() != null && im.getOutlineStroke() != 0.0f) {
            if (orientation == PlotOrientation.VERTICAL) {
                Line2D line = new Line2D.Double();
                double x0 = dataArea.getMinX();
                double x1 = dataArea.getMaxX();
                Paint paint = im.getOutlinePaint();
                paint.setStrokeWidth(im.getOutlineStroke());

                if (range.contains(start)) {
                    line.setLine(x0, start2d, x1, start2d);
                    g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(),
                            (float) line.getY2(), paint);
                }
                if (range.contains(end)) {
                    line.setLine(x0, end2d, x1, end2d);
                    g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(),
                            (float) line.getY2(), paint);
                }
            } else { // PlotOrientation.HORIZONTAL
                Line2D line = new Line2D.Double();
                double y0 = dataArea.getMinY();
                double y1 = dataArea.getMaxY();
                Paint paint = im.getOutlinePaint();
                paint.setStrokeWidth(im.getOutlineStroke());
                if (range.contains(start)) {
                    line.setLine(start2d, y0, start2d, y1);
                    g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(),
                            (float) line.getY2(), paint);
                }
                if (range.contains(end)) {
                    line.setLine(end2d, y0, end2d, y1);
                    g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(),
                            (float) line.getY2(), paint);
                }
            }
        }

        String label = marker.getLabel();
        RectangleAnchor anchor = marker.getLabelAnchor();
        if (label != null) {
            Font labelFont = marker.getLabelFont();
            // g2.setFont(labelFont);
            Paint pp = marker.getLabelPaint();
            // g2.setPaint(marker.getLabelPaint());
            Point2D coordinates = calculateRangeMarkerTextAnchorPoint(g2, orientation, dataArea, rect,
                    marker.getLabelOffset(), marker.getLabelOffsetType(), anchor);
            TextUtilities.drawAlignedString(label, g2, (float) coordinates.getX(), (float) coordinates.getY(),
                    marker.getLabelTextAnchor(), pp);
        }
        p.setAlpha(255);
    }
}

From source file:com.cssweb.android.view.KlineMini.java

private void drawMA(Canvas canvas, JSONObject quoteData, int begin, int count, double shapeWidth,
        double spaceWidth, double highPrice, double lowPrice, double highVolume, int actualDataLen)
        throws JSONException {
    if (quoteData == null)
        return;/*from  w  ww  .  j a  v  a2 s .co m*/

    int startX = klineX;
    int startY = klineY;
    double ma5Height = 0;
    double ma5x = 0;
    double ma5y = 0;
    double ma5 = 0;

    paint.setColor(GlobalColor.colorM5);
    for (int i = begin; i < (begin + count); i++) {
        //klineX = 0;

        ma5 = quoteData.getJSONArray("MA").getJSONArray(i).getDouble(1);

        if ((i - begin) == 0) {
            ma5x = startX + spaceWidth + shapeWidth / 2;
            ma5Height = (int) ((ma5 - lowPrice) * scale);
            ma5y = axisLabelHeight + klineHeight - ma5Height;
        } else {
            if (quoteData.getJSONArray("MA").getJSONArray(i - 1).getDouble(1) == 0) {
                ma5x = ma5x + spaceWidth + shapeWidth;
                ma5Height = (ma5 - lowPrice) * scale;
                ma5y = startY + klineHeight - ma5Height;
            } else {
                canvas.drawLine((float) ma5x, (float) ma5y, (float) (ma5x + spaceWidth + shapeWidth),
                        (float) (axisLabelHeight + klineHeight - (ma5 - lowPrice) * scale), paint);
                ma5x = ma5x + spaceWidth + shapeWidth;
                ma5Height = (ma5 - lowPrice) * scale;
                ma5y = startY + klineHeight - ma5Height;
            }
        }

    } // end for             

    double ma10x = 0;
    double ma10y = 0;
    double ma10Height = 0;
    double ma10 = 0;

    paint.setColor(GlobalColor.colorM10);

    for (int i = begin; i < (begin + count); i++) {
        //klineX = 0;

        ma10 = quoteData.getJSONArray("MA").getJSONArray(i).getDouble(2);

        if ((i - begin) == 0) {
            ma10x = startX + spaceWidth + shapeWidth / 2;
            ma10Height = (ma10 - lowPrice) * scale;
            ma10y = startY + klineHeight - ma10Height;
        } else {
            if (quoteData.getJSONArray("MA").getJSONArray(i - 1).getDouble(2) == 0) {
                ma10x = ma10x + spaceWidth + shapeWidth;
                ma10Height = (ma10 - lowPrice) * scale;
                ma10y = startY + klineHeight - ma10Height;
            } else {
                canvas.drawLine((float) ma10x, (float) ma10y, (float) (ma10x + spaceWidth + shapeWidth),
                        (float) (axisLabelHeight + klineHeight - (ma10 - lowPrice) * scale), paint);
                ma10x = ma10x + spaceWidth + shapeWidth;
                ma10Height = (ma10 - lowPrice) * scale;
                ma10y = startY + klineHeight - ma10Height;
            }
        }
    } // end for          

    double ma20x = 0;
    double ma20y = 0;
    double ma20Height = 0;
    double ma20 = 0;

    //paint.setColor(GlobalColor.colorM20);
    paint.setARGB(255, 255, 0, 255);
    for (int i = begin; i < (begin + count); i++) {
        //klineX = 0;

        ma20 = quoteData.getJSONArray("MA").getJSONArray(i).getDouble(3);

        if ((i - begin) == 0) {
            ma20x = startX + spaceWidth + shapeWidth / 2;
            ma20Height = (ma20 - lowPrice) * scale;
            ma20y = startY + klineHeight - ma20Height;
        } else {
            if (quoteData.getJSONArray("MA").getJSONArray(i - 1).getDouble(3) == 0) {
                ma20x = ma20x + spaceWidth + shapeWidth;
                ma20Height = (ma20 - lowPrice) * scale;
                ma20y = startY + klineHeight - ma20Height;
            } else {
                canvas.drawLine((float) ma20x, (float) ma20y, (float) (ma20x + spaceWidth + shapeWidth),
                        (float) (axisLabelHeight + klineHeight - (ma20 - lowPrice) * scale), paint);
                ma20x = ma20x + spaceWidth + shapeWidth;
                ma20Height = (ma20 - lowPrice) * scale;
                ma20y = startY + klineHeight - ma20Height;
            }
        }
    } // end for                                  

    double ma60x = 0;
    double ma60y = 0;
    double ma60Height = 0;
    double ma60 = 0;

    paint.setColor(GlobalColor.colorM60);
    for (int i = begin; i < (begin + count); i++) {
        //klineX = 0;

        ma60 = quoteData.getJSONArray("MA").getJSONArray(i).getDouble(4);

        if ((i - begin) == 0) {
            ma60x = startX + spaceWidth + shapeWidth / 2;
            ma60Height = (ma60 - lowPrice) * scale;
            ma60y = startY + klineHeight - ma60Height;
        } else {
            if (quoteData.getJSONArray("MA").getJSONArray(i - 1).getDouble(4) == 0) {
                ma60x = ma60x + spaceWidth + shapeWidth;
                ma60Height = (ma60 - lowPrice) * scale;
                ma60y = startY + klineHeight - ma60Height;
            } else {
                canvas.drawLine((float) ma60x, (float) ma60y, (float) (ma60x + spaceWidth + shapeWidth),
                        (float) (axisLabelHeight + klineHeight - (ma60 - lowPrice) * scale), paint);
                ma60x = ma60x + spaceWidth + shapeWidth;
                ma60Height = (ma60 - lowPrice) * scale;
                ma60y = startY + klineHeight - ma60Height;
            }
        }
    } // end for          
}

From source file:zz.itcast.jiujinhui.activity.PagerSlidingTabStrip.java

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

    if (isInEditMode() || tabCount == 0) {
        return;/*from  w w  w. jav a2s .  c o m*/
    }

    final int height = getHeight();

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

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

    if (maxLineWidth != 0 && lineRight - lineLeft > maxLineWidth) {

        canvas.drawRect(lineLeft + (lineRight - lineLeft - maxLineWidth) / 2, height - indicatorHeight,
                lineRight - (lineRight - lineLeft - maxLineWidth) / 2, height - underlineMarginTop, rectPaint);

    } else {

        canvas.drawRect(lineLeft, height - indicatorHeight, lineRight - underlineMarginTop, 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.cs.widget.tab.PagerSlidingTabStrip.java

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

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

    if (lineGravity == 1) {//tab
        canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);

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

    } else {//tab
        canvas.drawRect(lineLeft, 0, lineRight, indicatorHeight, rectPaint);

        // draw underline
        rectPaint.setColor(underlineColor);
        canvas.drawRect(0, 0, tabsContainer.getWidth(), underlineHeight, 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.itheima.oschina.ui.view.PagerSlidingTab.java

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

    if (isInEditMode() || tabCount == 0) {
        return;//  ww  w.j ava2s.  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);
    //      canvas.drawRect(lineLeft, 0, lineRight, height, rectPaint);
    //
    //      canvas.drawRect(lineLeft, 0, lineRight, indicatorHeight, rectPaint);
    //
    //      canvas.drawRect(lineLeft, height/2-indicatorHeight/2, lineRight, height/2+indicatorHeight/2, 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);
    }
}