android.graphics.Paint.FontMetrics Java Examples

The following examples show how to use android.graphics.Paint.FontMetrics. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: X8MainPitchingAngle.java    From FimiX8-RE with MIT License 6 votes vote down vote up
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);
    }
}
 
Example #2
Source File: NestHeader.java    From NestRefreshLayout with MIT License 6 votes vote down vote up
/**
     * @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);
    }
 
Example #3
Source File: RoundedIconGenerator.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #4
Source File: MediaDetailDownloadStateView.java    From FimiX8-RE with MIT License 6 votes vote down vote up
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);
    }
}
 
Example #5
Source File: RoundedIconGenerator.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #6
Source File: RLAutoSplitTextView.java    From Roid-Library with Apache License 2.0 6 votes vote down vote up
@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;
    }
}
 
Example #7
Source File: RoundedIconGenerator.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #8
Source File: TopicTipView.java    From umeng_community_android with MIT License 6 votes vote down vote up
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设置无效。暂时先在代码中设置。
}
 
Example #9
Source File: ViewUtils.java    From opencdk-appwidget with Apache License 2.0 5 votes vote down vote up
/**
 * 返回字体高度
 * 
 * @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;
}
 
Example #10
Source File: NumberProgressBar.java    From Android_Skin_2.0 with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #11
Source File: TextMessageWithLinkAndIconPreference.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #12
Source File: TitleBitmapFactory.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * @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);
}
 
Example #13
Source File: ColorTrackView.java    From ColorTrackView with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #14
Source File: ViewUtils.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * 返回字体高度
 * 
 * @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;
}
 
Example #15
Source File: SingleLineEditText.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * 返回字体高度
 * 
 * @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;
}
 
Example #16
Source File: DeviceProfile.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example #17
Source File: ViewUtil.java    From AndroidStudyDemo with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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));
}
 
Example #18
Source File: LyricView.java    From RhymeMusic with Apache License 2.0 5 votes vote down vote up
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();
        }
    });
}
 
Example #19
Source File: DrawTextUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
/**
 * 得到字符串信息包括行数,页数等信息
 */
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));
				}
			}
		}
	}
}
 
Example #20
Source File: TextMessageWithLinkAndIconPreference.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #21
Source File: X8MainReturnTimeTextView.java    From FimiX8-RE with MIT License 5 votes vote down vote up
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);
}
 
Example #22
Source File: ScrollTextView.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #23
Source File: TitleBitmapFactory.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * @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);
}
 
Example #24
Source File: TextMessageWithLinkAndIconPreference.java    From delion with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #25
Source File: ScrollTextView.java    From AndroidTvDemo with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #26
Source File: SimpleTextCacheStuffer.java    From letv with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #27
Source File: BezierCurveChart.java    From android-bezier-curve-chart with Apache License 2.0 4 votes vote down vote up
public float getTextHeight(Paint textPaint) {
	FontMetrics fm = textPaint.getFontMetrics();
	return (float) Math.ceil(fm.descent - fm.ascent) - 2;
}
 
Example #28
Source File: SinglePointSVGRenderer.java    From mil-sym-android with Apache License 2.0 4 votes vote down vote up
public Bitmap getTestSymbol()
{
	Bitmap temp = null;
	try
	{
		temp = Bitmap.createBitmap(70, 70, Config.ARGB_8888);

		Canvas canvas = new Canvas(temp);

		if (canvas.isHardwareAccelerated())
		{
			System.out.println("HW acceleration supported");
		}
		// canvas.drawColor(Color.WHITE);

		// Typeface tf = Typeface.createFromAsset(_am,
		// "fonts/unitfonts.ttf");
		Typeface tf = _tfUnits;

		Paint fillPaint = new Paint();
		fillPaint.setStyle(Paint.Style.FILL);
		fillPaint.setColor(Color.CYAN.toInt());
		fillPaint.setTextSize(50);
		fillPaint.setAntiAlias(true);
		fillPaint.setTextAlign(Align.CENTER);
		fillPaint.setTypeface(tf);

		Paint framePaint = new Paint();
		framePaint.setStyle(Paint.Style.FILL);
		framePaint.setColor(Color.BLACK.toInt());
		framePaint.setTextSize(50);
		framePaint.setAntiAlias(true);
		framePaint.setTextAlign(Align.CENTER);
		framePaint.setTypeface(tf);

		Paint symbolPaint = new Paint();
		symbolPaint.setStyle(Paint.Style.FILL);
		symbolPaint.setColor(Color.BLACK.toInt());
		symbolPaint.setTextSize(50);
		symbolPaint.setAntiAlias(true);
		symbolPaint.setTextAlign(Align.CENTER);
		symbolPaint.setTypeface(tf);

		String strFill = String.valueOf((char) 800);
		String strFrame = String.valueOf((char) 801);
		String strSymbol = String.valueOf((char) 1121);

		canvas.drawText(strFill, 35, 35, fillPaint);
		canvas.drawText(strFrame, 35, 35, framePaint);
		canvas.drawText(strSymbol, 35, 35, symbolPaint);

		FontMetrics mf = framePaint.getFontMetrics();
		float height = mf.bottom - mf.top;
		float width = fillPaint.measureText(strFrame);

		Log.i(TAG, "top: " + String.valueOf(mf.top));
		Log.i(TAG, "bottom: " + String.valueOf(mf.bottom));
		Log.i(TAG, "ascent: " + String.valueOf(mf.ascent));
		Log.i(TAG, "descent: " + String.valueOf(mf.descent));
		Log.i(TAG, "leading: " + String.valueOf(mf.leading));
		Log.i(TAG, "width: " + String.valueOf(width));
		Log.i(TAG, "height: " + String.valueOf(height));

	}
	catch (Exception exc)
	{
		Log.e(TAG, exc.getMessage());
		Log.e(TAG, getStackTrace(exc));
	}

	return temp;
}
 
