Example usage for android.graphics Canvas drawText

List of usage examples for android.graphics Canvas drawText

Introduction

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

Prototype

public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint) 

Source Link

Document

Draw the text, with origin at (x,y), using the specified paint.

Usage

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

public void drawHKPrice(Canvas canvas) {
    //canvas.restore();
    Paint paint = this.mPaint;
    tPaint.setColor(GlobalColor.clrLine);
    tPaint.setStyle(Paint.Style.STROKE);
    tPaint.setStrokeWidth(1);/*from w  ww. ja v  a  2 s . c o  m*/
    this.x = 0;
    this.y = 0;
    startX = x + tips / 2;
    endX = width - tips / 2;
    canvas.drawRect(startX, DY / 4, endX, height - DY / 4, tPaint);
    //      canvas.drawLine(startX, DY+DY/4, endX, DY+DY/4, tPaint);
    canvas.drawLine(startX, DY * 5 + DY / 4, endX, DY * 5 + DY / 4, tPaint);
    canvas.drawLine(startX, DY * 10 + DY / 4, endX, DY * 10 + DY / 4, tPaint);
    canvas.drawLine(startX, DY * 16 + DY / 4, endX, DY * 16 + DY / 4, tPaint);
    if (quoteData != null) {
        try {
            JSONArray jArr = quoteData.getJSONArray("data");
            JSONObject jo = jArr.getJSONObject(0);
            String str = "";
            double zrsp = jo.getDouble("zrsp");

            canvas.translate(0, DY);

            paint.setTypeface(Typeface.DEFAULT_BOLD);
            paint.setAntiAlias(true);
            paint.setTextSize(mTextSize);
            //            paint.setTextAlign(Paint.Align.LEFT);
            //            paint.setColor(GlobalColor.colorLabelName);
            //            canvas.drawText("", x+tips, y, paint);
            //
            //            paint.setTextAlign(Paint.Align.RIGHT);
            //            canvas.translate(width/2, 0);
            //            if(jo.getDouble("wb")<0)
            //               paint.setColor(GlobalColor.colorPriceDown);
            //            else if(jo.getDouble("wb")>0)
            //               paint.setColor(GlobalColor.colorpriceUp);
            //            else 
            //               paint.setColor(GlobalColor.colorPriceEqual);
            //            canvas.drawText(Utils.dataFormation(jo.getDouble("wb")*100, 1)+"%", x, y, paint);
            //            
            //            paint.setTextAlign(Paint.Align.LEFT);
            //            paint.setColor(GlobalColor.colorLabelName);
            //            canvas.drawText("", x+tips, y, paint);
            //
            //            paint.setTextAlign(Paint.Align.RIGHT);
            //            canvas.translate(width/2, 0);
            //            if(jo.getDouble("wc")<0) {
            //               paint.setColor(GlobalColor.colorPriceDown);
            //               canvas.drawText("-" + Utils.getAmountFormat(Math.abs(jo.getDouble("wc")), true), x-tips, y, paint);
            //            }
            //            else if(jo.getDouble("wc")>0) {
            //               paint.setColor(GlobalColor.colorpriceUp);
            //               canvas.drawText(Utils.getAmountFormat(jo.getDouble("wc"), true), x-tips, y, paint);
            //            }
            //            else {
            //               paint.setColor(GlobalColor.colorPriceEqual);
            //               canvas.drawText(Utils.getAmountFormat(jo.getDouble("wc"), true), x-tips, y, paint);
            //            }
            //            
            //            canvas.translate(-width, DY);

            paint.setTextAlign(Paint.Align.LEFT);
            paint.setColor(GlobalColor.colorLabelName);
            canvas.drawText("?", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x + tips, y, paint);

            paint.setTextAlign(Paint.Align.RIGHT);
            canvas.translate(width / 2, -DY * 4);
            for (int i = 5; i >= 1; i--) {
                double temp2 = jo.getDouble("sjw" + i);
                setColor(paint, temp2, zrsp);
                str = Utils.dataFormation(temp2, stockdigit, jo.getInt("tp"));
                canvas.drawText(str, x, y, paint);
                if (i != 1)
                    canvas.translate(0, DY);
            }

            paint.setColor(GlobalColor.colorStockName);
            canvas.translate(width / 2, -DY * 4);
            for (int i = 5; i >= 1; i--) {
                paint.setTextAlign(Paint.Align.RIGHT);
                canvas.drawText(Utils.getAmountFormat(jo.getInt("ssl" + i), false), x - tips, y, paint);
                if (i != 1)
                    canvas.translate(0, DY);
            }

            canvas.translate(-width, DY);

            paint.setTextAlign(Paint.Align.LEFT);
            paint.setColor(GlobalColor.colorLabelName);

            canvas.drawText("", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x + tips, y, paint);

            canvas.translate(width / 2, -DY * 4);
            paint.setTextAlign(Paint.Align.RIGHT);
            for (int i = 1; i <= 5; i++) {
                double temp2 = jo.getDouble("bjw" + i);
                setColor(paint, temp2, zrsp);
                str = Utils.dataFormation(temp2, stockdigit, jo.getInt("tp"));
                canvas.drawText(str, x, y, paint);
                if (i != 5)
                    canvas.translate(0, DY);
            }

            paint.setColor(GlobalColor.colorStockName);
            canvas.translate(width / 2, -DY * 4);
            for (int i = 1; i <= 5; i++) {
                paint.setTextAlign(Paint.Align.RIGHT);
                canvas.drawText(Utils.getAmountFormat(jo.getInt("bsl" + i), false), x - tips, y, paint);
                if (i != 5)
                    canvas.translate(0, DY);
            }

            canvas.translate(-width, DY);

            paint.setTextAlign(Paint.Align.LEFT);
            paint.setColor(GlobalColor.colorLabelName);

            canvas.drawText("", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x + tips, y, paint);

            canvas.translate(width / 2, -DY * 6);

            paint.setTextAlign(Paint.Align.RIGHT);
            double zjcj = jo.getDouble("zjcj");
            setColor(paint, zjcj, zrsp);
            canvas.drawText(Utils.dataFormation(zjcj, stockdigit, jo.getInt("tp")), x, y, paint);

            canvas.translate(0, DY);
            double zhangd = jo.getDouble("zd");
            if (zhangd < 0) {
                paint.setColor(GlobalColor.colorPriceDown);
            } else if (zhangd > 0) {
                paint.setColor(GlobalColor.colorpriceUp);
            } else {
                paint.setColor(GlobalColor.colorPriceEqual);
            }
            String zhangdie = Utils.dataFormation(zhangd, stockdigit, jo.getInt("tp"));
            if (zhangdie.equals("-") || zhangdie.equals(""))
                canvas.drawText("", x, y, paint);
            else
                canvas.drawText(zhangdie, x, y, paint);

            double zhangf = jo.getDouble("zf");
            if (zhangf < 0) {
                paint.setColor(GlobalColor.colorPriceDown);
            } else if (zhangf > 0) {
                paint.setColor(GlobalColor.colorpriceUp);
            } else {
                paint.setColor(GlobalColor.colorPriceEqual);
            }
            canvas.translate(0, DY);
            String zhangfu = Utils.dataFormation(zhangf * 100, 1, jo.getInt("tp"));
            if (zhangfu.equals("-") || zhangfu.equals(""))
                canvas.drawText("", x, y, paint);
            else
                canvas.drawText(zhangfu + "%", x, y, paint);

            paint.setColor(GlobalColor.colorStockName);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getInt("cjsl"), false), x, y, paint);
            paint.setColor(GlobalColor.colorpriceUp);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("wp"), false), x, y, paint);
            paint.setColor(GlobalColor.colorStockName);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getInt("msgs"), true), x, y, paint);

            canvas.translate(0, -DY * 5);

            paint.setTextAlign(Paint.Align.LEFT);
            paint.setColor(GlobalColor.colorLabelName);

            canvas.drawText("", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x + tips, y, paint);
            //            canvas.drawText("?", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x + tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x + tips, y, paint);

            canvas.translate(width / 2, -DY * 5);

            paint.setTextAlign(Paint.Align.RIGHT);
            double jrkp = jo.getDouble("jrkp");
            setColor(paint, jrkp, zrsp);
            canvas.drawText(Utils.dataFormation(jrkp, stockdigit, jo.getInt("tp")), x - tips, y, paint);

            canvas.translate(0, DY);
            double zg = jo.getDouble("zgcj");
            setColor(paint, zg, zrsp);
            canvas.drawText(Utils.dataFormation(zg, stockdigit, jo.getInt("tp")), x - tips, y, paint);

            canvas.translate(0, DY);
            double zd = jo.getDouble("zdcj");
            setColor(paint, zd, zrsp);
            canvas.drawText(Utils.dataFormation(zd, stockdigit, jo.getInt("tp")), x - tips, y, paint);

            paint.setColor(GlobalColor.colorStockName);
            //            canvas.translate(0, DY);
            //            canvas.drawText(Utils.dataFormation(jo.getDouble("lb"), 1), x - tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("cjje"), false), x - tips, y, paint);

            paint.setColor(GlobalColor.colorPriceDown);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("np"), false), x - tips, y, paint);
        } catch (JSONException e) {
            Log.e(TAG, e.toString());
        }
    }
}

