下面列出了android.graphics.Paint#descent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void drawLanguageOnSpacebar(final Key key, final Canvas canvas, final Paint paint) {
final Keyboard keyboard = getKeyboard();
if (keyboard == null) {
return;
}
final int width = key.getWidth();
final int height = key.getHeight();
paint.setTextAlign(Align.CENTER);
paint.setTypeface(Typeface.DEFAULT);
paint.setTextSize(mLanguageOnSpacebarTextSize);
final String language = layoutLanguageOnSpacebar(paint, keyboard.mId.mSubtype, width);
// Draw language text with shadow
final float descent = paint.descent();
final float textHeight = -paint.ascent() + descent;
final float baseline = height / 2 + textHeight / 2;
paint.setColor(mLanguageOnSpacebarTextColor);
paint.setAlpha(mLanguageOnSpacebarAnimAlpha);
canvas.drawText(language, width / 2, baseline - descent, paint);
paint.clearShadowLayer();
paint.setTextScaleX(1.0f);
}
/**
* 绘制右侧索引条文本
*
* @param canvas
*/
private void onDrawRightIndexText(Canvas canvas) {
L.d(LOGTAG, "onDrawRightIndexText");
// 绘画右侧索引条的字母
Paint indexPaint = new Paint();
indexPaint.setColor(Color.parseColor(COLOR_RIGHT_TEXT));
// indexPaint.setAlpha((int) (255 * mAlphaRate));
indexPaint.setAntiAlias(true);
indexPaint.setTextSize(14 * mScaledDensity);
float sectionHeight = (mIndexbarRect.height() - 2 * mIndexbarMargin)
/ mSections.length;
float paddingTop = (sectionHeight - (indexPaint.descent() - indexPaint
.ascent())) / 2;
for (int i = 0; i < mSections.length; i++) {
float paddingLeft = (mIndexbarWidth - indexPaint
.measureText(mSections[i])) / 2;
canvas.drawText(mSections[i], mIndexbarRect.left + paddingLeft,
mIndexbarRect.top + mIndexbarMargin + sectionHeight * i
+ paddingTop - indexPaint.ascent(), indexPaint);
}
}
private Bitmap getIconFromMinutes(Times t) {
int left = new Period(LocalDateTime.now(), t.getTime(LocalDate.now(), t.getNextTime()), PeriodType.minutes()).getMinutes();
Resources r = getContext().getResources();
int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, r.getDisplayMetrics());
Bitmap b = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(0xFFFFFFFF);
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(size);
paint.setTextSize(size * size / paint.measureText((left < 10 ? left * 10 : left) + ""));
float yPos = c.getHeight() / 2f - (paint.descent() + paint.ascent()) / 2;
c.drawText(left + "", size / 2f, yPos, paint);
return b;
}
@Override
public void draw(@NonNull Canvas canvas, CharSequence text,
int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
boolean isLeftEdge = x == 0.0f;
if (backgroundColor != 0) {
int extra1Dp = (int) (((TextPaint) paint).density * 1 + 0.5f);
float width = paint.measureText(text.subSequence(start, end).toString());
int newTop = (int) (bottom - paint.getFontSpacing() - paint.descent()) + 2 * extra1Dp;
int newBottom = bottom - extra1Dp;
int newLeft = (int) (isLeftEdge ? x : x - radius);
int newRight = (int) (isLeftEdge ? x + width + 2 * radius : x + width + radius);
RectF rect = new RectF(newLeft, newTop, newRight, newBottom);
paint.setColor(backgroundColor);
canvas.drawRoundRect(rect, radius, radius, paint);
}
if (textColor != 0) {
float textX = isLeftEdge ? x + radius : x;
paint.setColor(textColor);
canvas.drawText(text, start, end, textX, y, paint);
}
}
/**
* Create a bitmap from a text
*
* @param text
* Text which the bitmap is created for
* @param textSize
* Text size
* @param textColor
* Text color
* @param typeface
* Typeface of text
*
* @return a bitmap on which is text is drawn
*/
public static Bitmap createBitmapFromText(String text, float textSize, int textColor,
Typeface typeface) {
Paint paint = new Paint(ANTI_ALIAS_FLAG);
paint.setTextSize(textSize);
paint.setColor(textColor);
paint.setTypeface(typeface);
paint.setTextAlign(Paint.Align.LEFT);
float baseline = -paint.ascent();
int width = (int) (paint.measureText(text) + 0.5f);
int height = (int) (baseline + paint.descent() + 0.5f);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text, 0, baseline, paint);
return bitmap;
}
/**
* Calculate the bounds for a view's title
*
* @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;
}
private static Bitmap textAsBitmap(String text, float textSize, int textColor) {
Paint paint = new Paint();
paint.setTextSize(textSize);
paint.setColor(textColor);
paint.setTextAlign(Paint.Align.LEFT);
int width = (int) (paint.measureText(text) + 0.5f); // round
float baseline = (int) (-paint.ascent() + 0.5f); // ascent() is negative
int height = (int) (baseline + paint.descent() + 0.5f);
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
canvas.drawText(text, 0, baseline, paint);
return image;
}
/**
* Calculate the bounds for a view's title
*
* @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;
}
/**
* 绘制中间预览view
*
* @param canvas
*/
private void onDrawMiddlePreview(Canvas canvas) {
L.d(LOGTAG, "onDrawMiddlePreview");
Paint previewPaint = new Paint(); // 用来绘画预览文本背景的画笔
previewPaint.setColor(Color.parseColor(COLOR_MIDDLE_BACKGROUND));// 设置画笔颜色为黑色
previewPaint.setAlpha(96); // 设置透明度
previewPaint.setAntiAlias(true);// 设置抗锯齿
previewPaint.setShadowLayer(3, 0, 0, Color.argb(64, 0, 0, 0)); // 设置阴影层
Paint previewTextPaint = new Paint(); // 用来绘画预览字母的画笔
previewTextPaint.setColor(Color.parseColor(COLOR_MIDDLE_TEXT)); // 设置画笔为白色
previewTextPaint.setAntiAlias(true); // 设置抗锯齿
previewTextPaint.setTextSize(60 * mScaledDensity); // 设置字体大小
// 单个文本的宽度
float previewTextWidth = previewTextPaint
.measureText(mSections[mCurrentSection]);
float previewSize = 2 * mPreviewPadding + previewTextPaint.descent()
- previewTextPaint.ascent();
RectF previewRect = new RectF((mListViewWidth - previewSize) / 2,
(mListViewHeight - previewSize) / 2,
(mListViewWidth - previewSize) / 2 + previewSize,
(mListViewHeight - previewSize) / 2 + previewSize);
// 中间索引的那个框
canvas.drawRoundRect(previewRect, 5 * mDensity, 5 * mDensity,
previewPaint);
// 绘画索引字母
canvas.drawText(mSections[mCurrentSection], previewRect.left
+ (previewSize - previewTextWidth) / 2 - 1, previewRect.top
+ mPreviewPadding - previewTextPaint.ascent() + 1,
previewTextPaint);
}
/**
* Calculate the bounds for a view's title
*
* @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;
}
/**
* Calculate the bounds for a view's title
*
* @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;
}
private Rect a(int i1, Paint paint)
{
Rect rect = new Rect();
CharSequence charsequence = a(i1);
rect.right = (int)paint.measureText(charsequence, 0, charsequence.length());
rect.bottom = (int)(paint.descent() - paint.ascent());
return rect;
}
/**
* Calculate the bounds for a view's title
*
* @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;
}
/**
* Calculate the bounds for a view's title
*
* @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;
}
protected void drawBottomNumberText(Canvas canvas, String text, Paint textPaint) {
float strWidth = textPaint.measureText(text);
int xPos = (canvas.getWidth() / 2) - (int) strWidth / 2;
int yPos = (int) ((canvas.getHeight()) + ((textPaint.descent() + textPaint.ascent()) / 2) / 2);
canvas.drawText(text, xPos, yPos, textPaint);
}
public static float getTextCenterY(int centerY, Paint paint){
return centerY-((paint.descent() + paint.ascent()) / 2);
}
private void initAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CountDownProgressBar, defStyleAttr, 0);
int indexCount = typedArray.getIndexCount();
for (int i = 0; i < indexCount; i++) {
int attr = typedArray.getIndex(i);
switch (attr) {
case R.styleable.CountDownProgressBar_maxProress:
mMaxProgress = typedArray.getInt(attr, 100);
break;
case R.styleable.CountDownProgressBar_circleColor:
mCircleColor = typedArray.getColor(attr, 0xff0000);
break;
case R.styleable.CountDownProgressBar_textColor:
mTextColor = typedArray.getColor(attr, 0xff0000);
break;
case R.styleable.CountDownProgressBar_circleWidth:
mCircleWidth = typedArray.getDimensionPixelSize(attr, 3);
break;
case R.styleable.CountDownProgressBar_backgroundWidth:
mBackgroundWidth = typedArray.getDimensionPixelSize(attr, 10);
break;
case R.styleable.CountDownProgressBar_currentProgress:
mCurrentProgress = typedArray.getInt(attr, 0);
break;
case R.styleable.CountDownProgressBar_backgroundColor:
mBackgroundColor = typedArray.getColor(attr, 0xcfcfcf);
break;
case R.styleable.CountDownProgressBar_textSize:
mTextSize = typedArray.getDimensionPixelSize(attr, 40);
break;
}
}
typedArray.recycle();
mBgPaint = new Paint();
mBgPaint.setColor(mBackgroundColor);
mBgPaint.setStyle(Paint.Style.FILL);
mBgPaint.setAntiAlias(true);
mBgPaint.setStrokeWidth(mBackgroundWidth / 2);
mProgressPaint = new Paint();
mProgressPaint.setColor(mCircleColor);
mProgressPaint.setStyle(Paint.Style.STROKE);
mProgressPaint.setAntiAlias(true);
mProgressPaint.setStrokeWidth(mCircleWidth);
mTextPaint = new Paint();
mTextPaint.setColor(mTextColor);
mTextPaint.setTextSize(mTextSize);
mTextPaint.setTypeface(Typeface.DEFAULT);
mTextPaint.setTextAlign(Paint.Align.LEFT);
mTextRect = new Rect();
mTextPaint.getTextBounds(content, 0, content.length(), mTextRect);
mTextY = mTextPaint.descent() - mTextPaint.ascent();
rectF = new RectF(5, 5, mBackgroundWidth - 5, mBackgroundWidth - 5);
mStartAngel = mCurrentProgress * 360 / 100 - 90;
}
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(canvas, parent, state);
float scaledWidth = ((FastScrollRecyclerView)parent).scaledWidth;
float sx = ((FastScrollRecyclerView)parent).sx;
float scaledHeight= ((FastScrollRecyclerView)parent).scaledHeight;
float sy = ((FastScrollRecyclerView)parent).sy;
String[] sections = ((FastScrollRecyclerView)parent).sections;
String section = ((FastScrollRecyclerView)parent).section;
boolean showLetter = ((FastScrollRecyclerView)parent).showLetter;
// We draw the letter in the middle
if (showLetter & section != null && !section.equals("")) {
//overlay everything when displaying selected index Letter in the middle
Paint overlayDark = new Paint();
overlayDark.setColor(Color.BLACK);
overlayDark.setAlpha(100);
canvas.drawRect(0, 0, parent.getWidth(), parent.getHeight(), overlayDark);
float middleTextSize = mContext.getResources().getDimension(R.dimen.fast_scroll_overlay_text_size);
Paint middleLetter = new Paint();
middleLetter.setColor(Color.WHITE);
middleLetter.setTextSize(middleTextSize);
middleLetter.setAntiAlias(true);
middleLetter.setFakeBoldText(true);
middleLetter.setStyle(Paint.Style.FILL);
int xPos = (canvas.getWidth() - (int)middleTextSize)/ 2;
int yPos = (int) ((canvas.getHeight() / 2) - ((middleLetter.descent() + middleLetter.ascent()) / 2));
canvas.drawText(section.toUpperCase(), xPos, yPos, middleLetter);
}
// draw indez A-Z
Paint textPaint = new Paint();
textPaint.setAntiAlias(true);
textPaint.setStyle(Paint.Style.FILL);
for (int i = 0; i < sections.length; i++) {
if(showLetter & section != null && !section.equals("") && section!=null
&& sections[i].toUpperCase().equals(section.toUpperCase())) {
textPaint.setColor(Color.WHITE);
textPaint.setAlpha(255);
textPaint.setFakeBoldText(true);
textPaint.setTextSize((float)(scaledWidth / 2));
canvas.drawText(sections[i].toUpperCase(),
sx + textPaint.getTextSize() / 2, sy + parent.getPaddingTop()
+ scaledHeight * (i + 1), textPaint);
textPaint.setTextSize((float)(scaledWidth));
canvas.drawText("•",
sx - textPaint.getTextSize()/3, sy+parent.getPaddingTop()
+ scaledHeight * (i + 1) + scaledHeight/3, textPaint);
} else {
textPaint.setColor(Color.LTGRAY);
textPaint.setAlpha(200);
textPaint.setFakeBoldText(false);
textPaint.setTextSize(scaledWidth / 2);
canvas.drawText(sections[i].toUpperCase(),
sx + textPaint.getTextSize() / 2, sy + parent.getPaddingTop()
+ scaledHeight * (i + 1), textPaint);
}
}
}
protected void drawCenterNumberText(Canvas canvas, String text, Paint textPaint) {
int xPos = (canvas.getWidth() / 2);
int yPos = (int) ((canvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2));
//((textPaint.descent() + textPaint.ascent()) / 2) is the distance from the baseline to the center.
canvas.drawText(text, xPos, yPos, textPaint);
}
private static Bitmap getBitmap(String name, int size) {
Random random = new Random(name.hashCode());
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
// Let the name be the first and last capital letters
Character first = null;
Character last = null;
char c;
for (int i = 0; i < name.length(); i++) {
c = name.charAt(i);
if (Character.isUpperCase(c)) {
if (first == null)
first = c;
else
last = c;
}
}
if (first == null) {
if (name.isEmpty()) {
name = "";
} else {
name = String.valueOf(name.charAt(0)).toUpperCase();
}
} else {
name = String.valueOf(first);
if (last != null)
name += String.valueOf(last);
}
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(MATERIAL_COLORS[random.nextInt(MATERIAL_COLORS.length)]);
paint.setStyle(Paint.Style.FILL);
canvas.drawPaint(paint);
// Center text: http://stackoverflow.com/a/11121873/4759433
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setTextSize(size / (float) name.length());
paint.setTextAlign(Paint.Align.CENTER);
float xPos = (canvas.getWidth() / 2f);
float yPos = (canvas.getHeight() / 2f) - ((paint.descent() + paint.ascent()) / 2f);
canvas.drawText(name, xPos, yPos, paint);
return bitmap;
}