content: /*
   * Copyright  2017 [AllenCoder]
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.allen.apputils;
  
  import android.graphics.Paint;
  import android.graphics.Rect;
  
  /**
   * 文 件 名: PaintUtil
   * 创 建 人: Allen
   * 创建日期: 2017/5/3 11:15
   * 修改时间：
   * 修改备注：
   */
  
  public class PaintUtil {
      private PaintUtil() {
      }
  
      /**
       * calculates the approximate width of a text, depending on a demo text
       * avoid repeated calls (e.g. inside drawing methods)
       *
       * @param paint
       * @param demoText
       * @return
       */
      public static int calcTextWidth(Paint paint, String demoText) {
          return (int) paint.measureText(demoText);
      }
      /**
       * calculates the approximate height of a text, depending on a demo text
       * avoid repeated calls (e.g. inside drawing methods)
       *
       * @param paint
       * @param demoText
       * @return
       */
      public static int calcTextHeight(Paint paint, String demoText) {
  
          Rect r = new Rect();
          paint.getTextBounds(demoText, 0, demoText.length(), r);
          return r.height();
      }
  
      public static float getLineHeight(Paint paint) {
          Paint.FontMetrics metrics = paint.getFontMetrics();
          return metrics.descent - metrics.ascent;
      }
  
      public static float getLineSpacing(Paint paint) {
          Paint.FontMetrics metrics = paint.getFontMetrics();
          return metrics.ascent - metrics.top + metrics.bottom;
      }
  }
  