下面列出了怎么用android.graphics.Paint.FontMetrics的API类实例代码及写法,或者点击链接到github查看源代码。
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF rectF = new RectF(2.0f, 2.0f, (float) (getWidth() - 2), (float) (getWidth() - 2));
if (this.mState == com.fimi.album.widget.DownloadStateView.State.PAUSE) {
setBackgroundResource(R.drawable.album_btn_media_pause);
canvas.drawArc(rectF, -90.0f, (float) this.sweepAngle, false, this.mPaint);
} else if (this.mState == com.fimi.album.widget.DownloadStateView.State.DOWNLOADING) {
String text = Math.round(((float) this.sweepAngle) / 3.6f) + "%";
FontMetrics fontMetrics = this.mTextPaint.getFontMetrics();
canvas.drawText(text, (float) (getWidth() / 2), (((float) getHeight()) - ((((float) getHeight()) - (fontMetrics.bottom - fontMetrics.top)) / 2.0f)) - fontMetrics.bottom, this.mTextPaint);
setBackgroundResource(R.drawable.album_btn_media_detail_download);
this.mPaint.setColor(DOWNLOADING_COLOR);
canvas.drawArc(rectF, -90.0f, (float) this.sweepAngle, false, this.mPaint);
} else if (this.mState == com.fimi.album.widget.DownloadStateView.State.DOWNLOAD_FAIL) {
setBackgroundResource(R.drawable.album_btn_media_redownload);
this.mPaint.setColor(DOWNLOAD_FAIL_COLOR);
canvas.drawArc(rectF, -90.0f, (float) this.sweepAngle, false, this.mPaint);
}
}
private void drawText(Canvas canvas) {
float heightValue = getRegulationProgress();
FontMetrics fontMetrics = this.mTextPaint.getFontMetrics();
float dy = ((fontMetrics.descent - fontMetrics.ascent) / 2.0f) - fontMetrics.descent;
int intHeightValue = (int) heightValue;
String text = X8NumberUtil.getDistanceNumberString((float) intHeightValue, 0, true);
this.mTextPaint.getTextBounds(text, (int) this.rectangleLeft, text.length(), this.mBounds);
float textWidth = (float) this.mBounds.width();
float textHeight = (float) this.mBounds.height();
RectF rectF = new RectF();
rectF.left = this.rectangleLeft;
rectF.top = (((float) (getHeight() / 2)) - (textHeight / 2.0f)) - dy;
rectF.right = (this.rectangleLeft + textWidth) + (2.0f * dy);
rectF.bottom = (((float) (getHeight() / 2)) + (textHeight / 2.0f)) + dy;
canvas.drawRoundRect(rectF, 3.0f, 3.0f, this.mTextBgPaint);
canvas.drawText(text, this.rectangleLeft, ((float) (getHeight() / 2)) + (textHeight / 2.0f), this.mTextPaint);
if (this.listener != null) {
this.listener.onProgressChanage((float) intHeightValue);
}
}
/**
* Constructs the generator and initializes the common members ignoring display density.
*
* @param iconWidthPx The width of the generated icon in pixels.
* @param iconHeightPx The height of the generated icon in pixels.
* @param cornerRadiusPx The radius of the corners in the icon in pixels.
* @param backgroundColor Color at which the rounded rectangle should be drawn.
* @param textSizePx Size at which the text should be drawn in pixels.
*/
public RoundedIconGenerator(int iconWidthPx, int iconHeightPx, int cornerRadiusPx,
int backgroundColor, float textSizePx) {
mIconWidthPx = iconWidthPx;
mIconHeightPx = iconHeightPx;
mCornerRadiusPx = cornerRadiusPx;
mBackgroundRect = new RectF(0, 0, mIconWidthPx, mIconHeightPx);
mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBackgroundPaint.setColor(backgroundColor);
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(Color.WHITE);
mTextPaint.setFakeBoldText(true);
mTextPaint.setTextSize(textSizePx);
FontMetrics textFontMetrics = mTextPaint.getFontMetrics();
mTextHeight = (float) Math.ceil(textFontMetrics.bottom - textFontMetrics.top);
mTextYOffset = -textFontMetrics.top;
}
/**
* Constructs the generator and initializes the common members ignoring display density.
*
* @param iconWidthPx The width of the generated icon in pixels.
* @param iconHeightPx The height of the generated icon in pixels.
* @param cornerRadiusPx The radius of the corners in the icon in pixels.
* @param backgroundColor Color at which the rounded rectangle should be drawn.
* @param textSizePx Size at which the text should be drawn in pixels.
*/
public RoundedIconGenerator(int iconWidthPx, int iconHeightPx, int cornerRadiusPx,
int backgroundColor, float textSizePx) {
mIconWidthPx = iconWidthPx;
mIconHeightPx = iconHeightPx;
mCornerRadiusPx = cornerRadiusPx;
mBackgroundRect = new RectF(0, 0, mIconWidthPx, mIconHeightPx);
mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBackgroundPaint.setColor(backgroundColor);
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(Color.WHITE);
mTextPaint.setFakeBoldText(true);
mTextPaint.setTextSize(textSizePx);
FontMetrics textFontMetrics = mTextPaint.getFontMetrics();
mTextHeight = (float) Math.ceil(textFontMetrics.bottom - textFontMetrics.top);
mTextYOffset = -textFontMetrics.top;
}
private void init(){
mWidth = (int) getPaint().measureText(getText().toString());
FontMetrics fm = getPaint().getFontMetrics();
mFontHeiht = (float)Math.ceil(fm.descent - fm.ascent);
int horizontalPadding = getHorizontalPaddint();
int verticalPadding = getVerticalPaddint();
mPath.reset();
mPath.moveTo(0, 0);
mPath.lineTo(mWidth + horizontalPadding, 0);
mPath.lineTo(mWidth + horizontalPadding, mFontHeiht + verticalPadding);
mPath.lineTo(25, mFontHeiht + verticalPadding);
mPath.lineTo(0, mFontHeiht+verticalPadding+25);
mPath.close();
mPaint.setAntiAlias(true);
mPaint.setColor(ResFinder.getColor("umeng_comm_topic_tip_bg"));
mPaint.setStyle(Style.FILL);
getPaint().setColor(Color.WHITE);// 不知道什么原因,xml设置无效。暂时先在代码中设置。
}
/**
* Constructs the generator and initializes the common members ignoring display density.
*
* @param iconWidthPx The width of the generated icon in pixels.
* @param iconHeightPx The height of the generated icon in pixels.
* @param cornerRadiusPx The radius of the corners in the icon in pixels.
* @param backgroundColor Color at which the rounded rectangle should be drawn.
* @param textSizePx Size at which the text should be drawn in pixels.
*/
public RoundedIconGenerator(int iconWidthPx, int iconHeightPx, int cornerRadiusPx,
int backgroundColor, float textSizePx) {
mIconWidthPx = iconWidthPx;
mIconHeightPx = iconHeightPx;
mCornerRadiusPx = cornerRadiusPx;
mBackgroundRect = new RectF(0, 0, mIconWidthPx, mIconHeightPx);
mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBackgroundPaint.setColor(backgroundColor);
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(Color.WHITE);
mTextPaint.setFakeBoldText(true);
mTextPaint.setTextSize(textSizePx);
FontMetrics textFontMetrics = mTextPaint.getFontMetrics();
mTextHeight = (float) Math.ceil(textFontMetrics.bottom - textFontMetrics.top);
mTextYOffset = -textFontMetrics.top;
}
/**
* @see android.view.ViewGroup#dispatchDraw(Canvas)
*/
@Override
protected void dispatchDraw(Canvas canvas) {
if (mState == STATE_REFRESHING) {
//
// float centerX = mArrowImageView.getMeasuredWidth() / 2;
// float centerY = mArrowImageView.getMeasuredHeight() / 2;
// Matrix matrix = mArrowImageView.getImageMatrix();
// mArrowImageView.setScaleType(ScaleType.MATRIX);
// matrix.postRotate(25, centerX, centerY);
// mArrowImageView.setImageMatrix(matrix);
}
super.dispatchDraw(canvas);
if (isInEditMode()) {
return;
}
FontMetrics fontMetrics = mPaint.getFontMetrics();
// 计算文字高度
float fontHeight = fontMetrics.bottom - fontMetrics.top;
// 计算文字baseline
float textBaseY = getHeight() - (getHeight() - fontHeight) / 2 - fontMetrics.bottom;
canvas.drawText(mstrTitle, getWidth() / 2 + mArrowImageView.getMeasuredWidth(), textBaseY, mPaint);
}
@Override
protected void onDraw(Canvas canvas)
{
Paint mPaint = getPaint();
//Get font's height & leading:
FontMetrics fm = mPaint.getFontMetrics();
float height = fm.descent - fm.ascent + fm.leading;
float x = 0;
float y = height;
String[] textLines = autoSplit(getText().toString(), mPaint, getWidth());
for (String textLine : textLines)
{
canvas.drawText(textLine, x, y, mPaint);
y += height;
}
}
public void onDraw(Canvas canvas) {
int position = (getWidth() * this.percent) / 100;
if (((float) position) + this.photoWidth > ((float) getWidth())) {
position = (int) (((float) getWidth()) - this.photoWidth);
}
canvas.drawRoundRect(new RectF((float) position, 0.0f, ((float) position) + this.photoWidth, (float) getHeight()), 2.0f, 2.0f, this.mPaint);
canvas.drawRoundRect(new RectF((float) position, 0.0f, ((float) position) + this.photoWidth, (float) (getHeight() - 1)), 2.0f, 2.0f, this.mPaintStrock);
float Textx = (((float) position) + (this.photoWidth / 2.0f)) - (this.mPaintText.measureText(this.mStrTime) / 2.0f);
FontMetrics fontMetrics = this.mPaintText.getFontMetrics();
canvas.drawText(this.mStrTime, Textx, ((float) (getHeight() / 2)) + (((fontMetrics.descent - fontMetrics.ascent) / 2.0f) - fontMetrics.descent), this.mPaintText);
}
/**
* text height
*
* @param fontSize fontsize
* @return fontsize `s height
*/
public int getFontHeight(float fontSize) {
Paint paint = new Paint();
paint.setTextSize(fontSize);
FontMetrics fm = paint.getFontMetrics();
return (int) Math.ceil(fm.descent - fm.ascent);
}
protected Float getCacheHeight(BaseDanmaku danmaku, Paint paint) {
Float textSize = Float.valueOf(paint.getTextSize());
Float textHeight = (Float) sTextHeightCache.get(textSize);
if (textHeight != null) {
return textHeight;
}
FontMetrics fontMetrics = paint.getFontMetrics();
textHeight = Float.valueOf((fontMetrics.descent - fontMetrics.ascent) + fontMetrics.leading);
sTextHeightCache.put(textSize, textHeight);
return textHeight;
}
/**
* text height
*
* @param fontSize fontsize
* @return fontsize `s height
*/
public int getFontHeight(float fontSize)
{
Paint paint = new Paint();
paint.setTextSize(fontSize);
FontMetrics fm = paint.getFontMetrics();
return (int)Math.ceil(fm.descent - fm.ascent);
}
@Override
public View onCreateView(ViewGroup parent) {
View view = super.onCreateView(parent);
if (mNoBottomSpacing) {
ApiCompatibilityUtils.setPaddingRelative(
view,
ApiCompatibilityUtils.getPaddingStart(view),
view.getPaddingTop(),
ApiCompatibilityUtils.getPaddingEnd(view),
0);
}
((TextView) view.findViewById(android.R.id.summary)).setMovementMethod(
LinkMovementMethod.getInstance());
// The icon is aligned to the top of the text view, which can be higher than the
// ascender line of the text, and makes it look aligned improperly.
TextView textView = (TextView) view.findViewById(
getTitle() != null ? android.R.id.title : android.R.id.summary);
FontMetrics metrics = textView.getPaint().getFontMetrics();
ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
ApiCompatibilityUtils.setPaddingRelative(
icon, 0, (int) java.lang.Math.ceil(metrics.ascent - metrics.top), 0, 0);
return view;
}
/**
* @param context The current Android's context.
* @param incognito Whether the title are for incognito mode.
* @param nullFaviconResourceId A drawable resource id of a default favicon.
*/
public TitleBitmapFactory(Context context, boolean incognito, int nullFaviconResourceId) {
mNullFaviconResourceId = nullFaviconResourceId;
Resources res = context.getResources();
int textColor = ApiCompatibilityUtils.getColor(res, incognito
? R.color.compositor_tab_title_bar_text_incognito
: R.color.compositor_tab_title_bar_text);
float textSize = res.getDimension(R.dimen.compositor_tab_title_text_size);
boolean fakeBoldText = res.getBoolean(R.bool.compositor_tab_title_fake_bold_text);
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(textColor);
mTextPaint.setTextSize(textSize);
mTextPaint.setFakeBoldText(fakeBoldText);
mTextPaint.density = res.getDisplayMetrics().density;
FontMetrics textFontMetrics = mTextPaint.getFontMetrics();
mTextHeight = (float) Math.ceil(textFontMetrics.bottom - textFontMetrics.top);
mTextYOffset = -textFontMetrics.top;
mFaviconDimension = res.getDimensionPixelSize(R.dimen.compositor_tab_title_favicon_size);
mViewHeight = (int) Math.max(mFaviconDimension, mTextHeight);
int width = res.getDisplayMetrics().widthPixels;
int height = res.getDisplayMetrics().heightPixels;
mMaxWidth = (int) (TITLE_WIDTH_PERCENTAGE * Math.max(width, height));
// Set the favicon dimension here.
mFaviconDimension = Math.min(mMaxWidth, mFaviconDimension);
}
@Override
public View onCreateView(ViewGroup parent) {
View view = super.onCreateView(parent);
if (mNoBottomSpacing) {
ApiCompatibilityUtils.setPaddingRelative(
view,
ApiCompatibilityUtils.getPaddingStart(view),
view.getPaddingTop(),
ApiCompatibilityUtils.getPaddingEnd(view),
0);
}
((TextView) view.findViewById(android.R.id.summary)).setMovementMethod(
LinkMovementMethod.getInstance());
// The icon is aligned to the top of the text view, which can be higher than the
// ascender line of the text, and makes it look aligned improperly.
TextView textView = (TextView) view.findViewById(
getTitle() != null ? android.R.id.title : android.R.id.summary);
FontMetrics metrics = textView.getPaint().getFontMetrics();
ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
ApiCompatibilityUtils.setPaddingRelative(
icon, 0, (int) java.lang.Math.ceil(metrics.ascent - metrics.top), 0, 0);
return view;
}
/**
* 得到字符串信息包括行数,页数等信息
*/
public void GetTextIfon() {
char ch;
int w = 0;
int istart = 0;
FontMetrics fm = mPaint.getFontMetrics();// 得到系统默认字体属性
mFontHeight = (int) (Math.ceil(fm.descent - fm.top) + 2);// 获得字体高度
mPageLineNum = mTextHeight / mFontHeight;// 获得行数
int count = this.mStrText.length();
for (int i = 0; i < count; i++) {
ch = this.mStrText.charAt(i);
float[] widths = new float[1];
String str = String.valueOf(ch);
mPaint.getTextWidths(str, widths);
if (ch == '\n') {//原为\n
mRealLine++;// 真实的行数加一
mString.addElement(this.mStrText.substring(istart, i));
istart = i + 1;
w = 0;
} else {
w += (int) Math.ceil(widths[0]);
if (w > this.mTextWidth) {
mRealLine++;// 真实的行数加一
mString.addElement(this.mStrText.substring(istart, i));
istart = i;
i--;
w = 0;
} else {
if (i == count - 1) {
mRealLine++;// 真实的行数加一
mString.addElement(this.mStrText.substring(istart,
count));
}
}
}
}
}
private void initView()
{
mPaint = new TextPaint();
mPaint.setTextSize(mTextSize);
mPaint.setTextAlign(Align.CENTER);
mPaint.setAntiAlias(true);
// 计算字体高度
FontMetrics fm = mPaint.getFontMetrics();
mTextHeight = (int) (fm.bottom - fm.top);
// 滚动动画
mLineAnimator = new ValueAnimator();
mLineAnimator.setIntValues(0, 100);
mLineAnimator.setDuration(mLineAnimDuration);
mLineAnimator.addUpdateListener(new AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator animation)
{
int value = (Integer) animation.getAnimatedValue();
float percent = 1 - (float) value / 100;
mLineOffset = (int) (mAnimOffset * percent); // 更新偏移值,重绘View
invalidate();
}
});
}
/**
* get Font height
*
* @param view
* @return
*/
public static int getFontHeight(TextView view) {
Paint paint = new Paint();
paint.setTextSize(view.getTextSize());
FontMetrics fm = paint.getFontMetrics();
return (int) (Math.ceil(fm.descent - fm.ascent));
}
private void updateIconSize(float scale, int drawablePadding, Resources res,
DisplayMetrics dm) {
iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
iconDrawablePaddingPx = drawablePadding;
hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
// Search Bar
searchBarSpaceWidthPx = Math.min(widthPx,
res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width));
defaultSearchBarSpaceHeightPx = getSearchBarTopOffset()
+ res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
searchBarSpaceHeightPx = defaultSearchBarSpaceHeightPx;
// Calculate the actual text height
Paint textPaint = new Paint();
textPaint.setTextSize(iconTextSizePx);
FontMetrics fm = textPaint.getFontMetrics();
cellWidthPx = iconSizePx;
cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
// Hotseat
hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
hotseatCellWidthPx = iconSizePx;
hotseatCellHeightPx = iconSizePx;
// Folder
folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
folderCellHeightPx = cellHeightPx + edgeMarginPx;
folderBackgroundOffset = -edgeMarginPx;
folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
}
/**
* 返回字体高度
*
* @param textSize
* @return
*/
public static int getFontHeight(float textSize)
{
Paint paint = new Paint();
paint.setTextSize(textSize);
FontMetrics fm = paint.getFontMetrics();
return (int) Math.ceil(fm.descent - fm.top) + 2;
}
/**
* 返回字体高度
*
* @param textSize
* @return
*/
public int getFontHeight(float textSize)
{
Paint paint = new Paint();
paint.setTextSize(textSize);
FontMetrics fm = paint.getFontMetrics();
return (int) Math.ceil(fm.descent - fm.top) + 2;
}
/**
* 返回字体高度
*
* @param textSize
* @return
*/
public static int getFontHeight(float textSize)
{
Paint paint = new Paint();
paint.setTextSize(textSize);
FontMetrics fm = paint.getFontMetrics();
return (int) Math.ceil(fm.descent - fm.top) + 2;
}
private void measureText() {
mTextWidth = (int) mPaint.measureText(mText);
FontMetrics fm = mPaint.getFontMetrics();
mTextHeight = (int) Math.ceil(fm.descent - fm.top);
mPaint.getTextBounds(mText, 0, mText.length(), mTextBound);
mTextHeight = mTextBound.height();
}
/**
* @param context The current Android's context.
* @param incognito Whether the title are for incognito mode.
* @param nullFaviconResourceId A drawable resource id of a default favicon.
*/
public TitleBitmapFactory(Context context, boolean incognito, int nullFaviconResourceId) {
mNullFaviconResourceId = nullFaviconResourceId;
Resources res = context.getResources();
int textColor = ApiCompatibilityUtils.getColor(res, incognito
? R.color.compositor_tab_title_bar_text_incognito
: R.color.compositor_tab_title_bar_text);
float textSize = res.getDimension(R.dimen.compositor_tab_title_text_size);
boolean fakeBoldText = res.getBoolean(R.bool.compositor_tab_title_fake_bold_text);
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(textColor);
mTextPaint.setTextSize(textSize);
mTextPaint.setFakeBoldText(fakeBoldText);
mTextPaint.density = res.getDisplayMetrics().density;
FontMetrics textFontMetrics = mTextPaint.getFontMetrics();
mTextHeight = (float) Math.ceil(textFontMetrics.bottom - textFontMetrics.top);
mTextYOffset = -textFontMetrics.top;
mFaviconDimension = res.getDimensionPixelSize(R.dimen.compositor_tab_title_favicon_size);
mViewHeight = (int) Math.max(mFaviconDimension, mTextHeight);
int width = res.getDisplayMetrics().widthPixels;
int height = res.getDisplayMetrics().heightPixels;
mMaxWidth = (int) (TITLE_WIDTH_PERCENTAGE * Math.max(width, height));
// Set the favicon dimension here.
mFaviconDimension = Math.min(mMaxWidth, mFaviconDimension);
}
@Override
public View onCreateView(ViewGroup parent) {
View view = super.onCreateView(parent);
if (mNoBottomSpacing) {
ApiCompatibilityUtils.setPaddingRelative(
view,
ApiCompatibilityUtils.getPaddingStart(view),
view.getPaddingTop(),
ApiCompatibilityUtils.getPaddingEnd(view),
0);
}
((TextView) view.findViewById(android.R.id.summary)).setMovementMethod(
LinkMovementMethod.getInstance());
// The icon is aligned to the top of the text view, which can be higher than the
// ascender line of the text, and makes it look aligned improperly.
TextView textView = (TextView) view.findViewById(
getTitle() != null ? android.R.id.title : android.R.id.summary);
FontMetrics metrics = textView.getPaint().getFontMetrics();
ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
ApiCompatibilityUtils.setPaddingRelative(
icon, 0, (int) java.lang.Math.ceil(metrics.ascent - metrics.top), 0, 0);
return view;
}
private RectF generateTextRectF() {
FontMetrics fontMetrics = mTextPaint.getFontMetrics();
final float height = fontMetrics.bottom - fontMetrics.top;
String text = generateText();
final float width = Layout.getDesiredWidth(text, mTextPaint) + 2 * mTextLRPad;
return new RectF(0, 0, width, height);
}
/**
* @param context The current Android's context.
* @param incognito Whether the title are for incognito mode.
* @param nullFaviconResourceId A drawable resource id of a default favicon.
*/
public TitleBitmapFactory(Context context, boolean incognito, int nullFaviconResourceId) {
mNullFaviconResourceId = nullFaviconResourceId;
Resources res = context.getResources();
int textColor = ApiCompatibilityUtils.getColor(res, incognito
? R.color.compositor_tab_title_bar_text_incognito
: R.color.compositor_tab_title_bar_text);
int shadowColor = ApiCompatibilityUtils.getColor(res, incognito
? R.color.compositor_tab_title_bar_shadow_incognito
: R.color.compositor_tab_title_bar_shadow);
int shadowXOffset = res.getDimensionPixelOffset(incognito
? R.dimen.compositor_tab_title_bar_shadow_x_offset_incognito
: R.dimen.compositor_tab_title_bar_shadow_x_offset);
int shadowYOffset = res.getDimensionPixelOffset(incognito
? R.dimen.compositor_tab_title_bar_shadow_y_offset_incognito
: R.dimen.compositor_tab_title_bar_shadow_y_offset);
float textSize = res.getDimension(R.dimen.compositor_tab_title_text_size);
boolean fakeBoldText = res.getBoolean(R.bool.compositor_tab_title_fake_bold_text);
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(textColor);
if (shadowXOffset != 0 && shadowYOffset != 0) {
mTextPaint.setShadowLayer(0.001f, shadowXOffset, shadowYOffset, shadowColor);
}
mTextPaint.setTextSize(textSize);
mTextPaint.setFakeBoldText(fakeBoldText);
mTextPaint.density = res.getDisplayMetrics().density;
FontMetrics textFontMetrics = mTextPaint.getFontMetrics();
mTextHeight = (float) Math.ceil(textFontMetrics.bottom - textFontMetrics.top);
mTextYOffset = -textFontMetrics.top;
mFaviconDimension = res.getDimensionPixelSize(R.dimen.compositor_tab_title_favicon_size);
mViewHeight = (int) Math.max(mFaviconDimension, mTextHeight);
int width = res.getDisplayMetrics().widthPixels;
int height = res.getDisplayMetrics().heightPixels;
mMaxWidth = (int) (TITLE_WIDTH_PERCENTAGE * Math.max(width, height));
// Set the favicon dimension here.
mFaviconDimension = Math.min(mMaxWidth, mFaviconDimension);
}
/**
* @return 返回指定笔的文字高度
*/
public static float getFontHeight(Paint paint) {
FontMetrics fm = paint.getFontMetrics();
return fm.descent - fm.ascent;
}
/**
* @return 返回指定笔离文字顶部的基准距离
*/
public static float getFontLeading(Paint paint) {
FontMetrics fm = paint.getFontMetrics();
return fm.leading - fm.ascent;
}
private void updateIconSize(float scale, int drawablePadding, Resources resources,
DisplayMetrics dm) {
iconSizePx = (int) (DynamicGrid.pxFromDp(iconSize, dm) * scale);
iconTextSizePx = (int) (DynamicGrid.pxFromSp(iconTextSize, dm) * scale);
iconDrawablePaddingPx = drawablePadding;
hotseatIconSizePx = (int) (DynamicGrid.pxFromDp(hotseatIconSize, dm) * scale);
// Search Bar
searchBarSpaceWidthPx = Math.min(searchBarSpaceMaxWidthPx, widthPx);
searchBarSpaceMaxWidthPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width);
searchBarHeightPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
searchBarSpaceHeightPx = searchBarHeightPx + getSearchBarTopOffset();
// Calculate the actual text height
Paint textPaint = new Paint();
textPaint.setTextSize(iconTextSizePx);
FontMetrics fm = textPaint.getFontMetrics();
cellWidthPx = iconSizePx;
cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
final float scaleDps = resources.getDimensionPixelSize(R.dimen.dragViewScale);
dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
// Hotseat
hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
hotseatCellWidthPx = iconSizePx;
hotseatCellHeightPx = iconSizePx;
// Folder
folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
folderCellHeightPx = cellHeightPx + edgeMarginPx;
folderBackgroundOffset = -edgeMarginPx;
folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
// All Apps
Rect padding = getWorkspacePadding(isLandscape ?
CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
int pageIndicatorOffset =
resources.getDimensionPixelSize(R.dimen.apps_customize_page_indicator_offset);
if (isPhone()) {
searchBarSpaceWidthPx = Math.min(searchBarSpaceMaxWidthPx, widthPx);
} else {
searchBarSpaceWidthPx = widthPx - (isLandscape ? 3 : 1) * iconSizePx;
}
allAppsCellWidthPx = allAppsIconSizePx;
allAppsCellHeightPx = allAppsIconSizePx + drawablePadding + iconTextSizePx;
int maxLongEdgeCellCount =
resources.getInteger(R.integer.config_dynamic_grid_max_long_edge_cell_count);
int maxShortEdgeCellCount =
resources.getInteger(R.integer.config_dynamic_grid_max_short_edge_cell_count);
int minEdgeCellCount =
resources.getInteger(R.integer.config_dynamic_grid_min_edge_cell_count);
int maxRows = (isLandscape ? maxShortEdgeCellCount : maxLongEdgeCellCount);
int maxCols = (isLandscape ? maxLongEdgeCellCount : maxShortEdgeCellCount);
allAppsNumRows = (availableHeightPx - pageIndicatorHeightPx) /
(allAppsCellHeightPx + allAppsCellPaddingPx);
allAppsNumRows = Math.max(minEdgeCellCount, Math.min(maxRows, allAppsNumRows));
allAppsNumCols = (availableWidthPx) /
(allAppsCellWidthPx + allAppsCellPaddingPx);
allAppsNumCols = Math.max(minEdgeCellCount, Math.min(maxCols, allAppsNumCols));
}