使用非Android系统的其他字体
来源:互联网 发布:socket java 长连接 编辑:程序博客网 时间:2024/06/10 07:12
一、准备
使用非Android手机自带字体,首先需要有字体文件【ttf文件】。为不涉及权益纠纷,请使用网络免费开放字体。度娘搜索下载即可。将ttf文件放在资源文件目录下。
Android Studio创建资源文件夹:
二、第一种使用方法
以自定义控件的方式重写一个TextView。
** * 功能:设置TextView的字体为 正楷 * 作者:vision * 时间:2016/12/15 */public class FontTextView extends TextView { public FontTextView(Context context) { super(context); initContent(context); } public FontTextView(Context context, AttributeSet attrs) { super(context, attrs); initContent(context); } public FontTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initContent(context); } /** * 初始化内容 * * @param context */ private void initContent(Context context) { AssetManager am = context.getAssets(); Typeface typeface = Typeface.createFromAsset(am, "Font.ttf");//字体文件名 setTypeface(typeface); }}使用即行:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.future.fontdemo.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="原先基本字体" android:textSize="30dp" /> <com.future.fontdemo.view.FontTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="汉仪免费字体" android:textSize="30dp" /></LinearLayout>
方法优势:实现简单,单View实现的效果即可用此方法实现。
三、第二种使用方法
原理:使用笔绘制,设置笔型。
public class MainActivity extends AppCompatActivity { /** * 汉字书写目的地 */ private ImageView drawText; /** * 目标控件的高 */ private int destHeight; /** * 目标空间的宽 */ private int destLength; /** * 新的字体类型 */ private Typeface typeface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawText = (ImageView) findViewById(R.id.img_to_text); initLengthAndHeight(); initFontType(); } @Override protected void onResume() { super.onResume(); String content = "以正迎以奇胜123"; drawWillWritingFlagFont(content); } /** * 初始化汉字字体 */ private void initFontType() { String url = Environment.getExternalStorageDirectory().toString() + "/font/"; File file = new File(url + "akaFrivolity.ttf");// File file = new File(url + "rename.ttf"); if (!file.exists()) { try { /** * 复制资源文件到存储文件 */ FileUtil.CopyAssetFile("akaFrivolity.ttf", url);// FileUtil.CopyAssetFile("rename.ttf", url); } catch (Exception e) { e.printStackTrace(); } } typeface = Typeface.createFromFile(file); } /** * 初始化控件的大小 */ private void initLengthAndHeight() { int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); drawText.measure(w, h); destLength = drawText.getMeasuredWidth(); destHeight = drawText.getMeasuredHeight(); /* ViewTreeObserver vto = drawText.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { destHeight = drawText.getMeasuredHeight(); destLength = drawText.getMeasuredWidth(); return true; } });*/ /* ViewTreeObserver vto2 = drawText.getViewTreeObserver(); vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { drawText.getViewTreeObserver().removeGlobalOnLayoutListener(this); destHeight = drawText.getMeasuredHeight(); destLength = drawText.getMeasuredWidth(); } });*/ } /** * 绘制出内容 * * @param currChar */ private void drawWillWritingFlagFont(String currChar) { Bitmap b = Bitmap.createBitmap(destLength, destHeight, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); p.setStrokeWidth(1);// 基于500像素里的粗细 p.setColor(Color.BLACK); p.setTextSize(30); p.setTypeface(typeface); /** * 计算基线,免除超出框外 */ float baseline; Paint.FontMetrics fontMetrics = p.getFontMetrics(); baseline = 0 + (destHeight - 0 - fontMetrics.descent + fontMetrics.ascent) / 2 - fontMetrics.ascent; //去掉新创建paint 可能有问题 Paint paint = new Paint(); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); c.drawPaint(paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); c.drawText(currChar, 0, baseline, p); drawText.setImageBitmap(b); }}
注意:
1,使用ImageView设置Bitmap的形式展示最后结果;
2,Imageview的大小设置不能为包裹内容,在没有设置背景图时;获取控件大小为0;
3,现存有多种获取控件的方式,暂不明具体原理;
4,绘制内容的控制相对较难,且绘制汉字涉及下坡度、上坡度等细节内容,需要继续研究。
展示效果:
传送门
风乍起,合当奋意向人生。
起风了,唯有努力生存。
0 0
- 使用非Android系统的其他字体
- android使用非系统默认的字体
- android 使用其他字体
- Android中使用其他格式的字体
- JavaSE中使用非系统自带的字体
- 使用其他字体
- android 字体的使用
- Android字体的使用
- android里 通过Intent来启动其他非系统应用程序
- android下摄像头的使用,非系统调用
- Android paint字体的使用
- Android ttf字体的使用
- 利用其他工具(非ADT)开发Android上的应用程序
- VC6.0修改字体(非系统自带的5种字体)
- 关于Android系统字体的一些知识
- Android系统添加新的字体
- android系统自带的字体外观
- Android 系统原生的typeface字体
- 请求重定向和请求转发
- 说话习惯
- 【code force】模拟 746C
- 零行代码为 App 添加异常加载占位图
- Linux-常见环境变量及其作用
- 使用非Android系统的其他字体
- 服务器Reactor架构浅析
- 网络编程作业——UDP传输
- excel打开csv文件乱码问题
- 判断一个数是否是2的N次方
- 网络编程作业——UDP传输
- Poj_3278 Catch That Cow(BFS)
- xml中id带+与不带+的区别
- 【数据库开发】MySQL修改root密码