Android使用FontMetrics对象计算位置坐标

Canvas绘制文本时,使用FontMetrics对象,计算位置的坐标。

public static class FontMetrics {
  /**
   * The maximum distance above the baseline for the tallest glyph in
   * the font at a given text size.
   */
  public float  top;
  /**
   * The recommended distance above the baseline for singled spaced text.
   */
  public float  ascent;
  /**
   * The recommended distance below the baseline for singled spaced text.
   */
  public float  descent;
  /**
   * The maximum distance below the baseline for the lowest glyph in
   * the font at a given text size.
   */
  public float  bottom;
  /**
   * The recommended additional space to add between lines of text.
   */
  public float  leading;
}

它的各基准线可以参考下图:

上图其实是通过代码画出来的,具体代码如下:

/** 绘制FontMetrics对象的各种线 */
mPaint.reset();
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(80);
// FontMetrics对象
FontMetrics fontMetrics = mPaint.getFontMetrics();
String text = "abcdefg";
// 计算每一个坐标
float textWidth = mPaint.measureText(text);
float baseX = 30;
float baseY = 700;
float topY = baseY + fontMetrics.top;
float ascentY = baseY + fontMetrics.ascent;
float descentY = baseY + fontMetrics.descent;
float bottomY = baseY + fontMetrics.bottom;
// 绘制文本
canvas.drawText(text, baseX, baseY, mPaint);
// BaseLine描画
mPaint.setColor(Color.RED);
canvas.drawLine(baseX, baseY, baseX + textWidth, baseY, mPaint);
mPaint.setTextSize(20);
canvas.drawText("base", baseX + textWidth, baseY, mPaint);
// Base描画
canvas.drawCircle(baseX, baseY, 5, mPaint);
// TopLine描画
mPaint.setColor(Color.LTGRAY);
canvas.drawLine(baseX, topY, baseX + textWidth, topY, mPaint);
canvas.drawText("top", baseX + textWidth, topY, mPaint);
// AscentLine描画
mPaint.setColor(Color.GREEN);
canvas.drawLine(baseX, ascentY, baseX + textWidth, ascentY, mPaint);
canvas.drawText("ascent", baseX + textWidth, ascentY + 10, mPaint);
// DescentLine描画
mPaint.setColor(Color.YELLOW);
canvas.drawLine(baseX, descentY, baseX + textWidth, descentY, mPaint);
canvas.drawText("descent", baseX + textWidth, descentY, mPaint);
// ButtomLine描画
mPaint.setColor(Color.MAGENTA);
canvas.drawLine(baseX, bottomY, baseX + textWidth, bottomY, mPaint);
canvas.drawText("buttom", baseX + textWidth, bottomY + 10, mPaint);

相信通过以上程序,能够很好的理解topLine,buttomLine,baseLine,ascentLine,descentLine。
另外:Paint类有两个方法

/**
 * Return the distance above (negative) the baseline (ascent) based on the
 * current typeface and text size.
 *
 * @return the distance above (negative) the baseline (ascent) based on the
 *     current typeface and text size.
 */
public native float ascent();

/**
 * Return the distance below (positive) the baseline (descent) based on the
 * current typeface and text size.
 *
 * @return the distance below (positive) the baseline (descent) based on
 *     the current typeface and text size.
 */
public native float descent();

ascent():the distance above the baseline(baseline以上的height)
descent():the distance below the baseline(baseline以下的height)

所以ascent() + descent() 可以看成文字的height。

到此为止,怎么获取文字的height和width都已经揭晓了:

获取height : mPaint.ascent() + mPaint.descent()

