在图片上准确的绘制文字
来源:互联网 发布:上海穷游住宿攻略知乎 编辑:程序博客网 时间:2024/04/28 16:04
package com.test.imgtest;
/**
* @项目 SurfaceDemo 文件名 GLFont.java
* @GLFont
*/
/**
* @author leolaurel
* @version 1.0.0 2012-7-5
*/
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.FontMetrics;
public class GLFont {
/*
* 默认采用白色字体,宋体文字加粗
*/
public static Bitmap getImage(int width, int height ,String mString, int size) {
return getImage(width, height, mString, size, Color.RED, Typeface.create("宋体",Typeface.BOLD));
}
public static Bitmap getImage(int width, int height ,String mString,int size ,int color) {
return getImage(width, height, mString, size, color, Typeface.create("宋体",Typeface.BOLD));
}
public static Bitmap getImage(int width, int height ,String mString,int size ,int color, String familyName) {
return getImage(width, height, mString, size, color, Typeface.create(familyName,Typeface.BOLD));
}
public static Bitmap getImage(int width, int height ,String mString,int size, int color, Typeface font) {
int x = width;
int y = height;
Bitmap bmp = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
//图象大小要根据文字大小算下,以和文本长度对应
Canvas canvasTemp = new Canvas(bmp);
canvasTemp.drawColor(Color.BLACK);
Paint p = new Paint();
p.setColor(color);
p.setTypeface(font);
p.setAntiAlias(true);//去除锯齿
p.setFilterBitmap(true);//对位图进行滤波处理
p.setTextSize(scalaFonts(size));
float tX = (x - getFontlength(p, mString))/2;
float tY = (y - getFontHeight(p))/2+getFontLeading(p);
canvasTemp.drawText(mString,tX,tY,p);
return bmp;
}
/**
* 根据屏幕系数比例获取文字大小
* @return
*/
private static float scalaFonts(int size) {
//暂未实现
return size;
}
/**
* @return 返回指定笔和指定字符串的长度
*/
public static float getFontlength(Paint paint, String str) {
return paint.measureText(str);
}
/**
* @return 返回指定笔的文字高度
*/
public static float getFontHeight(Paint paint) {
FontMetrics fm = paint.getFontMetrics();
return fm.descent - fm.ascent;
}
/**
* @return 返回指定笔离文字顶部的基准距离
*/
public static float getFontLeading(Paint paint) {
FontMetrics fm = paint.getFontMetrics();
return fm.leading- fm.ascent;
}
}
/**
* @项目 SurfaceDemo 文件名 GLFont.java
* @GLFont
*/
/**
* @author leolaurel
* @version 1.0.0 2012-7-5
*/
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.FontMetrics;
public class GLFont {
/*
* 默认采用白色字体,宋体文字加粗
*/
public static Bitmap getImage(int width, int height ,String mString, int size) {
return getImage(width, height, mString, size, Color.RED, Typeface.create("宋体",Typeface.BOLD));
}
public static Bitmap getImage(int width, int height ,String mString,int size ,int color) {
return getImage(width, height, mString, size, color, Typeface.create("宋体",Typeface.BOLD));
}
public static Bitmap getImage(int width, int height ,String mString,int size ,int color, String familyName) {
return getImage(width, height, mString, size, color, Typeface.create(familyName,Typeface.BOLD));
}
public static Bitmap getImage(int width, int height ,String mString,int size, int color, Typeface font) {
int x = width;
int y = height;
Bitmap bmp = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
//图象大小要根据文字大小算下,以和文本长度对应
Canvas canvasTemp = new Canvas(bmp);
canvasTemp.drawColor(Color.BLACK);
Paint p = new Paint();
p.setColor(color);
p.setTypeface(font);
p.setAntiAlias(true);//去除锯齿
p.setFilterBitmap(true);//对位图进行滤波处理
p.setTextSize(scalaFonts(size));
float tX = (x - getFontlength(p, mString))/2;
float tY = (y - getFontHeight(p))/2+getFontLeading(p);
canvasTemp.drawText(mString,tX,tY,p);
return bmp;
}
/**
* 根据屏幕系数比例获取文字大小
* @return
*/
private static float scalaFonts(int size) {
//暂未实现
return size;
}
/**
* @return 返回指定笔和指定字符串的长度
*/
public static float getFontlength(Paint paint, String str) {
return paint.measureText(str);
}
/**
* @return 返回指定笔的文字高度
*/
public static float getFontHeight(Paint paint) {
FontMetrics fm = paint.getFontMetrics();
return fm.descent - fm.ascent;
}
/**
* @return 返回指定笔离文字顶部的基准距离
*/
public static float getFontLeading(Paint paint) {
FontMetrics fm = paint.getFontMetrics();
return fm.leading- fm.ascent;
}
}
0 0
- 在图片上准确的绘制文字
- 在QListWidgetItem上绘制文字和图片
- Core Graphics框架学习之在图片上绘制文字
- android使用Canvas在图片上绘制文字
- Core Graphics框架学习之在图片上绘制文字
- Android 使用Canvas在图片上绘制文字
- 向图片上绘制文字
- 在图片UIImage上绘制文字(字符串文字转图片)
- 在图片上动态的写文字
- 【VB.NET2010】在PictureBox上绘制文字的相关问题
- 在画布上实现正弦曲线和文字的绘制
- 图片上绘制水印文字(类目)
- 在按钮上绘制图片
- 在图片上画文字
- 在图片上添加文字
- 在图片上添加文字
- Flex进度条上的显示文字(完成率)不需要绝对准确
- php绘制在图片上的正余弦曲线
- Express文件上传之Multer
- C# CS格式文件导出为Dll库
- PHP实现验证码图片
- 【软工视频】需求分析
- 第一次参加亚洲区域赛(广州赛区):倒数第一
- 在图片上准确的绘制文字
- 利用MinGW64编译多线程版FFTW
- cocos2dx 3.3 setGlobalZOrder对label没有作用
- Linux之vi编辑器
- 《算法竞赛入门经典》习题2-1 位数(digit)
- 体会棋盘游戏中的数据存储
- 关于共有和私有ip的一点了解
- POJ 3522 Slim Span (并查集 + 枚举 + kruskal)
- HDU--2602 Bone Collector