getFontMetrics ( )源码实例Demo

android.graphics.Paint#getFontMetrics ( )源码实例Demo

下面列出了android.graphics.Paint#getFontMetrics ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Compass   文件: CompassDrawer.java
private void drawDirectionText(Canvas canvas, float degree, String text, float radiusPx, Paint paint) {
    Paint.FontMetrics fm = paint.getFontMetrics();
    float height = fm.bottom - fm.top + fm.leading;

    float cos = (float) Math.cos(Math.toRadians(degree));
    float sin = (float) Math.sin(Math.toRadians(degree));

    float x = (cos * (radiusPx)) + mCenter.x;
    float y = (sin * (radiusPx)) + mCenter.y;

    canvas.save();
    canvas.translate(x, y);

    canvas.rotate(90 + degree);
    canvas.drawText(text, -paint.measureText(text) / 2.0f, height, paint);
    canvas.restore();
}
 
源代码2 项目: country-picker-android   文件: SideBar.java
public SideBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SideBar, defStyleAttr, 0);
    letterColor = ta.getColor(R.styleable.SideBar_letterColor, Color.BLACK);
    selectColor = ta.getColor(R.styleable.SideBar_selectColor, Color.CYAN);
    letterSize = ta.getDimensionPixelSize(R.styleable.SideBar_letterSize, 24);
    ta.recycle();
    paint = new Paint();
    //消除锯齿
    paint.setAntiAlias(true);
    Paint.FontMetrics fontMetrics = paint.getFontMetrics();
    textHeight = (float) Math.ceil(fontMetrics.descent - fontMetrics.ascent);  //1.1---2   2.1--3
    String[] letters = {"A", "B", "C", "D",
            "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    indexes.addAll(Arrays.asList(letters));
}
 
源代码3 项目: AndroidStudyDemo   文件: SimpleHeader.java
public SimpleHeader(Context context) {
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setStyle(Style.FILL);
    int fontSize = (int) TypedValue
            .applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, context.getResources()
                    .getDisplayMetrics());
    mPaint.setTextSize(fontSize);
    mPaint.setTextAlign(Align.CENTER);
    mTextColor = 0xffffffff;
    mPointColor = 0xffffffff;
    mFontOffset = -(mPaint.getFontMetrics().top + mPaint.getFontMetrics().bottom) / 2;
    mHeight = (int) TypedValue
            .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, context.getResources()
                    .getDisplayMetrics());
    mPointRadius =
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2.5f, context.getResources()
                    .getDisplayMetrics());
    mCircleRadius = mPointRadius * 3.5f;
}
 
源代码4 项目: kAndroid   文件: MainDraw.java
@Override
public void drawMaxAndMin(@NonNull BaseKChartView view, Canvas canvas, float maxX, float minX,
                          ICandle maxPoint, ICandle minPoint) {
    float high = view.getMainY(maxPoint.getHighPrice());
    float low = view.getMainY(minPoint.getLowPrice());

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.GRAY);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setTextSize(view.getTextSize());
    float aa = view.getSclase();

    if (aa < 1.5) {
        paint.setTextScaleX((2.0f - aa));
    } else {
        paint.setTextScaleX(0.5f);
    }

    Paint.FontMetrics fm = paint.getFontMetrics();
    float textHeight = fm.descent - fm.ascent;
    float baseLine = (textHeight - fm.bottom - fm.top) / 2;
    float paddingTop = DensityUtil.dp2px( 8);//距顶
    float paddingBottom = DensityUtil.dp2px( 1);//距底
    canvas.drawText(maxPoint.getHighPrice() + "", maxX, high - baseLine+paddingTop, paint);
    canvas.drawText(minPoint.getLowPrice() + "", minX, low + baseLine-paddingBottom, paint);
}
 
源代码5 项目: Trebuchet   文件: 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));
    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;
}
 