获取width : mPaint.measureText(text)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android中FontMetrics的几个属性全面讲解

    今天和大家聊一聊Android中关于FontMetrics的几个属性的理解,在Android中用画笔绘制文字时,文字最终的大小是和绘制文字的字体的类型和字体的大小是相关的. 设置字体类型 Paint.setTypeface(Typeface typeface) 设置字体大小 Paint.setTextSize(float textSize) Paint.FontMetrics有5个属性,并且这5个属性都是跟字体相关的,下面是官方API文档的解释: 翻译一下他的意思: 1. 基准点是baselin

  • Android获取点击屏幕的位置坐标

    在Android开发过程中,有时需要获取触摸位置的坐标,以便作进一步处理,比如做炫酷的动画效果,或者响应其他操作. 本文简单介绍Android中触屏操作时,触屏的开始位置.当前位置.结束位置. 布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:l

  • Android编程之绘制文本(FontMetrics)实现方法

    本文实例讲述了Android编程之绘制文本(FontMetrics)实现方法.分享给大家供大家参考,具体如下: Canvas 作为绘制文本时,使用FontMetrics对象,计算位置的坐标. 它的思路和java.awt.FontMetrics的基本相同. FontMetrics对象 它以四个基本坐标为基准,分别为: FontMetrics.top FontMetrics.ascent FontMetrics.descent FontMetrics.bottom 该图片将如下 代码如下: Pain

  • Android使用FontMetrics对象计算位置坐标

    Canvas绘制文本时,使用FontMetrics对象,计算位置的坐标. public static class FontMetrics { /** * The maximum distance above the baseline for the tallest glyph in * the font at a given text size. */ public float top; /** * The recommended distance above the baseline for

  • Android Intent传递对象的两种方法(Serializable,Parcelable)详细介绍

    Android Intent传递对象的两种方法(Serializable,Parcelable)详细介绍 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口,为了让大家更容易理解我还是照常写

  • Android中将Bitmap对象以PNG格式保存在内部存储中的方法

    在Android中进行图像处理的任务时,有时我们希望将处理后的结果以图像文件的格式保存在内部存储空间中,本文以此为目的,介绍将Bitmap对象的数据以PNG格式保存下来的方法. 1.添加权限 由于是对SD card进行操作,必不可少的就是为你的程序添加读写权限,需要添加的内容如下: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

  • android将Bitmap对象保存到SD卡中的方法

    本文实例讲述了android将Bitmap对象保存到SD卡中的方法.分享给大家供大家参考.具体如下: Bitmap logoBitmap = BitmapFactory.decodeResource(mcontext.getResources(), R.drawable.arcnote_logo); ByteArrayOutputStream logoStream = new ByteArrayOutputStream(); boolean res = logoBitmap.compress(B

  • Android Color颜色过度计算实现代码

    Android Color颜色过度计算实现代码 在看自定义TypeEvaluator来计算属性动画的属性值时,用到了对颜色的过度计算,翻看了好多博客,找到了比较有优秀的解决方案,在此记录,以备后用. 实现效果图: 实现代码: /** * 根据fraction值来计算当前的颜色. */ private int getCurrentColor(float fraction, int startColor, int endColor) { int redCurrent; int blueCurrent

  • 详解Android中Intent对象与Intent Filter过滤匹配过程

    如果对Intent不是特别了解,可以参见博文<详解Android中Intent的使用方法>,该文对本文要使用的action.category以及data都进行了详细介绍.如果想了解在开发中常见Intent的使用,可以参见<Android中Intent习惯用法>. 本文内容有点长,希望大家可以耐心读完. 本文在描述组件在manifest中注册的Intent Filter过滤器时,统一用intent-filter表示. 一.概述 我们知道,Intent是分两种的:显式Intent和隐式

  • Android中传递对象的三种方法的实现

    Android中,Activity和Fragment之间传递对象,可以通过将对象序列化并存入Bundle或者Intent中进行传递,也可以将对象转化为JSON字符串,进行传递. 序列化对象可以使用Java的Serializable的接口.Parcelable接口.转化成JSON字符串,可以使用Gson等库. 1.Serializable public class Author implements Serializable{ private int id; private String name

  • Android传递Bitmap对象在两个Activity之间

    通过内部存储方式实现了在两个Activity之间传递Bitmap对象以及其它支持串行化的Java对象,关键点有如下: 1.  HTTP客户端下载图片,通过ImageView对象显示 2.  把ImageView上的Bitmap对象从当前Activity传递到另外一个Activity中并显示出来 3.  基于串行化传递Java对象数据 首先看我是怎么实现HTTP客户端下载图片,通过异步Task接口实现HTTP客户端下载图片并通过Handler来更新ImageView,代码如下: package c

  • 深入Android 五大布局对象的应用

    FrameLayout(帧布局),LinearLayout (线性布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局)FrameLayout:FrameLayout是最简单的一个布局对象.它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 ,比如,一张你要发布的图片.所有的子元素将会固定在屏幕的左上角:你不能为FrameLayout中的一个子元素指定一个位置.后一个子元素将会直接在前 一个子元素之上进行

随机推荐