android.text.TextPaint#descent ( )源码实例Demo

下面列出了android.text.TextPaint#descent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: android_9.0.0_r45   文件: SimpleMonthView.java
/**
 * Returns the row (0 indexed) closest to previouslyFocusedRect or center if null.
 */
private int findClosestRow(@Nullable Rect previouslyFocusedRect) {
    if (previouslyFocusedRect == null) {
        return 3;
    } else if (mDayHeight == 0) {
        return 0; // There hasn't been a layout, so just choose the first row
    } else {
        int centerY = previouslyFocusedRect.centerY();

        final TextPaint p = mDayPaint;
        final int headerHeight = mMonthHeight + mDayOfWeekHeight;
        final int rowHeight = mDayHeight;

        // Text is vertically centered within the row height.
        final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
        final int rowCenter = headerHeight + rowHeight / 2;

        centerY -= rowCenter - halfLineHeight;
        int row = Math.round(centerY / (float) rowHeight);
        final int maxDay = findDayOffset() + mDaysInMonth;
        final int maxRows = (maxDay / DAYS_IN_WEEK) - ((maxDay % DAYS_IN_WEEK == 0) ? 1 : 0);

        row = MathUtils.constrain(row, 0, maxRows);
        return row;
    }
}
 
源代码2 项目: android_9.0.0_r45   文件: SimpleMonthView.java
private void drawDaysOfWeek(Canvas canvas) {
    final TextPaint p = mDayOfWeekPaint;
    final int headerHeight = mMonthHeight;
    final int rowHeight = mDayOfWeekHeight;
    final int colWidth = mCellWidth;

    // Text is vertically centered within the day of week height.
    final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
    final int rowCenter = headerHeight + rowHeight / 2;

    for (int col = 0; col < DAYS_IN_WEEK; col++) {
        final int colCenter = colWidth * col + colWidth / 2;
        final int colCenterRtl;
        if (isLayoutRtl()) {
            colCenterRtl = mPaddedWidth - colCenter;
        } else {
            colCenterRtl = colCenter;
        }

        final String label = mDayOfWeekLabels[col];
        canvas.drawText(label, colCenterRtl, rowCenter - halfLineHeight, p);
    }
}
 
源代码3 项目: DateTimePicker   文件: SimpleMonthView.java
/**
 * Returns the row (0 indexed) closest to previouslyFocusedRect or center if null.
 */
private int findClosestRow(@Nullable Rect previouslyFocusedRect) {
    if (previouslyFocusedRect == null) {
        return 3;
    } else {
        int centerY = previouslyFocusedRect.centerY();

        final TextPaint p = mDayPaint;
        final int headerHeight = mMonthHeight + mDayOfWeekHeight;
        final int rowHeight = mDayHeight;

        // Text is vertically centered within the row height.
        final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
        final int rowCenter = headerHeight + rowHeight / 2;

        centerY -= rowCenter - halfLineHeight;
        int row = Math.round(centerY / (float) rowHeight);
        final int maxDay = findDayOffset() + mDaysInMonth;
        final int maxRows = (maxDay / DAYS_IN_WEEK) - ((maxDay % DAYS_IN_WEEK == 0) ? 1 : 0);

        row = mathConstrain(row, 0, maxRows);
        return row;
    }
}
 
源代码4 项目: DateTimePicker   文件: SimpleMonthView.java
private void drawDaysOfWeek(Canvas canvas) {
    final TextPaint p = mDayOfWeekPaint;
    final int headerHeight = mMonthHeight;
    final int rowHeight = mDayOfWeekHeight;
    final int colWidth = mCellWidth;

    // Text is vertically centered within the day of week height.
    final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
    final int rowCenter = headerHeight + rowHeight / 2;

    for (int col = 0; col < DAYS_IN_WEEK; col++) {
        final int colCenter = colWidth * col + colWidth / 2;
        final int colCenterRtl;
        if (isLayoutRtl()) {
            colCenterRtl = mPaddedWidth - colCenter;
        } else {
            colCenterRtl = colCenter;
        }

        final String label = mDayOfWeekLabels[col];
        canvas.drawText(label, colCenterRtl, rowCenter - halfLineHeight, p);
    }
}
 