源代码6 项目: o2oa   文件: CircleTextView.java
private Bitmap createCircleImage(Bitmap source, int min) {
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    Bitmap target = Bitmap.createBitmap(min, min, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(target);
    canvas.drawCircle(min / 2, min / 2, min / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    if (source != null){
        canvas.drawBitmap(source, 0, 0, paint);
    }else{
        paint.setColor(inColor);
        canvas.drawCircle(min / 2, min / 2, min / 2, paint);
    }
    if (outColor != -1) {
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(outStrokeWidth);
        paint.setColor(outColor);
        canvas.drawCircle(min / 2, min / 2, min / 2, paint);
    }
    if(mText != null){
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(1);
        if(mTextColor != 0 ){
            paint.setColor(mTextColor);
        }
        if(mTextSize != 0 ){
            paint.setTextSize(mTextSize);
        }
        paint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetrics fm = paint.getFontMetrics();
        float fFontHeight = (float)Math.ceil(fm.descent - fm.ascent);
        canvas.drawText(mText, mWidth/2, mHeight/2+fFontHeight/4, paint);
    }

    return target;
}
 
源代码7 项目: tysq-android   文件: IconTextSpan.java
/**
 * draw
 *
 * @param text   完整文本
 * @param start  setSpan里设置的start
 * @param end    setSpan里设置的start
 * @param x
 * @param top    当前span所在行的上方y
 * @param y      y其实就是metric里baseline的位置
 * @param bottom 当前span所在行的下方y(包含了行间距),会和下一行的top重合
 * @param paint  使用此span的画笔
 */
@Override
public void draw(@NonNull Canvas canvas,
                 CharSequence text,
                 int start,
                 int end,
                 float x,
                 int top,
                 int y,
                 int bottom,
                 @NonNull Paint paint) {
    //画背景
    Paint bgPaint = new Paint();
    bgPaint.setColor(mContext.getResources().getColor(mBgColorResId));
    bgPaint.setStyle(Paint.Style.FILL);
    bgPaint.setAntiAlias(true);
    Paint.FontMetrics metrics = paint.getFontMetrics();

    float textHeight = metrics.descent - metrics.ascent;
    //算出背景开始画的y坐标
    float bgStartY = y + (textHeight - mBgHeight) / 2 + metrics.ascent;

    //画背景
    RectF bgRect = new RectF(x, bgStartY, x + mBgWidth, bgStartY + mBgHeight);
    canvas.drawRoundRect(bgRect, mRadius, mRadius, bgPaint);

    //把字画在背景中间
    TextPaint textPaint = new TextPaint();
    textPaint.setColor(mContext.getResources().getColor(mTextColorResId));
    textPaint.setTextSize(mTextSize);
    textPaint.setAntiAlias(true);
    textPaint.setTextAlign(Paint.Align.CENTER);  //这个只针对x有效
    Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
    float textRectHeight = fontMetrics.bottom - fontMetrics.top;
    canvas.drawText(mText, x + mBgWidth / 2, bgStartY + (mBgHeight - textRectHeight) / 2 - fontMetrics.top, textPaint);
}
 
源代码8 项目: FabricView   文件: CText.java
private void calculateTextSizes() {
    Paint p = getPaint();
    Paint.FontMetrics metric = p.getFontMetrics();
    int textHeight = (int) Math.ceil(metric.descent - metric.ascent);
    int y = (int)(textHeight - metric.descent);

    Rect bounds = new Rect();
    getPaint().getTextBounds(getText(), 0, getText().length(), bounds);
   // setYcoords(getYcoords() + y);
    setHeight(bounds.height() + (MARGIN*2));
    setWidth(bounds.width()+ (MARGIN*2));
}
 
源代码9 项目: DS4Android   文件: Ruler.java
public static void handleTextByPos(Object str, Paint paint, Canvas canvas, int x, int y, Rect rect) {
    String res = str.toString();
    paint.getTextBounds(res, 0, res.length(), rect);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setColor(Color.BLACK);
    Paint.FontMetrics fm = paint.getFontMetrics();
    rect.offset(-rect.width() / 2 + x, y);


    canvas.drawRect(rect, paint);

    paint.setColor(Color.WHITE);
    canvas.drawText(res, x, y, paint);

}
 
源代码10 项目: SimplePomodoro-android   文件: PieView.java
public PieView(Context context, AttributeSet attrs){
    super(context, attrs);
    textSize = MyUtils.sp2px(context, 15);
    lineThickness = MyUtils.dip2px(context, 1);
    lineLength = MyUtils.dip2px(context, 10);

    textPaint = new Paint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(TEXT_COLOR);
    textPaint.setTextSize(textSize);
    textPaint.setTextAlign(Paint.Align.CENTER);
    Paint.FontMetrics fm = new Paint.FontMetrics();
    textPaint.getFontMetrics(fm);
    textRect = new Rect();
    textPaint.getTextBounds("18",0,1,textRect);
    redPaint = new Paint(textPaint);
    redPaint.setColor(RED_COLOR);
    linePaint = new Paint(textPaint);
    linePaint.setColor(GRAY_COLOR);
    linePaint.setStrokeWidth(lineThickness);
    whitePaint = new Paint(linePaint);
    whitePaint.setColor(Color.WHITE);
    tempPoint = new Point();
    pieCenterPoint = new Point();
    tempPointRight = new Point();
    cirRect = new RectF();
    leftTextWidth = textPaint.measureText("18");
    rightTextWidth = textPaint.measureText("6");
    topTextHeight = textRect.height();
}
 
源代码11 项目: kAndroid   文件: BookAssetsPieChartView.java
/**
 * 高度
 *
 * @param paint
 * @return
 */
public float getFontBaseLineHeight(Paint paint) {
    Paint.FontMetrics fm = paint.getFontMetrics();
    float textHeight = fm.descent - fm.ascent;
    float baseLine = (textHeight - fm.bottom - fm.top) / 2;
    return baseLine;
}
 
源代码12 项目: NetKnight   文件: Utils.java
public static float getLineSpacing(Paint paint) {
    Paint.FontMetrics metrics = paint.getFontMetrics();
    return metrics.ascent - metrics.top + metrics.bottom;
}
 
源代码13 项目: iMoney   文件: Utils.java
public static float getLineHeight(Paint paint) {
    Paint.FontMetrics metrics = paint.getFontMetrics();
    return metrics.descent - metrics.ascent;
}
 
源代码14 项目: SortedContactUI   文件: BitmapUtil.java
/**
 * @return 返回指定笔的文字高度
 */
public static float getFontHeight(Paint paint) {
	FontMetrics fm = paint.getFontMetrics();
	return fm.descent - fm.ascent;
}
 
源代码15 项目: LChart   文件: Utils.java
public static int textHeightAsc(Paint paint) {
    paint.getFontMetrics(mFontMetrics);
    return (int) (Math.abs(mFontMetrics.ascent));
}
 
源代码16 项目: JNChartDemo   文件: Utils.java
public static float getLineHeight(Paint paint) {
    Paint.FontMetrics metrics = paint.getFontMetrics();
    return metrics.descent - metrics.ascent;
}
 
源代码17 项目: SortedContactUI   文件: BitmapUtil.java
/**
 * @return 返回指定笔离文字顶部的基准距离
 */
public static float getFontLeading(Paint paint) {
	FontMetrics fm = paint.getFontMetrics();
	return fm.leading - fm.ascent;
}
 
源代码18 项目: kAndroid   文件: BaseDetailView.java
protected float getFontBaseLineHeight(Paint paint) {
    Paint.FontMetrics fm = paint.getFontMetrics();
    float textHeight = fm.descent - fm.ascent;
    float baseLine = (textHeight - fm.bottom - fm.top) / 2;
    return baseLine;
}
 
源代码19 项目: kAndroid   文件: BaseView.java
protected float getFontHeight(Paint paint) {
    Paint.FontMetrics fm = paint.getFontMetrics();
    float textHeight = fm.descent - fm.ascent;
    return textHeight;
}
 
源代码20 项目: VMLibrary   文件: VMDimen.java
/**
 * 计算文字的高度
 *
 * @param paint 绘制文字的画笔
 * @return 返回字符串高度
 */
public static float getTextHeight(Paint paint) {
    Paint.FontMetrics fm = paint.getFontMetrics();
    return (float) Math.ceil(fm.descent - fm.ascent);
}