Example usage for android.graphics Paint ascent

List of usage examples for android.graphics Paint ascent

Introduction

In this page you can find the example usage for android.graphics Paint ascent.

Prototype

public float ascent() 

Source Link

Document

Return the distance above (negative) the baseline (ascent) based on the current typeface and text size.

Usage

From source file:com.along.altmcssd.pda.widget.viewpagerindicator.TitlePageIndicator.java

/**
 * Calculate the bounds for a view's title
 * //from  ww  w. j  av  a2s . c  o m
 * @param index
 * @param paint
 * @return
 */
private Rect calcBounds(int index, Paint paint) {
    // Calculate the text bounds
    Rect bounds = new Rect();
    CharSequence title = getTitle(index);
    bounds.right = (int) paint.measureText(title, 0, title.length());
    bounds.bottom = (int) (paint.descent() - paint.ascent());
    return bounds;
}

From source file:android.hqs.view.pager.indicator.TitlePageIndicator.java

/**
 * Calculate the bounds for a view's title
 *
 * @param index/*from   w w  w. j a  va2s . co m*/
 * @param paint
 * @return
 */
private Rect calcBounds(int index, Paint paint) {
    //Calculate the text bounds
    Rect bounds = new Rect();
    CharSequence title = getTitle(index);
    bounds.right = (int) paint.measureText(title, 0, title.length());
    bounds.bottom = (int) (paint.descent() - paint.ascent());
    return bounds;
}

From source file:cn.eoe.app.indicator.TitlePageIndicator.java

/**
 * Calculate the bounds for a view's title
 *
 * @param index/*from  www  . ja v a 2 s  .co  m*/
 * @param paint
 * @return
 */
private Rect calcBounds(int index, Paint paint) {
    //Calculate the text bounds
    Rect bounds = new Rect();
    CharSequence title = getTitle(index);
    bounds.right = (int) paint.measureText(title, 0, title.length()) + TextMaginLeftAndRight;
    bounds.bottom = (int) (paint.descent() - paint.ascent());
    return bounds;
}

From source file:com.focustech.common.widget.TitlePageIndicator.java

/**
 * Calculate the bounds for a view's title
 * // w w w . ja  va2s.co m
 * @param index
 * @param paint
 * @return
 */
private Rect calcBounds(int index, Paint paint) {
    // Calculate the text bounds
    Rect bounds = new Rect();
    CharSequence title = getTitle(index);
    if (index == 0 && mIsShowPoint) {
        bounds.right = (int) paint.measureText(title, 0, title.length()) + 15;
        bounds.bottom = (int) (paint.descent() - paint.ascent());
    } else {
        bounds.right = (int) paint.measureText(title, 0, title.length());
        bounds.bottom = (int) (paint.descent() - paint.ascent());
    }
    return bounds;
}

From source file:io.doist.datetimepicker.time.RadialTimePickerView.java

/**
 * Using the trigonometric Unit Circle, calculate the positions that the text will need to be
 * drawn at based on the specified circle radius. Place the values in the textGridHeights and
 * textGridWidths parameters.//from   w w w . java2s .c o  m
 */
private static void calculateGridSizes(Paint paint, float numbersRadius, float xCenter, float yCenter,
        float textSize, float[] textGridHeights, float[] textGridWidths) {
    /*
     * The numbers need to be drawn in a 7x7 grid, representing the points on the Unit Circle.
     */
    final float offset1 = numbersRadius;
    // cos(30) = a / r => r * cos(30)
    final float offset2 = numbersRadius * COSINE_30_DEGREES;
    // sin(30) = o / r => r * sin(30)
    final float offset3 = numbersRadius * SINE_30_DEGREES;

    paint.setTextSize(textSize);
    // We'll need yTextBase to be slightly lower to account for the text's baseline.
    yCenter -= (paint.descent() + paint.ascent()) / 2;

    textGridHeights[0] = yCenter - offset1;
    textGridWidths[0] = xCenter - offset1;
    textGridHeights[1] = yCenter - offset2;
    textGridWidths[1] = xCenter - offset2;
    textGridHeights[2] = yCenter - offset3;
    textGridWidths[2] = xCenter - offset3;
    textGridHeights[3] = yCenter;
    textGridWidths[3] = xCenter;
    textGridHeights[4] = yCenter + offset3;
    textGridWidths[4] = xCenter + offset3;
    textGridHeights[5] = yCenter + offset2;
    textGridWidths[5] = xCenter + offset2;
    textGridHeights[6] = yCenter + offset1;
    textGridWidths[6] = xCenter + offset1;
}

From source file:com.viewpagerindicator.RichTitlePageIndicator.java

/**
 * Calculate the bounds for a view's title
 *
 * @param index// w  w  w  .  j ava 2 s  . com
 * @param paint
 * @return
 */
private Rect calcBounds(int index, Paint paint) {
    //Calculate the text bounds
    Rect bounds = new Rect();
    String title = getTitle(index);
    Bitmap icon = getIcon(index);

    int textWidth = (int) paint.measureText(title);
    int iconWidth = icon == null ? 0 : icon.getWidth();
    bounds.right = textWidth + iconWidth;

    int textHeight = (int) (paint.descent() - paint.ascent());
    bounds.bottom = textHeight;
    //int iconHeight = icon == null ? 0 : icon.getHeight();
    //bounds.bottom = Math.max(textHeight, iconHeight);
    return bounds;
}