源代码5 项目: KA27   文件: SplashView.java
private void draw(Canvas canvas, int x, int y, int radius) {
    if (radius > 0) canvas.drawCircle(x / 2, y / 2, radius, mPaintCircle);
    matrix.postRotate(rotate);
    Bitmap iconRotate = Bitmap.createBitmap(icon, 0, 0, icon.getWidth(), icon.getHeight(), matrix, false);
    canvas.drawBitmap(iconRotate, x / 2 - iconRotate.getWidth() / 2, y / 2 - iconRotate.getHeight() / 2, mPaintCircle);

    TextPaint textPaint = new TextPaint();
    textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    textPaint.setColor(textColor);
    textPaint.setAntiAlias(true);
    textPaint.setTextAlign(Paint.Align.CENTER);
    textPaint.setTextSize(textSize);
    float textHeight = textPaint.descent() - textPaint.ascent();
    float textOffset = (textHeight / 2) - textPaint.descent();

    canvas.drawText(getResources().getString(R.string.root_waiting), x / 2, y - textOffset - y / 4, textPaint);
}
 
源代码6 项目: kernel_adiutor   文件: CircleChart.java
private void draw(Canvas canvas, int x, int y) {
    mRectF.set(mPadding, mPadding, x - mPadding, y - mPadding);
    canvas.drawArc(mRectF, 0, 360, false, mPaintBackground);
    float offset = 360 / (float) mMax;
    canvas.drawArc(mRectF, 270, offset * mProgress, false, mPaintCircle);

    TextPaint textPaint = new TextPaint();
    textPaint.setColor(mCircleColor);
    textPaint.setAntiAlias(true);
    textPaint.setTextAlign(Paint.Align.CENTER);
    textPaint.setTextSize(mTextsize);
    float textHeight = textPaint.descent() - textPaint.ascent();
    float textOffset = (textHeight / 2) - textPaint.descent();

    RectF bounds = new RectF(mPadding, mPadding, x - mPadding, y - mPadding);
    String text = String.valueOf(mProgress);
    canvas.drawText(text, bounds.centerX(), bounds.centerY() + textOffset, textPaint);
}
 
源代码7 项目: kernel_adiutor   文件: SplashView.java
private void draw(Canvas canvas, int x, int y, int radius) {
    if (radius > 0) canvas.drawCircle(x / 2, y / 2, radius, mPaintCircle);
    matrix.postRotate(rotate);
    Bitmap iconRotate = Bitmap.createBitmap(icon, 0, 0, icon.getWidth(), icon.getHeight(), matrix, false);
    canvas.drawBitmap(iconRotate, x / 2 - iconRotate.getWidth() / 2, y / 2 - iconRotate.getHeight() / 2, mPaintCircle);

    TextPaint textPaint = new TextPaint();
    textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    textPaint.setColor(textColor);
    textPaint.setAntiAlias(true);
    textPaint.setTextAlign(Paint.Align.CENTER);
    textPaint.setTextSize(textSize);
    float textHeight = textPaint.descent() - textPaint.ascent();
    float textOffset = (textHeight / 2) - textPaint.descent();

    canvas.drawText(getResources().getString(R.string.root_waiting), x / 2, y - textOffset - y / 4, textPaint);
}
 
源代码8 项目: SublimePicker   文件: SimpleMonthView.java
private void drawDaysOfWeek(Canvas canvas) {
    final TextPaint p = mDayOfWeekPaint;
    final int headerHeight = mMonthHeight;
    final int rowHeight = mDayOfWeekHeight;
    final int colWidth = mCellWidth;

    // Text is vertically centered within the day of week height.
    final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
    final int rowCenter = headerHeight + rowHeight / 2;

    for (int col = 0; col < DAYS_IN_WEEK; col++) {
        final int colCenter = colWidth * col + colWidth / 2;
        final int colCenterRtl;
        if (SUtils.isLayoutRtlCompat(this)) {
            colCenterRtl = mPaddedWidth - colCenter;
        } else {
            colCenterRtl = colCenter;
        }

        final int dayOfWeek = (col + mWeekStart) % DAYS_IN_WEEK;
        final String label = getDayOfWeekLabel(dayOfWeek);
        canvas.drawText(label, colCenterRtl, rowCenter - halfLineHeight, p);
    }
}
 
