Java 类android.graphics.Paint.FontMetrics 实例源码

项目:GitHub    文件:TerminalBridge.java   
private int fontSizeCompare(float sizeDp, int cols, int rows, int width, int height) {
    // read new metrics to get exact pixel dimensions
    defaultPaint.setTextSize((int) (sizeDp * this.displayDensity + 0.5f));
    FontMetrics fm = defaultPaint.getFontMetrics();

    float[] widths = new float[1];
    defaultPaint.getTextWidths("X", widths);
    int termWidth = (int) widths[0] * cols;
    int termHeight = (int) Math.ceil(fm.descent - fm.top) * rows;

    Log.d("fontsize", String.format("font size %fdp resulted in %d x %d", sizeDp, termWidth, termHeight));

    // Check to see if it fits in resolution specified.
    if (termWidth > width || termHeight > height)
        return 1;

    if (termWidth == width || termHeight == height)
        return 0;

    return -1;
}
项目:chromium-for-android-56-debug-video    文件:RoundedIconGenerator.java   
/**
 * 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;
}
项目:LiteSDK    文件:TopicTipView.java   
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(Color.RED);
    mPaint.setStyle(Style.FILL);

    getPaint().setColor(Color.WHITE);// 不知道什么原因,xml设置无效。暂时先在代码中设置。
}
项目:AndroidChromium    文件:RoundedIconGenerator.java   
/**
 * 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;
}
项目:iOffice    文件:TitleBar.java   
public TitleBar(Context context)
{
    super(context);

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(),  R.drawable.sys_title_bg_vertical, opts); 
    height = opts.outHeight;        

    setBackgroundResource(R.drawable.sys_title_bg_vertical);

    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.WHITE);
    paint.setTextSize(24);
    FontMetrics fm = paint.getFontMetrics();

    yPostion = (height - fm.descent + fm.ascent) / 2  -  fm.ascent;

    mBusyIndicator = new ProgressBar(getContext());
    mBusyIndicator.setIndeterminate(true);
    //mBusyIndicator.setBackgroundResource(R.drawable.busy);
    addView(mBusyIndicator);
    mBusyIndicator.setVisibility(GONE);
}
项目:connectbot    文件:TerminalBridge.java   
private int fontSizeCompare(float size, int cols, int rows, int width, int height) {
    // read new metrics to get exact pixel dimensions
    defaultPaint.setTextSize(size);
    FontMetrics fm = defaultPaint.getFontMetrics();

    float[] widths = new float[1];
    defaultPaint.getTextWidths("X", widths);
    int termWidth = (int)widths[0] * cols;
    int termHeight = (int)Math.ceil(fm.descent - fm.top) * rows;

    Log.d("fontsize", String.format("font size %f resulted in %d x %d", size, termWidth, termHeight));

    // Check to see if it fits in resolution specified.
    if (termWidth > width || termHeight > height)
        return 1;

    if (termWidth == width || termHeight == height)
        return 0;

    return -1;
}
项目:EntboostIM    文件:AbGraphical.java   
/**
 * 描述:绘制文本,支持换行.
 *
 * @param canvas the canvas
 * @param text the text
 * @param maxWPix the max w pix
 * @param paint the paint
 * @param left the left
 * @param top the top
 * @return the int
 */
public static int drawText(Canvas canvas,String text,int maxWPix,TextPaint paint,int left,int top) {
if(AbStrUtil.isEmpty(text)){
    return 1;
}
//需要根据文字长度控制换行
   //测量文字的长度
List<String> mStrList  = getDrawRowStr(text,maxWPix,paint);

    FontMetrics fm  = paint.getFontMetrics();
    int hSize = (int)Math.ceil(fm.descent - fm.ascent)+2;

    for(int i=0;i<mStrList.size();i++){
     //计算坐标
     int x = left;
        int y = top+hSize/2+hSize*i;
     String textLine = mStrList.get(i);
        canvas.drawText(textLine,x,y, paint); 

    }
    return mStrList.size();
}
项目:Vafrinn    文件:RoundedIconGenerator.java   
/**
 * 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;
}
项目:umeng_community_android    文件:TopicTipView.java   
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设置无效。暂时先在代码中设置。
}
项目:eshow-android    文件:AbTextView.java   
/**
 * Draw text.
 *
 * @param canvas the canvas
 * @param text the text
 * @param maxWPix the max w pix
 * @param paint the paint
 * @return the int
 */
