Android的绘制文本对象FontMetrics的介绍及绘制

系统 1628 0
一。Android绘制文本对象FontMetrics介绍

   FontMetrics对象
   它以四个基本坐标为基准,分别为:

   ・FontMetrics.top
   ・FontMetrics.ascent
   ・FontMetrics.descent
   ・FontMetrics.bottom
  



   实例JAVA代码如下:
   Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG);
   textPaint.setTextSize( 35);
   textPaint.setColor( Color.WHITE);

   // FontMetrics对象
   FontMetrics fontMetrics = textPaint.getFontMetrics();
   String text = "abcdefghijklmnopqrstu";

   // 计算每一个坐标
   float baseX = 0;
   float baseY = 100;
   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, textPaint);

   // BaseLine描画
   Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
   baseLinePaint.setColor( Color.RED);
   canvas.drawLine(0, baseY, getWidth(), baseY, baseLinePaint);

   // Base描画
   canvas.drawCircle( baseX, baseY, 5, baseLinePaint);

   // TopLine描画
   Paint topLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
   topLinePaint.setColor( Color.LTGRAY);
   canvas.drawLine(0, topY, getWidth(), topY, topLinePaint);

   // AscentLine描画
   Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
   ascentLinePaint.setColor( Color.GREEN);
   canvas.drawLine(0, ascentY, getWidth(), ascentY, ascentLinePaint);

   // DescentLine描画
   Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
   descentLinePaint.setColor( Color.YELLOW);
   canvas.drawLine(0, descentY, getWidth(), descentY, descentLinePaint);

   // ButtomLine描画
   Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
   bottomLinePaint.setColor( Color.MAGENTA);
   canvas.drawLine(0, bottomY, getWidth(), bottomY, bottomLinePaint);

二。繪製文本。

1.TextUtil 類



    package com.wistron.test.DrawText;

import java.util.Vector;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.view.KeyEvent;

public class TextUtil {
 private int mTextPosx = 0;// x坐标
 private int mTextPosy = 0;// y坐标
 private int mTextWidth = 0;// 绘制宽度
 private int mTextHeight = 0;// 绘制高度
 private int mFontHeight = 0;// 绘制字体高度
 private int mPageLineNum = 0;// 每一页显示的行数
 private int mCanvasBGColor = 0;// 背景颜色
 private int mFontColor = 0;// 字体颜色
 private int mAlpha = 0;// Alpha值
 private int mRealLine = 0;// 字符串真实的行数
 private int mCurrentLine = 0;// 当前行
 private int mTextSize = 0;// 字体大小
 private String mStrText = "";
 private Vector mString = null;
 private Paint mPaint = null;

 public TextUtil(String StrText, int x, int y, int w, int h, int bgcolor,
   int textcolor, int alpha, int textsize) {
  mPaint = new Paint();
  mString = new Vector();
  this.mStrText = StrText;
  this.mTextPosx = x;
  this.mTextPosy = y;
  this.mTextWidth = w;
  this.mTextHeight = h;
  this.mCanvasBGColor = bgcolor;
  this.mFontColor = textcolor;
  this.mAlpha = alpha;
  this.mTextSize = textsize;
 }

 public void InitText() {
  mString.clear();// 清空Vector
  // 对画笔属性的设置
  // mPaint.setARGB(this.mAlpha, Color.red(this.mFontColor), Color
  // .green(this.mFontColor), Color.blue(this.mFontColor));
  mPaint.setTextSize(this.mTextSize);
  mPaint.setColor(Color.BLUE);
  this.GetTextIfon();
 }