private void drawDaysOfWeek(Canvas canvas) {
    final TextPaint p = mDayOfWeekPaint;
    final int headerHeight = mMonthHeight;
    final int rowHeight = mDayOfWeekHeight;
    final int colWidth = mCellWidth;

    // Text is vertically centered within the day of week height.
    final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
    final int rowCenter = headerHeight + rowHeight / 2;

    for (int col = 0; col < DAYS_IN_WEEK; col++) {
        final int colCenter = colWidth * col + colWidth / 2;
        final int colCenterRtl;
        if (ViewCompatUtils.isLayoutRtl(this)) {
            colCenterRtl = mPaddedWidth - colCenter;
        } else {
            colCenterRtl = colCenter;
        }

        final int dayOfWeek = (col + mWeekStart) % DAYS_IN_WEEK;
        final String label = getDayOfWeekLabel(dayOfWeek);
        canvas.drawText(label, colCenterRtl, rowCenter - halfLineHeight, p);
    }
}
 
源代码10 项目: talk-android   文件: RecipientEditTextView.java
/**
 * Given a height, returns a Y offset that will draw the text in the middle of the height.
 */
protected float getTextYOffset(String text, TextPaint paint, int height) {
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int textHeight = bounds.bottom - bounds.top;
    return height - ((height - textHeight) / 2) - (int) paint.descent() / 2;
}
 
源代码11 项目: talk-android   文件: TextChipsEditView.java
/**
 * Given a height, returns a Y offset that will draw the text in the middle of the height.
 */
protected float getTextYOffset(String text, TextPaint paint, int height) {
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int textHeight = bounds.bottom - bounds.top;
    return height - ((height - textHeight) / 2) - (int) paint.descent() / 2;
}
 
public TextFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    final Resources res = getResources();

    mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mTextPaint.density = res.getDisplayMetrics().density;
    mTextPaint.setTextAlign(Paint.Align.CENTER);
    yCenterOffset = -(int) ((mTextPaint.descent() + mTextPaint.ascent()) / 2);

    init(context, attrs, defStyleAttr);
}
 
源代码13 项目: ChipsLibrary   文件: RecipientEditTextView.java
private static float getTextYOffset(final String text,final TextPaint paint,final int height)
{
final Rect bounds=new Rect();
paint.getTextBounds(text,0,text.length(),bounds);
final int textHeight=bounds.bottom-bounds.top;
return height-(height-textHeight)/2-(int)paint.descent();
}
 
源代码14 项目: android_9.0.0_r45   文件: SimpleMonthView.java
/**
 * Draws the month days.
 */
private void drawDays(Canvas canvas) {
    final TextPaint p = mDayPaint;
    final int headerHeight = mMonthHeight + mDayOfWeekHeight;
    final int rowHeight = mDayHeight;
    final int colWidth = mCellWidth;

    // Text is vertically centered within the row height.
    final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
    int rowCenter = headerHeight + rowHeight / 2;

    for (int day = 1, col = findDayOffset(); day <= mDaysInMonth; day++) {
        final int colCenter = colWidth * col + colWidth / 2;
        final int colCenterRtl;
        if (isLayoutRtl()) {
            colCenterRtl = mPaddedWidth - colCenter;
        } else {
            colCenterRtl = colCenter;
        }

        int stateMask = 0;

        final boolean isDayEnabled = isDayEnabled(day);
        if (isDayEnabled) {
            stateMask |= StateSet.VIEW_STATE_ENABLED;
        }

        final boolean isDayActivated = mActivatedDay == day;
        final boolean isDayHighlighted = mHighlightedDay == day;
        if (isDayActivated) {
            stateMask |= StateSet.VIEW_STATE_ACTIVATED;

            // Adjust the circle to be centered on the row.
            final Paint paint = isDayHighlighted ? mDayHighlightSelectorPaint :
                    mDaySelectorPaint;
            canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, paint);
        } else if (isDayHighlighted) {
            stateMask |= StateSet.VIEW_STATE_PRESSED;

            if (isDayEnabled) {
                // Adjust the circle to be centered on the row.
                canvas.drawCircle(colCenterRtl, rowCenter,
                        mDaySelectorRadius, mDayHighlightPaint);
            }
        }

        final boolean isDayToday = mToday == day;
        final int dayTextColor;
        if (isDayToday && !isDayActivated) {
            dayTextColor = mDaySelectorPaint.getColor();
        } else {
            final int[] stateSet = StateSet.get(stateMask);
            dayTextColor = mDayTextColor.getColorForState(stateSet, 0);
        }
        p.setColor(dayTextColor);

        canvas.drawText(mDayFormatter.format(day), colCenterRtl, rowCenter - halfLineHeight, p);

        col++;

        if (col == DAYS_IN_WEEK) {
            col = 0;
            rowCenter += rowHeight;
        }
    }
}
 
源代码15 项目: AndroidWM   文件: BitmapUtils.java
/**
 * build a bitmap from a text.
 *
 * @return {@link Bitmap} the bitmap return.
 */