From source file:kr.jjoe64.graphview.series.TitleLineGraphSeries.java

@Override
public void draw(GraphView graphView, Canvas canvas, boolean isSecondScale) {
    paint.setColor(getColor());/* w  w w.  ja  v  a 2s.  c  o m*/
    paint.setStrokeWidth(thickness);
    paintBackground.setColor(backgroundColor);
    pathBackground.reset();

    resetDataPoints();

    // get data
    double maxX = graphView.getViewport().getMaxX(false);
    double minX = graphView.getViewport().getMinX(false);

    double maxY;
    double minY;
    if (isSecondScale) {
        maxY = graphView.getSecondScale().getMaxY(false);
        minY = graphView.getSecondScale().getMinY(false);
    } else {
        maxY = graphView.getViewport().getMaxY(false);
        minY = graphView.getViewport().getMinY(false);
    }

    // draw data
    double diffY = maxY - minY;
    double diffX = maxX - minX;

    float graphHeight = graphView.getGraphContentHeight();
    float graphWidth = graphView.getGraphContentWidth();
    float graphLeft = graphView.getGraphContentLeft();
    float graphTop = graphView.getGraphContentTop();

    // draw background
    double lastEndY = 0;
    double lastEndX = 0;

    double titleY = 0;

    double lastUsedEndX = 0;
    float firstX = 0;
    int i = 0;
    Iterator<E> values = getValues(minX, maxX);
    while (values.hasNext()) {
        E value = values.next();

        double valY = value.getY() - minY;
        double ratY = valY / diffY;
        double y = graphHeight * ratY;

        double valX = value.getX() - minX;
        double ratX = valX / diffX;
        double x = graphWidth * ratX;

        double orgX = x;
        double orgY = y;

        if (i > 0) {
            // overdraw
            if (x > graphWidth) { // end right
                double b = ((graphWidth - lastEndX) * (y - lastEndY) / (x - lastEndX));
                y = lastEndY + b;
                x = graphWidth;
            }
            if (y < 0) { // end bottom
                double b = ((0 - lastEndY) * (x - lastEndX) / (y - lastEndY));
                x = lastEndX + b;
                y = 0;
            }
            if (y > graphHeight) { // end top
                double b = ((graphHeight - lastEndY) * (x - lastEndX) / (y - lastEndY));
                x = lastEndX + b;
                y = graphHeight;
            }
            if (lastEndY < 0) { // start bottom
                double b = ((0 - y) * (x - lastEndX) / (lastEndY - y));
                lastEndX = x - b;
                lastEndY = 0;
            }
            if (lastEndX < 0) { // start left
                double b = ((0 - x) * (y - lastEndY) / (lastEndX - x));
                lastEndY = y - b;
                lastEndX = 0;
            }
            if (lastEndY > graphHeight) { // start top
                double b = ((graphHeight - y) * (x - lastEndX) / (lastEndY - y));
                lastEndX = x - b;
                lastEndY = graphHeight;
            }

            float startX = (float) lastEndX + (graphLeft + 1);
            float startY = (float) (graphTop - lastEndY) + graphHeight;
            float endX = (float) x + (graphLeft + 1);
            float endY = (float) (graphTop - y) + graphHeight;

            registerDataPoint(endX, endY, value);

            path.reset();
            path.moveTo(startX, startY);
            path.lineTo(endX, endY);
            canvas.drawPath(path, paint);
            if (i == 1) {
                firstX = startX;
                pathBackground.moveTo(startX, startY);
            }
            pathBackground.lineTo(endX, endY);
            lastUsedEndX = endX;
        }
        lastEndY = orgY;
        lastEndX = orgX;
        titleY = Math.max(titleY, orgY);
        i++;
    }

    pathBackground.lineTo((float) lastUsedEndX, graphHeight + graphTop);
    pathBackground.lineTo(firstX, graphHeight + graphTop);
    pathBackground.close();
    canvas.drawPath(pathBackground, paintBackground);

    if (StringUtils.isNotBlank(getTitle()) && lastUsedEndX > 0) {
        float x = (float) (lastUsedEndX + firstX) / 2;
        float y = (float) (graphTop - titleY) + graphHeight - 10;

        paintTitle.setColor(getColor());
        paintTitle.setTextSize(graphView.getLegendRenderer().getTextSize());
        paintTitle.setFakeBoldText(textBold);
        canvas.drawText(getTitle(), x, y, paintTitle);
    }
}