public int drawText(Canvas canvas, String text, int maxWPix,
        TextPaint paint) {
    if (TextUtils.isEmpty(text)) {
        return 1;
    }
    // 需要根据文字长度控制换行
    // 测量文字的长度
    List<String> mStrList = getDrawRowStr(text, maxWPix, paint);

    FontMetrics fm = paint.getFontMetrics();
    int hSize = (int)Math.ceil(fm.descent - fm.ascent);

    for (int i = 0; i < mStrList.size(); i++) {
        // 计算坐标
        float x = leftPadding;
        float y = topPadding+hSize/2+i*(hSize+lineSpacing)+bottomPadding;
        String textLine = mStrList.get(i);
        if(i < maxLines){
            canvas.drawText(textLine, x, y, paint);
        }
    }
    return mStrList.size();
}
项目:AndroidGeek    文件:TopicTipView.java   
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设置无效。暂时先在代码中设置。
}
项目:Android-SmartChart    文件:PieSeries.java   
protected void drawLabel(Canvas canvas, String labelText, List<RectF> prevLabelsBounds, int centerX, int centerY,
            float shortRadius, float longRadius, float currentAngle, float angle, int left, int right, int color,
            Paint paint, boolean line, boolean showMoreLabel)
{

    LabelPoint labelPoint =
                drawLabelHandler(canvas, labelText, prevLabelsBounds, centerX, centerY, shortRadius, longRadius,
                            currentAngle, angle, left, right, color, paint, line);
    String[] texts = labelText.split("/");
    for (int index = 0; index < texts.length; index++)
    {
        canvas.drawText(texts[index], labelPoint.xLabel, labelPoint.yLabel, paint);
        FontMetrics fm = paint.getFontMetrics();
        labelPoint.yLabel = labelPoint.yLabel + Math.round(fm.descent - fm.top) + 2;
    }
    if (line)
    {
        prevLabelsBounds.add(new RectF(labelPoint.xLabel, labelPoint.yLabel, labelPoint.xLabel
                    + labelPoint.widthLabel, labelPoint.yLabel + labelPoint.size));
    }
}
项目:365browser    文件:RoundedIconGenerator.java   
/**
 * 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;
}
项目:NestRefreshLayout    文件:NestHeader.java   
/**
     * @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);
    }
项目:Unity    文件:WheelView.java   
/**
 * 画左右的Label
 *
 * @param canvas
 */
private void drawLabel(Canvas canvas) {
    canvas.save();
    int vh = getHeight();

    FontMetrics fontMetrics = paint.getFontMetrics();
    // 计算文字高度
    float fontHeight = fontMetrics.bottom - fontMetrics.top;
    // 计算文字baseline
    float y = PADDING + vh - (vh - fontHeight) / 2 - fontMetrics.bottom;

    if (!TextUtils.isEmpty(mRightLabel)) {
        canvas.drawText(mRightLabel, getWidth() - rightLabelLen / 2
                - PADDING, y, labelPaint);
    }
    if (!TextUtils.isEmpty(mLeftLabel)) {
        canvas.drawText(mLeftLabel, PADDING + leftLabelLen / 2, y,
                labelPaint);
    }

    canvas.restore();
}
项目:lookingforseats-nwpu    文件:TasksCompletedView.java   
private void initVariable() {
    mCirclePaint = new Paint();
    mCirclePaint.setAntiAlias(true);
    mCirclePaint.setColor(mCircleColor);
    mCirclePaint.setStyle(Paint.Style.FILL);

    mRingPaint = new Paint();
    mRingPaint.setAntiAlias(true);
    mRingPaint.setColor(mRingColor);
    mRingPaint.setStyle(Paint.Style.STROKE);
    mRingPaint.setStrokeWidth(mStrokeWidth);

    mTextPaint = new Paint();
    mTextPaint.setAntiAlias(true);
    mTextPaint.setStyle(Paint.Style.FILL);
    mTextPaint.setARGB(255, 255, 255, 255);
    mTextPaint.setTextSize(mRadius / 2);

    FontMetrics fm = mTextPaint.getFontMetrics();
    mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent);

}
项目:androidsummary    文件:AbTextView.java   
/**
 * Draw text.
 *
 * @param canvas the canvas
 * @param text the text
 * @param maxWPix the max w pix
 * @param paint the paint
 * @return the int
 */