From source file:org.mariotaku.twidere.text.OriginalStatusSpan.java

@Override
public void draw(final Canvas canvas, final CharSequence text, final int start, final int end, final float x,
        final int top, final int y, final int bottom, final Paint paint) {
    if (!(paint instanceof TextPaint))
        return;//from w  w w .j  av a2  s. c om
    final TextPaint tp = (TextPaint) paint;
    mBounds.left = x;
    mBounds.right = x + paint.measureText(text, start, end) + mPadding * 2;
    mBounds.top = top;
    mBounds.bottom = bottom;
    final int innerTextColor = TwidereColorUtils.getContrastYIQ(tp.linkColor, ThemeUtils.ACCENT_COLOR_THRESHOLD,
            mDarkLightColors[0], mDarkLightColors[1]);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setColor(tp.linkColor);
    mBounds.inset(mPaint.getStrokeWidth() / 2, mPaint.getStrokeWidth() / 2);
    canvas.drawRoundRect(mBounds, mCornerRadius, mCornerRadius, mPaint);
    mBounds.inset(-mPaint.getStrokeWidth() / 2, -mPaint.getStrokeWidth() / 2);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setColor(
            ColorUtils.compositeColors(ColorUtils.setAlphaComponent(innerTextColor, 0x80), tp.linkColor));
    mBounds.inset(mPaint.getStrokeWidth() / 2, mPaint.getStrokeWidth() / 2);
    canvas.drawRoundRect(mBounds, mCornerRadius, mCornerRadius, mPaint);
    paint.setColor(innerTextColor);
    canvas.drawText(text, start, end, x + mPadding,
            top + (bottom - top) / 2 - (paint.descent() + paint.ascent()) / 2, paint);
}

From source file:com.gruporaido.tasker_library.util.Helper.java

/**
 * @param drawableId/*from   w  w  w .  j  a v a 2 s.  c o m*/
 * @param text
 * @param textSize
 * @param offsetX
 * @param offsetY
 * @return
 */
public Bitmap drawTextOnDrawable(int drawableId, String text, int textSize, int offsetX, int offsetY) {

    Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), drawableId).copy(Bitmap.Config.ARGB_8888,
            true);

    Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTypeface(tf);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setTextSize(dpToPx(textSize));

    Rect textRect = new Rect();
    paint.getTextBounds(text, 0, text.length(), textRect);

    Canvas canvas = new Canvas(bm);

    //If the text is bigger than the canvas , reduce the font size
    if (textRect.width() >= (canvas.getWidth() - 4)) //the padding on either sides is considered as 4, so as to appropriately fit in the text
        paint.setTextSize(dpToPx(textSize / 2)); //Scaling needs to be used for different dpi's

    //Calculate the positions
    int xPos = (canvas.getWidth() / 2) - 2 + dpToPx(offsetX); //-2 is for regulating the x position offset

    //"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) + dpToPx(offsetY);

    canvas.drawText(text, xPos, yPos, paint);

    return bm;
}

From source file:io.doist.datetimepicker.time.RadialTimePickerView.java

private void drawDebug(Canvas canvas) {
    // Draw outer numbers circle
    final float outerRadius = mCircleRadius[HOURS] * mNumbersRadiusMultiplier[HOURS];
    canvas.drawCircle(mXCenter, mYCenter, outerRadius, mPaintDebug);

    // Draw inner numbers circle
    final float innerRadius = mCircleRadius[HOURS] * mNumbersRadiusMultiplier[HOURS_INNER];
    canvas.drawCircle(mXCenter, mYCenter, innerRadius, mPaintDebug);

    // Draw outer background circle
    canvas.drawCircle(mXCenter, mYCenter, mCircleRadius[HOURS], mPaintDebug);

    // Draw outer rectangle for circles
    float left = mXCenter - outerRadius;
    float top = mYCenter - outerRadius;
    float right = mXCenter + outerRadius;
    float bottom = mYCenter + outerRadius;
    canvas.drawRect(left, top, right, bottom, mPaintDebug);

    // Draw outer rectangle for background
    left = mXCenter - mCircleRadius[HOURS];
    top = mYCenter - mCircleRadius[HOURS];
    right = mXCenter + mCircleRadius[HOURS];
    bottom = mYCenter + mCircleRadius[HOURS];
    canvas.drawRect(left, top, right, bottom, mPaintDebug);

    // Draw outer view rectangle
    canvas.drawRect(0, 0, getWidth(), getHeight(), mPaintDebug);

    // Draw selected time
    final String selected = String.format("%02d:%02d", getCurrentHour(), getCurrentMinute());

    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    TextView tv = new TextView(getContext());
    tv.setLayoutParams(lp);//from   w  w w.  j  a v a2 s  . com
    tv.setText(selected);
    tv.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    Paint paint = tv.getPaint();
    paint.setColor(DEBUG_TEXT_COLOR);

    final int width = tv.getMeasuredWidth();

    float height = paint.descent() - paint.ascent();
    float x = mXCenter - width / 2;
    float y = mYCenter + 1.5f * height;

    canvas.drawText(selected, x, y, paint);
}