From source file:com.albedinsky.android.ui.widget.SeekBarWidget.java

/**
 * Draws discrete indicator of this SeekBarWidget at its current position updated by
 * {@link #updateDiscreteIndicatorPosition(int, int)} according to the current progress.
 *
 * @param canvas Canvas on which to draw discrete indicator's drawable.
 *//*  ww w. j a  va  2s .  c  o  m*/
private void drawDiscreteIndicator(Canvas canvas) {
    if (mDiscreteIndicatorHeight == 0) {
        return;
    }
    // todo: draw according to LTR/RTL layout direction.
    mDiscreteIndicator.draw(canvas);

    // Draw current progress over indicator's graphics.
    final Rect indicatorBounds = mDiscreteIndicator.getBounds();
    final Paint textPaint = DISCRETE_INDICATOR_TEXT_INFO.paint;
    textPaint.getTextBounds("0", 0, 1, mRect);
    final float textSize = mRect.height();
    final Rect textPadding = DISCRETE_INDICATOR_TEXT_INFO.padding;
    final int absoluteTextGravity = WidgetGravity.getAbsoluteGravity(DISCRETE_INDICATOR_TEXT_INFO.gravity,
            ViewCompat.getLayoutDirection(this));

    final float textX, textY;
    // Resolve horizontal text position according to the requested gravity.
    switch (absoluteTextGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.CENTER_HORIZONTAL:
        textPaint.setTextAlign(Paint.Align.CENTER);
        textX = indicatorBounds.centerX();
        break;
    case Gravity.RIGHT:
        textPaint.setTextAlign(Paint.Align.RIGHT);
        textX = indicatorBounds.right - textPadding.right;
        break;
    case Gravity.LEFT:
    default:
        textPaint.setTextAlign(Paint.Align.LEFT);
        textX = indicatorBounds.left + textPadding.left;
        break;
    }
    // Resolve vertical text position according to the requested gravity.
    switch (absoluteTextGravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.CENTER_VERTICAL:
        textY = indicatorBounds.centerY() + textSize / 2f;
        break;
    case Gravity.BOTTOM:
        textY = indicatorBounds.bottom - textPadding.bottom;
        break;
    case Gravity.TOP:
    default:
        textY = indicatorBounds.top + textSize + textPadding.top;
        break;
    }
    canvas.drawText(Integer.toString(getProgress()), textX, textY, textPaint);
}

