Java Code Examples for android.graphics.Paint#getFontMetrics()

The following examples show how to use android.graphics.Paint#getFontMetrics() . 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: CompassDrawer.java    From Compass with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 2
Source File: SideBar.java    From country-picker-android with Apache License 2.0 6 votes vote down vote up
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));
}
 
Example 3
Source File: MainDraw.java    From kAndroid with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 4
Source File: SimpleHeader.java    From AndroidStudyDemo with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 5
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 6
Source File: CircleTextView.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: IconTextSpan.java    From tysq-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 8
Source File: CText.java    From FabricView with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example 9
Source File: Ruler.java    From DS4Android with MIT License 5 votes vote down vote up
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);

}
 
Example 10
Source File: PieView.java    From SimplePomodoro-android with MIT License 5 votes vote down vote up
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();
}
 
Example 11
Source File: BookAssetsPieChartView.java    From kAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * 高度
 *
 * @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;
}
 
Example 12
Source File: Utils.java    From LChart with Apache License 2.0 4 votes vote down vote up
public static int textHeightAsc(Paint paint) {
    paint.getFontMetrics(mFontMetrics);
    return (int) (Math.abs(mFontMetrics.ascent));
}
 
Example 13
Source File: BaseView.java    From kAndroid with Apache License 2.0 4 votes vote down vote up
protected float getFontHeight(Paint paint) {
    Paint.FontMetrics fm = paint.getFontMetrics();
    float textHeight = fm.descent - fm.ascent;
    return textHeight;
}
 
Example 14
Source File: BaseDetailView.java    From kAndroid with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 15
Source File: BitmapUtil.java    From SortedContactUI with Apache License 2.0 4 votes vote down vote up
/**
 * @return 返回指定笔离文字顶部的基准距离
 */
public static float getFontLeading(Paint paint) {
	FontMetrics fm = paint.getFontMetrics();
	return fm.leading - fm.ascent;
}
 
Example 16
Source File: Utils.java    From JNChartDemo with Apache License 2.0 4 votes vote down vote up
public static float getLineHeight(Paint paint) {
    Paint.FontMetrics metrics = paint.getFontMetrics();
    return metrics.descent - metrics.ascent;
}
 
Example 17
Source File: Utils.java    From NetKnight with Apache License 2.0 4 votes vote down vote up
public static float getLineSpacing(Paint paint) {
    Paint.FontMetrics metrics = paint.getFontMetrics();
    return metrics.ascent - metrics.top + metrics.bottom;
}
 
Example 18
Source File: BitmapUtil.java    From SortedContactUI with Apache License 2.0 4 votes vote down vote up
/**
 * @return 返回指定笔的文字高度
 */
public static float getFontHeight(Paint paint) {
	FontMetrics fm = paint.getFontMetrics();
	return fm.descent - fm.ascent;
}
 
Example 19
Source File: Utils.java    From iMoney with Apache License 2.0 4 votes vote down vote up
public static float getLineHeight(Paint paint) {
    Paint.FontMetrics metrics = paint.getFontMetrics();
    return metrics.descent - metrics.ascent;
}
 
Example 20
Source File: VMDimen.java    From VMLibrary with Apache License 2.0 2 votes vote down vote up
/**
 * 计算文字的高度
 *
 * @param paint 绘制文字的画笔
 * @return 返回字符串高度
 */
public static float getTextHeight(Paint paint) {
    Paint.FontMetrics fm = paint.getFontMetrics();
    return (float) Math.ceil(fm.descent - fm.ascent);
}