zoukankan      html  css  js  c++  java
  • Canvas drawText实现中英文居中

    @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);
        }
  • 相关阅读:
    Aspect Oriented Programming
    jsp01
    监听器
    Java编写验证码
    servlet07
    MySQL02
    MySQL01
    Java的jdk1.6与jre1.8中存在的差异
    登陆验证和二级联动
    ajax和json
  • 原文地址:https://www.cnblogs.com/l2rf/p/3981695.html
Copyright © 2011-2022 走看看