public static Bitmap textAsBitmap(Context context, WatermarkText watermarkText) {
    TextPaint watermarkPaint = new TextPaint();
    watermarkPaint.setColor(watermarkText.getTextColor());
    watermarkPaint.setStyle(watermarkText.getTextStyle());

    if (watermarkText.getTextAlpha() >= 0 && watermarkText.getTextAlpha() <= 255) {
        watermarkPaint.setAlpha(watermarkText.getTextAlpha());
    }

    float value = (float) watermarkText.getTextSize();
    int pixel = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            value, context.getResources().getDisplayMetrics());
    watermarkPaint.setTextSize(pixel);

    if (watermarkText.getTextShadowBlurRadius() != 0
            || watermarkText.getTextShadowXOffset() != 0
            || watermarkText.getTextShadowYOffset() != 0) {
        watermarkPaint.setShadowLayer(watermarkText.getTextShadowBlurRadius(),
                watermarkText.getTextShadowXOffset(),
                watermarkText.getTextShadowYOffset(),
                watermarkText.getTextShadowColor());
    }

    if (watermarkText.getTextFont() != 0) {
        Typeface typeface = ResourcesCompat.getFont(context, watermarkText.getTextFont());
        watermarkPaint.setTypeface(typeface);
    }

    watermarkPaint.setAntiAlias(true);
    watermarkPaint.setTextAlign(Paint.Align.LEFT);
    watermarkPaint.setStrokeWidth(5);

    float baseline = (int) (-watermarkPaint.ascent() + 1f);
    Rect bounds = new Rect();
    watermarkPaint.getTextBounds(watermarkText.getText(),
            0, watermarkText.getText().length(), bounds);

    int boundWidth = bounds.width() + 20;
    int mTextMaxWidth = (int) watermarkPaint.measureText(watermarkText.getText());
    if (boundWidth > mTextMaxWidth) {
        boundWidth = mTextMaxWidth;
    }
    StaticLayout staticLayout = new StaticLayout(watermarkText.getText(),
            0, watermarkText.getText().length(),
            watermarkPaint, mTextMaxWidth, android.text.Layout.Alignment.ALIGN_NORMAL, 2.0f,
            2.0f, false);

    int lineCount = staticLayout.getLineCount();
    int height = (int) (baseline + watermarkPaint.descent() + 3) * lineCount;
    Bitmap image = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    if (boundWidth > 0 && height > 0) {
        image = Bitmap.createBitmap(boundWidth, height, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(image);
    canvas.drawColor(watermarkText.getBackgroundColor());
    staticLayout.draw(canvas);
    return image;
}
 
源代码16 项目: DateTimePicker   文件: SimpleMonthView.java
/**
 * Draws the month days.
 */
private void drawDays(Canvas canvas) {
    final TextPaint p = mDayPaint;
    final int headerHeight = mMonthHeight + mDayOfWeekHeight;
    final int rowHeight = mDayHeight;
    final int colWidth = mCellWidth;

    // Text is vertically centered within the row height.
    final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
    int rowCenter = headerHeight + rowHeight / 2;

    for (int day = 1, col = findDayOffset(); day <= mDaysInMonth; day++) {
        final int colCenter = colWidth * col + colWidth / 2;
        final int colCenterRtl;
        if (isLayoutRtl()) {
            colCenterRtl = mPaddedWidth - colCenter;
        } else {
            colCenterRtl = colCenter;
        }

        int stateMask = 0;

        final boolean isDayEnabled = isDayEnabled(day);
        if (isDayEnabled) {
            stateMask |= StateSet.VIEW_STATE_ENABLED;
        }

        final boolean isDayActivated = mActivatedDay == day;
        final boolean isDayHighlighted = mHighlightedDay == day;
        if (isDayActivated) {
            stateMask |= StateSet.VIEW_STATE_ACTIVATED;

            // Adjust the circle to be centered on the row.
            final Paint paint = isDayHighlighted ? mDayHighlightSelectorPaint :
                    mDaySelectorPaint;
            canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, paint);
        } else if (isDayHighlighted) {
            stateMask |= StateSet.VIEW_STATE_PRESSED;

            if (isDayEnabled) {
                // Adjust the circle to be centered on the row.
                canvas.drawCircle(colCenterRtl, rowCenter,
                        mDaySelectorRadius, mDayHighlightPaint);
            }
        }

        final boolean isDayToday = mToday == day;
        final int dayTextColor;
        if (isDayToday && !isDayActivated) {
            dayTextColor = mDaySelectorPaint.getColor();
        } else {
            final int[] stateSet = StateSet.get(stateMask);
            dayTextColor = mDayTextColor.getColorForState(stateSet, 0);
        }
        p.setColor(dayTextColor);

        canvas.drawText(mDayFormatter.format(day), colCenterRtl, rowCenter - halfLineHeight, p);

        col++;

        if (col == DAYS_IN_WEEK) {
            col = 0;
            rowCenter += rowHeight;
        }
    }
}
 