From source file:radialdemo.RadialMenuWidget.java

@Override
protected void onDraw(Canvas c) {

    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from   w ww  .  j a v a 2s  . com*/
    paint.setStrokeWidth(3);

    // draws a dot at the source of the press
    if (showSource == true) {
        paint.setColor(outlineColor);
        paint.setAlpha(outlineAlpha);
        paint.setStyle(Paint.Style.STROKE);
        c.drawCircle(xSource, ySource, cRadius / 10, paint);

        paint.setColor(selectedColor);
        paint.setAlpha(selectedAlpha);
        paint.setStyle(Paint.Style.FILL);
        c.drawCircle(xSource, ySource, cRadius / 10, paint);
    }
    //inner
    for (int i = 0; i < Wedges.length; i++) {
        RadialMenuWedge f = Wedges[i];
        paint.setColor(outlineColor);
        paint.setAlpha(outlineAlpha);
        paint.setStyle(Paint.Style.STROKE);
        c.drawPath(f, paint);
        if (f == enabled && Wedge2Shown == true) {
            paint.setColor(wedge2Color);
            paint.setAlpha(wedge2Alpha);
            paint.setStyle(Paint.Style.FILL);
            c.drawPath(f, paint);
        } else if (f != enabled && Wedge2Shown == true) {
            paint.setColor(disabledColor);
            paint.setAlpha(disabledAlpha);
            paint.setStyle(Paint.Style.FILL);
            c.drawPath(f, paint);
        } else if (f == enabled && Wedge2Shown == false) {
            paint.setColor(wedge2Color);
            paint.setAlpha(wedge2Alpha);
            paint.setStyle(Paint.Style.FILL);
            c.drawPath(f, paint);
        } else if (f == selected) {
            paint.setColor(wedge2Color);
            paint.setAlpha(wedge2Alpha);
            paint.setStyle(Paint.Style.FILL);
            c.drawPath(f, paint);
        } else {
            paint.setColor(defaultColor);
            paint.setAlpha(defaultAlpha);
            paint.setStyle(Paint.Style.FILL);
            c.drawPath(f, paint);
        }
        //button content
        Rect rf = iconRect[i];

        if ((menuEntries.get(i).getIcon() != 0) && (menuEntries.get(i).getLabel() != null)) {

            // This will look for a "new line" and split into multiple lines
            String menuItemName = menuEntries.get(i).getLabel();
            String[] stringArray = menuItemName.split("\n");

            paint.setColor(textColor);
            if (f != enabled && Wedge2Shown == true) {
                paint.setAlpha(disabledAlpha);
            } else {
                paint.setAlpha(textAlpha);
            }
            paint.setStyle(Paint.Style.FILL);
            paint.setTextSize(textSize);

            Rect rect = new Rect();
            float textHeight = 0;
            for (int j = 0; j < stringArray.length; j++) {
                paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect);
                textHeight = textHeight + (rect.height() + 3);
            }

            Rect rf2 = new Rect();
            rf2.set(rf.left, rf.top - ((int) textHeight / 2), rf.right, rf.bottom - ((int) textHeight / 2));

            float textBottom = rf2.bottom;
            for (int j = 0; j < stringArray.length; j++) {
                paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect);
                float textLeft = rf.centerX() - rect.width() / 2;
                textBottom = textBottom + (rect.height() + 3);
                c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint);
            }

            // Puts in the Icon
            Drawable drawable = getResources().getDrawable(menuEntries.get(i).getIcon());
            drawable.setBounds(rf2);
            if (f != enabled && Wedge2Shown == true) {
                drawable.setAlpha(disabledAlpha);
            } else {
                drawable.setAlpha(pictureAlpha);
            }
            drawable.draw(c);

            // Icon Only
        } else if (menuEntries.get(i).getIcon() != 0) {
            // Puts in the Icon
            Drawable drawable = getResources().getDrawable(menuEntries.get(i).getIcon());
            drawable.setBounds(rf);
            if (f != enabled && Wedge2Shown == true) {
                drawable.setAlpha(disabledAlpha);
            } else {
                drawable.setAlpha(pictureAlpha);
            }
            drawable.draw(c);

            // Text Only
        } else {
            // Puts in the Text if no Icon
            paint.setColor(textColor);
            if (f != enabled && Wedge2Shown == true) {
                paint.setAlpha(disabledAlpha);
            } else {
                paint.setAlpha(textAlpha);
            }
            paint.setStyle(Paint.Style.FILL);
            paint.setTextSize(textSize + 10);

            // This will look for a "new line" and split into multiple lines
            String menuItemName = menuEntries.get(i).getLabel();
            String[] stringArray = menuItemName.split("\n");

            // gets total height
            Rect rect = new Rect();
            float textHeight = 0;
            for (int j = 0; j < stringArray.length; j++) {
                paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect);
                textHeight = textHeight + (rect.height() + 3);
            }

            float textBottom = rf.centerY() - (textHeight / 2);
            for (int j = 0; j < stringArray.length; j++) {
                paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect);
                float textLeft = rf.centerX() - rect.width() / 2;
                textBottom = textBottom + (rect.height() + 3);
                c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint);
            }
        }

    }

    // Animate the outer ring in/out
    if (animateOuterIn == true) {
        animateOuterWedges(ANIMATE_IN);
    } else if (animateOuterOut == true) {
        animateOuterWedges(ANIMATE_OUT);
    }
    //outer
    if (Wedge2Shown == true) {

        for (int i = 0; i < Wedges2.length; i++) {
            RadialMenuWedge f = Wedges2[i];
            paint.setColor(outlineColor);
            paint.setAlpha(20);
            paint.setStyle(Paint.Style.STROKE);
            c.drawPath(f, paint);
            if (f == selected2) {
                paint.setColor(selectedColor);
                paint.setAlpha(selectedAlpha);
                paint.setStyle(Paint.Style.FILL);
                c.drawPath(f, paint);
            } else {
                paint.setColor(wedge2Color);
                paint.setAlpha(wedge2Alpha);
                paint.setStyle(Paint.Style.FILL);
                c.drawPath(f, paint);
            }

            Rect rf = iconRect2[i];
            if ((wedge2Data.getChildren().get(i).getIcon() != 0)
                    && (wedge2Data.getChildren().get(i).getLabel() != null)) {

                // This will look for a "new line" and split into multiple
                // lines
                String menuItemName = wedge2Data.getChildren().get(i).getLabel();
                String[] stringArray = menuItemName.split("\n");

                paint.setColor(textColor);
                paint.setAlpha(textAlpha);
                paint.setStyle(Paint.Style.FILL);
                paint.setTextSize(animateTextSize);

                Rect rect = new Rect();
                float textHeight = 0;
                for (int j = 0; j < stringArray.length; j++) {
                    paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect);
                    textHeight = textHeight + (rect.height() + 3);
                }

                Rect rf2 = new Rect();
                rf2.set(rf.left, rf.top - ((int) textHeight / 2), rf.right, rf.bottom - ((int) textHeight / 2));

                float textBottom = rf2.bottom;
                for (int j = 0; j < stringArray.length; j++) {
                    paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect);
                    float textLeft = rf.centerX() - rect.width() / 2;
                    textBottom = textBottom + (rect.height() + 3);
                    c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint);
                }

                // Puts in the Icon
                Drawable drawable = getResources().getDrawable(wedge2Data.getChildren().get(i).getIcon());
                drawable.setBounds(rf2);
                drawable.setAlpha(pictureAlpha);
                drawable.draw(c);

                // Icon Only
            } else if (wedge2Data.getChildren().get(i).getIcon() != 0) {
                // Puts in the Icon
                Drawable drawable = getResources().getDrawable(wedge2Data.getChildren().get(i).getIcon());
                drawable.setBounds(rf);
                drawable.setAlpha(pictureAlpha);
                drawable.draw(c);

                // Text Only
            } else {
                // Puts in the Text if no Icon
                paint.setColor(textColor);
                paint.setAlpha(textAlpha);
                paint.setStyle(Paint.Style.FILL);
                paint.setTextSize(animateTextSize);

                // This will look for a "new line" and split into multiple
                // lines
                String menuItemName = wedge2Data.getChildren().get(i).getLabel();
                String[] stringArray = menuItemName.split("\n");

                // gets total height
                Rect rect = new Rect();
                float textHeight = 0;
                for (int j = 0; j < stringArray.length; j++) {
                    paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect);
                    textHeight = textHeight + (rect.height() + 3);
                }

                float textBottom = rf.centerY() - (textHeight / 2);
                for (int j = 0; j < stringArray.length; j++) {
                    paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect);
                    float textLeft = rf.centerX() - rect.width() / 2;
                    textBottom = textBottom + (rect.height() + 3);
                    c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint);
                }
            }
        }
    }

    //Check if the user has given input for centre circle
    if (centerCircle != null) {
        // Draws the Middle Circle
        paint.setColor(outlineColor);
        paint.setAlpha(outlineAlpha);
        paint.setStyle(Paint.Style.STROKE);
        c.drawCircle(xPosition, yPosition, cRadius, paint);
        if (inCircle == true) {
            paint.setColor(selectedColor);
            paint.setAlpha(selectedAlpha);
            paint.setStyle(Paint.Style.FILL);
            c.drawCircle(xPosition, yPosition, cRadius, paint);
            helper.onCloseAnimation(this, xPosition, yPosition, xSource, ySource);
        } else {
            paint.setColor(defaultColor);
            paint.setAlpha(defaultAlpha);
            paint.setStyle(Paint.Style.FILL);
            c.drawCircle(xPosition, yPosition, cRadius, paint);
        }

        // Draw the circle picture
        if ((centerCircle.getIcon() != 0) && (centerCircle.getLabel() != null)) {

            // This will look for a "new line" and split into multiple lines
            String menuItemName = centerCircle.getLabel();
            String[] stringArray = menuItemName.split("\n");

            paint.setColor(textColor);
            paint.setAlpha(textAlpha);
            paint.setStyle(Paint.Style.FILL);
            paint.setTextSize(textSize);

            Rect rectText = new Rect();
            Rect rectIcon = new Rect();
            Drawable drawable = getResources().getDrawable(centerCircle.getIcon());

            int h = getIconSize(drawable.getIntrinsicHeight(), MinIconSize, MaxIconSize);
            int w = getIconSize(drawable.getIntrinsicWidth(), MinIconSize, MaxIconSize);
            rectIcon.set(xPosition - w / 2, yPosition - h / 2, xPosition + w / 2, yPosition + h / 2);

            float textHeight = 0;
            for (int j = 0; j < stringArray.length; j++) {
                paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rectText);
                textHeight = textHeight + (rectText.height() + 3);
            }

            rectIcon.set(rectIcon.left, rectIcon.top - ((int) textHeight / 2), rectIcon.right,
                    rectIcon.bottom - ((int) textHeight / 2));

            float textBottom = rectIcon.bottom;
            for (int j = 0; j < stringArray.length; j++) {
                paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rectText);
                float textLeft = xPosition - rectText.width() / 2;
                textBottom = textBottom + (rectText.height() + 3);
                c.drawText(stringArray[j], textLeft - rectText.left, textBottom - rectText.bottom, paint);
            }

            // Puts in the Icon
            drawable.setBounds(rectIcon);
            drawable.setAlpha(pictureAlpha);
            drawable.draw(c);

            // Icon Only
        } else if (centerCircle.getIcon() != 0) {

            Rect rect = new Rect();

            Drawable drawable = getResources().getDrawable(centerCircle.getIcon());

            int h = getIconSize(drawable.getIntrinsicHeight(), MinIconSize, MaxIconSize);
            int w = getIconSize(drawable.getIntrinsicWidth(), MinIconSize, MaxIconSize);
            rect.set(xPosition - w / 2, yPosition - h / 2, xPosition + w / 2, yPosition + h / 2);

            drawable.setBounds(rect);
            drawable.setAlpha(pictureAlpha);
            drawable.draw(c);

            // Text Only
        } else {
            // Puts in the Text if no Icon
            paint.setColor(textColor);
            paint.setAlpha(textAlpha);
            paint.setStyle(Paint.Style.FILL);
            paint.setTextSize(textSize);

            // This will look for a "new line" and split into multiple lines
            String menuItemName = centerCircle.getLabel();
            String[] stringArray = menuItemName.split("\n");

            // gets total height
            Rect rect = new Rect();
            float textHeight = 0;
            for (int j = 0; j < stringArray.length; j++) {
                paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect);
                textHeight = textHeight + (rect.height() + 3);
            }

            float textBottom = yPosition - (textHeight / 2);
            for (int j = 0; j < stringArray.length; j++) {
                paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect);
                float textLeft = xPosition - rect.width() / 2;
                textBottom = textBottom + (rect.height() + 3);
                c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint);
            }

        }
    }

    // Draws Text in TextBox
    if (headerString != null) {

        paint.setTextSize(headerTextSize);
        paint.getTextBounds(headerString, 0, headerString.length(), this.textRect);
        if (HeaderBoxBounded == false) {
            determineHeaderBox();
            HeaderBoxBounded = true;
        }

        paint.setColor(outlineColor);
        paint.setAlpha(outlineAlpha);
        paint.setStyle(Paint.Style.STROKE);
        c.drawRoundRect(this.textBoxRect, scalePX(5), scalePX(5), paint);
        paint.setColor(headerBackgroundColor);
        paint.setAlpha(headerBackgroundAlpha);
        paint.setStyle(Paint.Style.FILL);
        c.drawRoundRect(this.textBoxRect, scalePX(5), scalePX(5), paint);

        paint.setColor(headerTextColor);
        paint.setAlpha(headerTextAlpha);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(headerTextSize);
        c.drawText(headerString, headerTextLeft, headerTextBottom, paint);
    }

}

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