public int drawText(Canvas canvas, String text, int maxWPix,
        TextPaint paint) {
    if (TextUtils.isEmpty(text)) {
        return 1;
    }
    // 需要根据文字长度控制换行
    // 测量文字的长度
    List<String> mStrList = getDrawRowStr(text, maxWPix, paint);

    FontMetrics fm = paint.getFontMetrics();
    int hSize = (int)Math.ceil(fm.descent - fm.ascent);

    for (int i = 0; i < mStrList.size(); i++) {
        // 计算坐标
        float x = leftPadding;
        float y = topPadding+hSize/2+i*(hSize+lineSpacing)+bottomPadding;
        String textLine = mStrList.get(i);
        if(i < maxLines){
            canvas.drawText(textLine, x, y, paint);
        }
    }
    return mStrList.size();
}
项目:GomeOnline    文件:InvestGroupChart.java   
protected void onDraw(Canvas canvas){
        Paint paint = new Paint();
        paint.setStyle(Style.STROKE);
        paint.setAntiAlias(true);
        paint.setColor(0xffe9e9e9);
        paint.setTextSize(Function.getFitPx(getContext(), 30));
        FontMetrics fm = paint.getFontMetrics();
//      canvas.drawRect(drawRect, paint);
        canvas.drawLine(drawRect.left, drawRect.top, drawRect.right, drawRect.top, paint);
        canvas.drawLine(drawRect.left, drawRect.bottom, drawRect.right, drawRect.bottom, paint);
        float h = (maxValue - minValue) / 2;
        float y = Function.getYCoordinate(drawRect, minValue+h, maxValue, minValue);
        paint.setColor(0xffbbbbbc);
        canvas.drawLine(drawRect.left, y, drawRect.right, y, paint);
        paint.setTextAlign(Align.RIGHT);
        int baseline = (int) (drawRect.top + (drawRect.bottom - drawRect.top - fm.bottom + fm.top) / 2 - fm.top); 
        canvas.drawText("0%", drawRect.left-Function.getFitPx(getContext(), 20), baseline, paint);
        paint.setColor(getContext().getResources().getColor(R.color.font_de3031));          
        canvas.drawText("3.4%", drawRect.left-Function.getFitPx(getContext(), 20), drawRect.top-(fm.top-fm.bottom)/2 + 2, paint);
        paint.setColor(getContext().getResources().getColor(R.color.font_32a632));          
        canvas.drawText("-3.4%", drawRect.left-Function.getFitPx(getContext(), 20), drawRect.bottom, paint);
    }
项目:Roid-Library    文件:RLAutoSplitTextView.java   
@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;
    }
}
项目:GitHub    文件:TerminalBridge.java   
/**
 * Request a different font size. Will make call to parentChanged() to make
 * sure we resize PTY if needed.
 *
 * @param sizeDp Size of font in dp
 */