 /**
  * 得到字符串信息包括行数,页数等信息
  */
 public void GetTextIfon() {
  char ch;
  int w = 0;
  int istart = 0;
  FontMetrics fm = mPaint.getFontMetrics();// 得到系统默认字体属性
  mFontHeight = (int) (Math.ceil(fm.descent - fm.top) + 2);// 获得字体高度
  mPageLineNum = mTextHeight / mFontHeight;// 获得行数
  int count = this.mStrText.length();
  for (int i = 0; i < count; i++) {
   ch = this.mStrText.charAt(i);
   float[] widths = new float[1];
   String str = String.valueOf(ch);
   mPaint.getTextWidths(str, widths);
   if (ch == '\n') {
    mRealLine++;// 真实的行数加一
    mString.addElement(this.mStrText.substring(istart, i));
    istart = i + 1;
    w = 0;
   } else {
    w += (int) Math.ceil(widths[0]);
    if (w > this.mTextWidth) {
     mRealLine++;// 真实的行数加一
     mString.addElement(this.mStrText.substring(istart, i));
     istart = i;
     i--;
     w = 0;
    } else {
     if (i == count - 1) {
      mRealLine++;// 真实的行数加一
      mString.addElement(this.mStrText.substring(istart,
        count));
     }
    }
   }
  }
 }

 /**
  * 绘制字符串
  * 
  * @param canvas
  */
 public void DrawText(Canvas canvas) {
  for (int i = this.mCurrentLine, j = 0; i < this.mRealLine; i++, j++) {
   if (j > this.mPageLineNum) {
    break;
   }
   canvas.drawText((String) (mString.elementAt(i)), this.mTextPosx,
     this.mTextPosy + this.mFontHeight * j, mPaint);
  }
 }

 /**
  * 翻页等按键处理
  * 
  * @param keyCode
  * @param event
  * @return
  */
 public boolean KeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
   if (this.mCurrentLine > 0) {
    this.mCurrentLine--;
   }
  } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
   if ((this.mCurrentLine + this.mPageLineNum) < (this.mRealLine - 1)) {
    this.mCurrentLine++;
   }
  }
  return false;
 }

}

  



2. MyView 類

    package com.wistron.test.DrawText;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.View;

public class MyView extends View implements Runnable {
 private final Paint mPaint;
 private int mICount = 0;
 private final TextUtil mTextUtil;

 public MyView(Context context) {
  super(context);
  mPaint = new Paint();
  String string = "测试自动换行(第1行)\n\n测试自动换行(第2行)\n测试自动换行(第3行)\n测试自动换行(第4行)\n测试自动换行(第5行)\n测试自动换行(第6行)\n测试自动换行(第7行)"
    + "生活教會了我們什麼,自己的感覺現在已經徹底的隕滅,生命中彷彿是蒸發了為自己幸福而爭的念頭,一切就這樣的循規蹈矩!";
  mTextUtil = new TextUtil(string, 15, 150, 300, 80, Color.LTGRAY,
    0xff00ff, 255, 16);
  mTextUtil.InitText();
  new Thread(this).start();
 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  canvas.drawColor(Color.LTGRAY);
  mPaint.setAntiAlias(true);
  if (mICount < 100) {
   mICount++;
  }
  mPaint.setColor(Color.RED);
  canvas.drawText("裝載進度:" + mICount + "%......", 10, 20, mPaint);
  mTextUtil.DrawText(canvas);
 }

 @Override
 public void run() {
  Thread.currentThread();
  while (!Thread.interrupted()) {
   try {
    Thread.sleep(10);
   } catch (Exception ex) {
    ex.printStackTrace();
    Thread.currentThread().interrupt();
   }
   this.postInvalidate();
  }
 }

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {

  return mTextUtil.KeyDown(keyCode, event);
 }

}

  



3. DrawText  Activity.

    package com.wistron.test.DrawText;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;

public class DrawText extends Activity {
    /** Called when the activity is first created. */
 private MyView mv;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  mv = new MyView(this);
  setContentView(mv);
 }

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  mv.onKeyDown(keyCode, event);
  return super.onKeyDown(keyCode, event);
 }

}
  

Android的绘制文本对象FontMetrics的介绍及绘制文本


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论