下面列出了android.text.TextPaint#ascent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
/**
* 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;
}
}
private void initIfNecessary(TextPaint tp) {
if (jumpAnimator != null) {
return;
}
shift = (int) tp.ascent() / 2;
jumpAnimator = ValueAnimator.ofInt(0, shift, 0);
jumpAnimator
.setDuration(loopDuration)
.setStartDelay(delay);
jumpAnimator.setInterpolator(new JumpInterpolator(animatedRange));
jumpAnimator.setRepeatCount(ValueAnimator.INFINITE);
jumpAnimator.setRepeatMode(ValueAnimator.RESTART);
jumpAnimator.addUpdateListener(this);
jumpAnimator.start();
}
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);
}
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);
}
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);
}
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTextSize(tp.getTextSize() * relativeSize);
tp.baselineShift += (int) (tp.ascent() * relativeSize) / 4;
}
@Override
public void updateDrawState(TextPaint tp) {
tp.baselineShift -= (int) (tp.ascent() / 2);
}
@Override
public void updateDrawState(@NonNull TextPaint textPaint) {
textPaint.baselineShift += (int) (textPaint.ascent() / 2);
}
@Override
public void updateMeasureState(@NonNull TextPaint textPaint) {
textPaint.baselineShift += (int) (textPaint.ascent() / 2);
}
@Override
public void updateMeasureState(TextPaint tp) {
tp.baselineShift -= (int) (tp.ascent() / 2);
}
/**
* 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;
}
}
}
@Override
public void updateMeasureState(final TextPaint paint) {
paint.baselineShift += (int) (paint.ascent() * rate);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.baselineShift += (int) (tp.ascent() / 2);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.baselineShift -= (int) (tp.ascent() / 2);
}
@Override
public void updateDrawState(@NonNull final TextPaint paint) {
paint.baselineShift += (int) (paint.ascent() * RATE);
}
private void apply(TextPaint paint) {
paint.setTextSize(paint.getTextSize() * HtmlPlugin.SCRIPT_DEF_TEXT_SIZE_RATIO);
paint.baselineShift += (int) (paint.ascent() / 2);
}
@Override
public void updateMeasureState(TextPaint tp) {
tp.baselineShift -= (int) (tp.ascent() / 2);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.baselineShift += (int) (tp.ascent() / 2);
}
/**
* 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;
}
}
}