private final void setFontSize(float sizeDp) {
    if (sizeDp <= 0.0) {
        return;
    }

    final int fontSizePx = (int) (sizeDp * this.displayDensity + 0.5f);

    defaultPaint.setTextSize(fontSizePx);
    fontSizeDp = sizeDp;

    // read new metrics to get exact pixel dimensions
    FontMetrics fm = defaultPaint.getFontMetrics();
    charTop = (int) Math.ceil(fm.top);

    float[] widths = new float[1];
    defaultPaint.getTextWidths("X", widths);
    charWidth = (int) Math.ceil(widths[0]);
    charHeight = (int) Math.ceil(fm.descent - fm.top);

    // refresh any bitmap with new font size
    if (parent != null) {
        parentChanged(parent);
    }

    for (FontSizeChangedListener ofscl : fontSizeChangedListeners) {
        ofscl.onFontSizeChanged(sizeDp);
    }

    host.setFontSize((int) sizeDp);
    manager.hostdb.saveHost(host);

    forcedSize = false;
}
项目:Quran    文件:HighlightingImageView.java   
private boolean initOverlayParams(Matrix matrix) {
  if (overlayParams == null || pageBounds == null) {
    return false;
  }

  // Overlay params previously initiated; skip
  if (overlayParams.init) {
    return true;
  }

  int overlayColor = overlayTextColor;
  if (isNightMode) {
    overlayColor = Color.rgb(nightModeTextBrightness,
        nightModeTextBrightness, nightModeTextBrightness);
  }
  overlayParams.paint.setColor(overlayColor);

  // Use font metrics to calculate the maximum possible height of the text
  FontMetrics fm = overlayParams.paint.getFontMetrics();

  final RectF mappedRect = new RectF();
  matrix.mapRect(mappedRect, pageBounds);

  // Calculate where the text's baseline should be
  // (for top text and bottom text)
  // (p.s. parts of the glyphs will be below the baseline such as a
  // 'y' or 'ي')
  overlayParams.topBaseline = -fm.top;
  overlayParams.bottomBaseline = getHeight() - fm.bottom;

  // Calculate the horizontal margins off the edge of screen
  overlayParams.offsetX = Math.min(
      mappedRect.left, getWidth() - mappedRect.right);

  overlayParams.init = true;
  return true;
}
项目:Hotspot-master-devp    文件:SliderView.java   
/**
 * 获取字体的高度
 * @param fontSize
 * @return
 */
private int getFontHeight(float fontSize){  
     Paint paint = new Paint();  
     paint.setTextSize(fontSize);  
     FontMetrics fm = paint.getFontMetrics();  
    return (int) Math.ceil(fm.descent - fm.top) + 2;  
}
项目:chromium-for-android-56-debug-video    文件:TitleBitmapFactory.java   
/**
 * @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);
}
项目:chromium-for-android-56-debug-video    文件:TextMessageWithLinkAndIconPreference.java   
@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;
}
项目:letv    文件:SimpleTextCacheStuffer.java   
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;
}
项目:AndroidTvDemo    文件:ScrollTextView.java   
/**
 * 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);
}
项目:android-project-gallery    文件:ViewUtils.java   
/**
 * 返回字体高度
 * 
 * @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;
}
项目:android-project-gallery    文件:SingleLineEditText.java   
/**
 * 返回字体高度
 * 
 * @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;
}
项目:Lunei    文件:ViewUtils.java   
/**
 * get Font height
 */
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));
}
项目:SimplOS    文件:DeviceProfile.java   
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));
    searchBarSpaceHeightPx = getSearchBarTopOffset()
            + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);

    // 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;
}
项目:AndroidChromium    文件:TitleBitmapFactory.java   
/**
 * @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);
}
项目:AndroidChromium    文件:TextMessageWithLinkAndIconPreference.java   
@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;
}
项目:BigApp_Discuz_Android    文件:DrawTextUtils.java   
/**
 * 得到字符串信息包括行数,页数等信息
 */
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));
                }
            }
        }
    }
}
项目:Diary    文件:MultipleVerticalTextView.java   
private void CalculateText() {
        paint.setTextSize(mFontSize);
        //获得字宽
        if (mLineWidth == 0) {
            paint.getTextWidths("正", widths);//获取单个汉字的宽度
            paint.getTextWidths(" ", space);
//            mLineWidth = (int) Math.ceil((widths[0] + space[0]) * 1.1 + 2);
            mLineWidth = (int) Math.ceil((widths[0] + space[0]) * 1.1 + 2)  //获得字体宽度
                    + DisplayUtil.dp2px(getContext(), 2);                  //增加间距
        }

        FontMetrics fm = paint.getFontMetrics();
//        mFontHeight = (int) (Math.ceil(fm.descent - fm.top) * 0.9);// 获得字体高度
        mFontHeight = (int) (Math.ceil(fm.descent - fm.ascent)) // 获得字体高度
                + DisplayUtil.dp2px(getContext(), 2);          //增加间距
        //计算文字行数
        mRealLine = 0;
        for (int i = 0; i < this.TextLength; i++) {
            ch = this.text.charAt(i);
            if (ch == '\n') {
                mRealLine++;// 真实的行数加一
                h = 0;
            } else {
                h += mFontHeight;
                if (h > this.mTextHeight) {
                    mRealLine++;// 真实的行数加一
                    i--;
                    h = 0;
                } else {
                    if (i == this.TextLength - 1) {
                        mRealLine++;// 真实的行数加一
                    }
                }
            }
        }
        mRealLine += 1;//额外增加一行
        mTextWidth = mLineWidth * mRealLine;//计算文字总宽度
        measure(mTextWidth, getHeight());//重新调整大小
        layout(getLeft(), getTop(), getLeft() + mTextWidth, getBottom());//重新绘制容器
    }