private void drawMACD(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 || quoteData.isNull("MACD")) {
        isNetworkErrorDisplay();/*from  w  w  w .j  a va 2s.c om*/
        return;
    }
    double max = 0.001;
    double min = -0.001;
    int len = quoteData.getJSONArray("MACD").length();
    for (int i = begin; i < (begin + count) && i < len; i++) {
        double dif = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(1);
        double dea = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(2);
        double macd = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(3);

        if (dif > max)
            max = dif;
        if (dea > max)
            max = dea;
        if (macd > max)
            max = macd;

        if (dif < 0) {
            if (dif < min)
                min = dif;
        }
        if (dea < 0) {
            if (dea < min)
                min = dea;
        }
        if (macd < 0) {
            if (macd < min)
                min = macd;
        }
    }

    double upHeight = 0;
    double downHeight = 0;
    upHeight = volumeHeight * (max / (max + Math.abs(min)));
    downHeight = volumeHeight * (Math.abs(min) / (max + Math.abs(min)));

    if (isTrackStatus == false) {
        String axisLabel1 = "";
        if (zs)
            axisLabel1 = Utils.dataFormation(max, 0);
        else
            axisLabel1 = Utils.dataFormation(max, stockdigit);
        tPaint.setColor(GlobalColor.colorKlabel);
        canvas.drawText(axisLabel1, klineX - tips / 4, klineY, tPaint);

        canvas.drawText(Utils.dataFormation(0, stockdigit), klineX - tips / 4, (int) (klineY + upHeight),
                tPaint);

        String axisLabel3 = "";
        if (zs)
            axisLabel3 = Utils.dataFormation(min, 0);
        else
            axisLabel3 = Utils.dataFormation(min, stockdigit);
        canvas.drawText(axisLabel3, klineX - tips / 4, (klineY + volumeHeight), tPaint);
    }
    paint.setColor(GlobalColor.clrLine);
    Graphics.drawDashline(canvas, klineX, klineY, width, klineY, paint);
    Graphics.drawDashline(canvas, klineX, (int) (klineY + upHeight), width, (int) (klineY + upHeight), paint);
    canvas.drawLine(klineX, height - axisLabelHeight, width, height - axisLabelHeight, paint);

    if (quoteData != null) {
        double axisX = klineX;

        double dif = 0, dea = 0, macd = 0, difx1 = 0, difx2 = 0, deax1 = 0, deax2 = 0;
        int dify1 = 0, dify2 = 0, deay1 = 0, deay2 = 0;
        for (int i = begin; i < (begin + count) && i < len; i++) {
            dif = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(1);
            dea = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(2);
            macd = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(3);

            double difScale = 0;
            double deaScale = 0;
            double macdScale = 0;

            double difHeight = 0;
            double deaHeight = 0;
            double macdHeight = 0;

            if (dif >= 0) {
                difScale = upHeight / max;
            } else {
                difScale = downHeight / Math.abs(min);
            }

            if (dea >= 0) {
                deaScale = upHeight / max;
            } else {
                deaScale = downHeight / Math.abs(min);
            }

            if (macd >= 0) {
                macdScale = upHeight / max;
            } else {
                macdScale = downHeight / Math.abs(min);
            }

            if ((i - begin) == 0)
                axisX = axisX + spaceWidth;
            else
                axisX = axisX + spaceWidth + shapeWidth;

            if ((i - begin) == 0) {
                difx2 = difx2 + klineX + spaceWidth + shapeWidth / 2;
                difHeight = Math.abs(dif) * difScale;
                if (dif >= 0) {
                    dify2 = (int) (klineY + upHeight - difHeight);
                } else {
                    dify2 = (int) (klineY + upHeight + difHeight);
                }
            } else {
                difx1 = difx2;
                dify1 = dify2;
                difx2 = difx2 + spaceWidth + shapeWidth;
                difHeight = Math.abs(dif) * difScale;
                if (dif >= 0) {
                    dify2 = (int) (klineY + upHeight - difHeight);
                } else {
                    dify2 = (int) (klineY + upHeight + difHeight);
                }

                paint.setColor(GlobalColor.colorM5);
                canvas.drawLine((float) difx1, (float) dify1, (float) difx2, (float) dify2, paint);
            }

            if ((i - begin) == 0) {
                deax2 = deax2 + klineX + spaceWidth + shapeWidth / 2;
                deaHeight = Math.abs(dea) * deaScale;
                if (dea >= 0) {
                    deay2 = (int) (klineY + upHeight - deaHeight);
                } else {
                    deay2 = (int) (klineY + upHeight + deaHeight);
                }
            } else {
                deax1 = deax2;
                deay1 = deay2;
                deax2 = deax2 + spaceWidth + shapeWidth;
                deaHeight = Math.abs(dea) * deaScale;
                if (dea >= 0) {
                    deay2 = (int) (klineY + upHeight - deaHeight);
                } else {
                    deay2 = (int) (klineY + upHeight + deaHeight);
                }

                paint.setColor(GlobalColor.colorM10);
                canvas.drawLine((float) deax1, (float) deay1, (float) deax2, (float) deay2, paint);
            }

            if (macd >= 0) {
                macdHeight = Math.abs(macd) * macdScale;
                paint.setColor(GlobalColor.colorKdown);
                canvas.drawLine((int) (axisX + shapeWidth / 2), (int) (klineY + upHeight - macdHeight),
                        (int) (axisX + shapeWidth / 2), (int) (klineY + upHeight), paint);
            } else {
                macdHeight = Math.abs(macd) * macdScale;
                paint.setARGB(255, 84, 255, 255);
                canvas.drawLine((int) (axisX + shapeWidth / 2), (int) (klineY + upHeight),
                        (int) (axisX + shapeWidth / 2), (int) (klineY + upHeight + macdHeight), paint);
            }

            if (i == (begin + count - 1)) {
                //
            }
        } // end for
    } // end if
}