Example #29
Source File: SinglePointRenderer.java    From mil-sym-android with Apache License 2.0 4 votes vote down vote up
public Bitmap getTestSymbol()
 {
     Bitmap temp = null;
     try
     {
         temp = Bitmap.createBitmap(70, 70, Config.ARGB_8888);

         Canvas canvas = new Canvas(temp);

         if (canvas.isHardwareAccelerated())
         {
             System.out.println("HW acceleration supported");
         }
//canvas.drawColor(Color.WHITE);

         //Typeface tf = Typeface.createFromAsset(_am, "fonts/unitfonts.ttf");
         Typeface tf = _tfUnits;

         Paint fillPaint = new Paint();
         fillPaint.setStyle(Paint.Style.FILL);
         fillPaint.setColor(Color.CYAN.toInt());
         fillPaint.setTextSize(50);
         fillPaint.setAntiAlias(true);
         fillPaint.setTextAlign(Align.CENTER);
         fillPaint.setTypeface(tf);

         Paint framePaint = new Paint();
         framePaint.setStyle(Paint.Style.FILL);
         framePaint.setColor(Color.BLACK.toInt());
         framePaint.setTextSize(50);
         framePaint.setAntiAlias(true);
         framePaint.setTextAlign(Align.CENTER);
         framePaint.setTypeface(tf);

         Paint symbolPaint = new Paint();
         symbolPaint.setStyle(Paint.Style.FILL);
         symbolPaint.setColor(Color.BLACK.toInt());
         symbolPaint.setTextSize(50);
         symbolPaint.setAntiAlias(true);
         symbolPaint.setTextAlign(Align.CENTER);
         symbolPaint.setTypeface(tf);

         String strFill = String.valueOf((char) 800);
         String strFrame = String.valueOf((char) 801);
         String strSymbol = String.valueOf((char) 1121);

         canvas.drawText(strFill, 35, 35, fillPaint);
         canvas.drawText(strFrame, 35, 35, framePaint);
         canvas.drawText(strSymbol, 35, 35, symbolPaint);

         FontMetrics mf = framePaint.getFontMetrics();
         float height = mf.bottom - mf.top;
         float width = fillPaint.measureText(strFrame);

         Log.i(TAG, "top: " + String.valueOf(mf.top));
         Log.i(TAG, "bottom: " + String.valueOf(mf.bottom));
         Log.i(TAG, "ascent: " + String.valueOf(mf.ascent));
         Log.i(TAG, "descent: " + String.valueOf(mf.descent));
         Log.i(TAG, "leading: " + String.valueOf(mf.leading));
         Log.i(TAG, "width: " + String.valueOf(width));
         Log.i(TAG, "height: " + String.valueOf(height));

     }
     catch (Exception exc)
     {
         Log.e(TAG, exc.getMessage());
         Log.e(TAG, getStackTrace(exc));
     }

     return temp;
 }
 
Example #30
Source File: SinglePointRenderer.java    From mil-sym-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onSettingsChanged(SettingsChangedEvent sce)
{

    if(sce != null && sce.getEventType().equals(SettingsChangedEvent.EventType_FontChanged))
    {
        synchronized (_modifierFont)
        {
            _modifierFont = RendererSettings.getInstance().getModiferFont();
            _modifierOutlineFont = RendererSettings.getInstance().getModiferFont();
            FontMetrics fm = new FontMetrics();
            fm = _modifierFont.getFontMetrics();
            _modifierDescent = fm.descent;
            //_modifierFontHeight = fm.top + fm.bottom;
            _modifierFontHeight = fm.bottom - fm.top;

            _modifierFont.setStrokeWidth(RendererSettings.getInstance().getTextOutlineWidth());
            _modifierOutlineFont.setColor(Color.white.toInt());
            _deviceDPI = RendererSettings.getInstance().getDeviceDPI();

            ModifierRenderer.setModifierFont(_modifierFont, _modifierFontHeight, _modifierDescent);

        }
    }
    
    if(sce != null && sce.getEventType().equals(SettingsChangedEvent.EventType_CacheSizeChanged))
    {
        int cSize = RendererSettings.getInstance().getCacheSize()/2;
        //adjust unit cache
        if(cSize != cacheSize)
        {
            synchronized(_unitCache)
            {
                _unitCache.evictAll();
                _unitCache = new LruCache<String, ImageInfo>(cSize)
                {	
                    @Override
                    protected int sizeOf(String key, ImageInfo ii)
                    {
                        return ii.getImage().getByteCount();// / 1024;
                    }
                };
            }
            //adjust tg cache
            synchronized(_tgCache)
            {
                _tgCache.evictAll();
                _tgCache = new LruCache<String, ImageInfo>(cSize)
                {	
                    @Override
                    protected int sizeOf(String key, ImageInfo ii)
                    {
                        return ii.getImage().getByteCount();// / 1024;
                    }
                };
            }
            cacheSize = cSize;
            if(cacheSize >= 5)
                maxCachedEntrySize = cacheSize / 5;
            else
                maxCachedEntrySize = 1;
        }
    }
}