码迷,mamicode.com
首页 > 其他好文 > 详细

Canvas drawText实现中英文居中

时间:2014-09-19 17:36:15      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   div   sp   log   on   c   line   

@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实现中英文居中

标签:style   blog   color   div   sp   log   on   c   line   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!