首页 > 其他 > 详细

Canvas drawText实现中英文居中

时间:2014-09-19 17:36:15      阅读:282      评论:0      收藏:0      [点我收藏+]
@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    
            Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            // Measure a text
            Rect textBounds = new Rect();
            mTextPaint.getTextBounds(String.valueOf(mLetter), 0, 1, textBounds);
            float textWidth = mTextPaint.measureText(String.valueOf(mLetter));
            float textHeight = textBounds.height();

            FontMetrics fontMetrics = mTextPaint.getFontMetrics();
            // 计算文字高度
            float fontHeight = fontMetrics.bottom - fontMetrics.top;
            // 计算文字baseline
            float textBaseY = getHeight() - (getHeight() - fontHeight) / 2
                    - fontMetrics.bottom;

            // Draw the text
            canvas.drawText(String.valueOf(mLetter), getWidth() / 2f
                    - textWidth / 2f, textBaseY, mTextPaint);
    }

 

Canvas drawText实现中英文居中

原文:http://www.cnblogs.com/l2rf/p/3981695.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!