项目:RhymeMusic    文件:LyricView.java   
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();
        }
    });
}
项目:iOffice    文件:ColumnHeader.java   
private void drawFirstVisibleColumn(Canvas canvas, float zoom, Paint paint)
{
    FontMetrics fm = paint.getFontMetrics();  
    float visibleColumnWidth = 0;
    SheetScroller minRowAndColumnInformation = sheetview.getMinRowAndColumnInformation();
    //draw rest part of first column
    float columnWidht = (minRowAndColumnInformation.getColumnWidth() * zoom);
    visibleColumnWidth = (float)(minRowAndColumnInformation.getVisibleColumnWidth() * zoom);
    // 绘制header
    if(HeaderUtil.instance().isActiveColumn(sheetview.getCurrentSheet(), minRowAndColumnInformation.getMinColumnIndex()))
    {
        paint.setColor(SSConstant.ACTIVE_COLOR);
    }
    else
    {
        paint.setColor(SSConstant.HEADER_FILL_COLOR);
    }

    rect.set((int)x, 0, (int)(x + visibleColumnWidth), columnHeaderHeight);
    canvas.drawRect(rect, paint);        

    //header line
    paint.setColor(SSConstant.HEADER_GRIDLINE_COLOR);
    canvas.drawRect(x, 0, x + 1, columnHeaderHeight, paint);

    // 绘制文本
    canvas.save();
    canvas.clipRect(rect);
    paint.setColor(SSConstant.HEADER_TEXT_COLOR);

    String rowText = HeaderUtil.instance().getColumnHeaderTextByIndex(minRowAndColumnInformation.getMinColumnIndex());
    float textWidth = paint.measureText(rowText);
    float textX = (columnWidht - textWidth) / 2;
    float textY = (int)(columnHeaderHeight - Math.ceil(fm.descent - fm.ascent)) / 2;
    canvas.drawText(rowText, x + textX - (columnWidht - visibleColumnWidth), textY - fm.ascent, paint);

    canvas.restore();
}
项目:iOffice    文件:AbstractChart.java   
/**
 * single legend width and height
 * @param renderer
 * @param titles
 * @param paint
 * @param chartWidth
 * @return
 */