源代码17 项目: WatermarkCreator   文件: WaterMarkCreator.java
public Bitmap convertToBitmapFromText() {
    if (mTextSize == 0.0F) {
        throw new WaterMarkCreatorException("Did not provide the text size");
    }
    if (mTextColor == 0) {
        throw new WaterMarkCreatorException("Did not provide the text color");
    }

    TextPaint paint = new TextPaint();
    paint.setColor(mTextColor);
    paint.setTextSize(mTextSize);
    paint.setStrokeWidth(5);
    paint.setTypeface(Typeface.MONOSPACE);

    paint.setAntiAlias(true);
    paint.setTextAlign(Paint.Align.LEFT);

    //ascent : The recommended distance above the baseline for singled spaced text
    float baseline = (int) (-paint.ascent() + 3f); // ascent() is negative
    Log.e("test", " " + paint.ascent() + " baseline: " + baseline);

    // First decode with Rect to check dimensions
    Rect bounds = new Rect();
    paint.getTextBounds(mText.toString(), 0, mText.length(), bounds);

    int boundWidth = bounds.width() + MARGIN_RIGHT;
    // mRequestWidth must be in pixels
    if (boundWidth > mTextMaxWidth) {
        boundWidth = mTextMaxWidth;
    }
    StaticLayout staticLayout = new StaticLayout(mText, 0, mText.length(),
            paint, mTextMaxWidth, android.text.Layout.Alignment.ALIGN_NORMAL, 1.0f,
            1.0f, false);

    int lineCount = staticLayout.getLineCount();
    //Descent: The recommended distance below the baseline for singled spaced text
    int height = (int) (baseline + paint.descent() + 3) * lineCount + 10;

    Bitmap image = Bitmap.createBitmap(boundWidth, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    canvas.drawARGB(0xFF, 0xFF, 0xFF, 0xFF);
    staticLayout.draw(canvas);
    return image;
}
 
/**
 * Draws the month days.
 */
private void drawDays(Canvas canvas) {
    final TextPaint p = mDayPaint;
    final int headerHeight = mMonthHeight + mDayOfWeekHeight;
    final int rowHeight = mDayHeight;
    final int colWidth = mCellWidth;

    // Text is vertically centered within the row height.
    final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
    int rowCenter = headerHeight + rowHeight / 2;

    for (int day = 1, col = findDayOffset(); day <= mDaysInMonth; day++) {
        final int colCenter = colWidth * col + colWidth / 2;
        final int colCenterRtl;
        if (ViewCompatUtils.isLayoutRtl(this)) {
            colCenterRtl = mPaddedWidth - colCenter;
        } else {
            colCenterRtl = colCenter;
        }

        int state = 0;

        final boolean isDayEnabled = isDayEnabled(day);
        final boolean isDayActivated = mActivatedDay == day;
        if (isDayActivated) {
            state = VIEW_STATE_SELECTED;

            // Adjust the circle to be centered on the row.
            canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDaySelectorPaint);
        } else if (mTouchedItem == day) {
            state = VIEW_STATE_PRESSED;

            if (isDayEnabled) {
                // Adjust the circle to be centered on the row.
                canvas.drawCircle(colCenterRtl, rowCenter,
                        mDaySelectorRadius, mDayHighlightPaint);
            }
        }

        final boolean isDayToday = mToday == day;
        final int dayTextColor;
        if (isDayToday && !isDayActivated) {
            dayTextColor = mDaySelectorPaint.getColor();
        } else {
            final int[] stateSet = buildState(isDayEnabled, state);
            dayTextColor = mDayTextColor.getColorForState(stateSet, 0);
        }
        p.setColor(dayTextColor);

        canvas.drawText(mDayFormatter.format(day), colCenterRtl, rowCenter - halfLineHeight, p);

        col++;

        if (col == DAYS_IN_WEEK) {
            col = 0;
            rowCenter += rowHeight;
        }
    }
}