From source file:com.viewpagerindicator.TitlePageIndicator.java

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

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

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

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

    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    final int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mCurrentOffset <= halfWidth) {
        offsetPercent = 1.0f * mCurrentOffset / width;
    } else {
        page += 1;
        offsetPercent = 1.0f * (width - mCurrentOffset) / width;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    //Verify if the current view must be clipped to the screen
    RectF curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            RectF bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                float w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                RectF rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = rightBound.left - w - mTitlePadding;
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            RectF bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                float w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                RectF leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = leftBound.right + mTitlePadding;
                    bound.right = bound.left + w;
                }
            }
        }
    }

    //Now draw views
    for (int i = 0; i < count; i++) {
        //Get the title
        RectF bound = bounds.get(i);
        //Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

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

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

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

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

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

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

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

From source file:com.viewpagerindicator.TitlePageIndicatorEx1.java

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

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

    final int count = mViewPager.getAdapter().getCount();
    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    final int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    //        int page = mSelectedPage;
    float offsetPercent;
    //        Log.d("xx", "page: "+mCurrentPage+", offset: "+mCurrentOffset+", halfwidth: "+halfWidth);

    if (mCurrentOffset <= halfWidth) {
        offsetPercent = 1.0f * mCurrentOffset / width;
    } else {/*w w w.  j a  va2 s . c o m*/
        page += 1;
        offsetPercent = 1.0f * (width - mCurrentOffset) / width;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    //Verify if the current view must be clipped to the screen
    RectF curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    //        if (curPageBound.left < leftClip) {
    //            //Try to clip to the screen (left side)
    //            clipViewOnTheLeft(curPageBound, curPageWidth, left);
    //        }
    //        if (curPageBound.right > rightClip) {
    //            //Try to clip to the screen (right side)
    //            clipViewOnTheRight(curPageBound, curPageWidth, right);
    //        }
    //
    //        //Left views starting from the current position
    //        if (mCurrentPage > 0) {
    //            for (int i = mCurrentPage - 1; i >= 0; i--) {
    //                RectF bound = bounds.get(i);
    //                //Is left side is outside the screen
    //                if (bound.left < leftClip) {
    //                    float w = bound.right - bound.left;
    //                    //Try to clip to the screen (left side)
    //                    clipViewOnTheLeft(bound, w, left);
    //                    //Except if there's an intersection with the right view
    //                    RectF rightBound = bounds.get(i + 1);
    //                    //Intersection
    //                    if (bound.right + mTitlePadding > rightBound.left) {
    //                        bound.left = rightBound.left - w - mTitlePadding;
    //                        bound.right = bound.left + w;
    //                    }
    //                }
    //            }
    //        }
    //        //Right views starting from the current position
    //        if (mCurrentPage < countMinusOne) {
    //            for (int i = mCurrentPage + 1 ; i < count; i++) {
    //                RectF bound = bounds.get(i);
    //                //If right side is outside the screen
    //                if (bound.right > rightClip) {
    //                    float w = bound.right - bound.left;
    //                    //Try to clip to the screen (right side)
    //                    clipViewOnTheRight(bound, w, right);
    //                    //Except if there's an intersection with the left view
    //                    RectF leftBound = bounds.get(i - 1);
    //                    //Intersection
    //                    if (bound.left - mTitlePadding < leftBound.right) {
    //                        bound.left = leftBound.right + mTitlePadding;
    //                        bound.right = bound.left + w;
    //                    }
    //                }
    //            }
    //        }

    //Now draw views
    for (int i = 0; i < count; i++) {
        //Get the title
        RectF bound = bounds.get(i);
        //Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

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

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

        }
    }

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

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

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

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

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

From source file:at.ac.uniklu.mobile.sportal.ui.viewpagerindicator.TitlePageIndicator.java

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

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

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

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

    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    final int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mCurrentOffset <= halfWidth) {
        offsetPercent = 1.0f * mCurrentOffset / width;
    } else {
        page += 1;
        offsetPercent = 1.0f * (width - mCurrentOffset) / width;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    //Verify if the current view must be clipped to the screen
    RectF curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            RectF bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                float w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                RectF rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = rightBound.left - w - mTitlePadding;
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            RectF bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                float w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                RectF leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = leftBound.right + mTitlePadding;
                    bound.right = bound.left + w;
                }
            }
        }
    }

    //Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        //Get the title
        RectF bound = bounds.get(i);
        //Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

            //Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                //Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }
            canvas.drawText(mTitleProvider.getTitle(i), bound.left, bound.bottom + mTopPadding, mPaintText);

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

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

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

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

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

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

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

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

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

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

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

    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    final int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mCurrentOffset <= halfWidth) {
        offsetPercent = 1.0f * mCurrentOffset / width;
    } else {
        page += 1;
        offsetPercent = 1.0f * (width - mCurrentOffset) / width;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    //Verify if the current view must be clipped to the screen
    RectF curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            RectF bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                float w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                RectF rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = rightBound.left - w - mTitlePadding;
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            RectF bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                float w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                RectF leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = leftBound.right + mTitlePadding;
                    bound.right = bound.left + w;
                }
            }
        }
    }

    //Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        //Get the title
        RectF bound = bounds.get(i);
        //Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

            //Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                //Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }
            canvas.drawText(mTitleProvider.getTitle(i), bound.left, bound.bottom + mTopPadding, mPaintText);

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

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

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

    case Underline:
        if (!currentSelected || page >= boundsSize) {
            break;
        }

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

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

From source file:com.android.common.TitlePageIndicator.java

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

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

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

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

    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    final int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mCurrentOffset <= halfWidth) {
        offsetPercent = 1.0f * mCurrentOffset / width;
    } else {
        page += 1;
        offsetPercent = 1.0f * (width - mCurrentOffset) / width;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    //Verify if the current view must be clipped to the screen
    RectF curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            RectF bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                float w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                RectF rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = rightBound.left - w - mTitlePadding;
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            RectF bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                float w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                RectF leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = leftBound.right + mTitlePadding;
                    bound.right = bound.left + w;
                }
            }
        }
    }

    //Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        //Get the title
        RectF bound = bounds.get(i);
        //Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

            //Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                //Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }
            canvas.drawText(mTitleProvider.GetTitle(i), bound.left, bound.bottom + mTopPadding, mPaintText);

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

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

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

    case Underline:
        if (!currentSelected || page >= boundsSize) {
            break;
        }

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

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