public Rectangle getSingleAutoLegendSize(DefaultRenderer renderer, String[] titles, Paint paint, int legendWidth)
{
 float width = -1f;
 float height = -1f;

 paint.setTextSize(renderer.getLegendTextSize() * renderer.getZoomRate());
 int seriesCnt = Math.min(titles.length, renderer.getSeriesRendererCount());
    for (int i = 0; i < seriesCnt; i++)
    {
      String text = titles[i].replace("\n", " ");
      //the rest cell
        FontMetrics fm = paint.getFontMetrics();        
        // 文本高度
        height = Math.max((float)(Math.ceil(fm.descent - fm.ascent)), height);  
        // 文本宽度
        width = Math.max((float)(paint.measureText(text)), width);
    }

    float maxLegendTextWidth = legendWidth - getLegendShapeWidth(0) * renderer.getZoomRate() * 2;
    if(width > maxLegendTextWidth)
 {
  //the max width legend shape layout more than one lines
  int lines = (int)Math.ceil(width / maxLegendTextWidth);
  return new Rectangle(legendWidth, (int)Math.ceil(height) * lines);
 }
 else
 {
  return new Rectangle((int)Math.ceil(width + getLegendShapeWidth(0) * renderer.getZoomRate() * 2), (int)Math.ceil(height));
 }
}
项目:FunEditText-Android    文件:FunDrawable.java   
@Override
public void draw(Canvas canvas) {
    if (first) {
        invalidateXY();
        if (!mEditText.isFocused() && mEditText.getText().length() == 0)
            down();
        first = false;
    }

    FontMetrics fm = mTextPaint.getFontMetrics();
    float baseline = 0 - fm.top;
    float mTxtHeight = fm.bottom - fm.top;
    float mTxtWidth = mTextPaint.measureText(hintText);
    mRectF.set(xOffset, yOffset - baseline, xOffset + mTxtWidth, yOffset
            - baseline + mTxtHeight);

    canvas.save();
    canvas.clipRect(mRectF, Region.Op.DIFFERENCE);
    mShape.resize(canvas.getWidth(), canvas.getHeight() - mPaddingTop);
    canvas.translate(0, mPaddingTop);
    mShape.draw(canvas, mPaint);
    canvas.restore();

    mTextPaint.setTextSize(mEditText.getTextSize());
    mTextPaint.setColor(mColor);

    canvas.drawText(hintText, xOffset, yOffset, mTextPaint);
}
项目:connectbot    文件:TerminalBridge.java   
/**
 * Request a different font size. Will make call to parentChanged() to make
 * sure we resize PTY if needed.
 */
/* package */ final void setFontSize(float size) {
    if (size <= 0.0)
        return;

    defaultPaint.setTextSize(size);
    fontSize = size;

    // read new metrics to get exact pixel dimensions
    FontMetrics fm = defaultPaint.getFontMetrics();
    charTop = (int)Math.ceil(fm.top);

    float[] widths = new float[1];
    defaultPaint.getTextWidths("X", widths);
    charWidth = (int)Math.ceil(widths[0]);
    charHeight = (int)Math.ceil(fm.descent - fm.top);

    // refresh any bitmap with new font size
    if(parent != null)
        parentChanged(parent);

    for (FontSizeChangedListener ofscl : fontSizeChangedListeners)
        ofscl.onFontSizeChanged(size);

    host.setFontSize((int) fontSize);
    manager.hostdb.updateFontSize(host);

    forcedSize = false;
}
项目:EntboostIM    文件:AbMonitorView.java   
/**
 * 描述:TODO.
 *
 * @param canvas the canvas
 * @see android.view.View#onDraw(android.graphics.Canvas)
 * @author: amsoft.cn
 * @date:2013-6-17 上午9:04:49
 * @version v1.0
 */
@Override
protected void onDraw(Canvas canvas) {

    //if(D)Log4jLog.d(LONG_TAG, "--AbMonitorView onDraw--");
    canvas.drawColor(Color.argb(80, 0, 0, 0));
    if (mStartTime == -1) {
        mStartTime = SystemClock.elapsedRealtime();
        mCounter = 0;
    }

    long now = SystemClock.elapsedRealtime();
    long delay = now - mStartTime;

    if(delay!=0){
        // 计算帧速率
        mFps = (int)(mCounter * 1000 / delay);
    }

       String text = mFps + " fps";
       //获取值的文本的高度
       TextPaint mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
       mTextPaint.setTypeface(Typeface.DEFAULT);
       mTextPaint.setTextSize(16);
       FontMetrics fm  = mTextPaint.getFontMetrics();
       //得到行高
       int textHeight = (int)Math.ceil(fm.descent - fm.ascent)+2;
       int textWidth = (int)AbGraphical.getStringWidth(text,mTextPaint);


    canvas.drawText(text,(this.getWidth()-textWidth)/2, textHeight, mPaint);
    if (delay > 1000L) {
        mStartTime = now;
        mFps = mCounter;
        mCounter = 0;
    }
    mCounter++;
    super.onDraw(canvas);
}