一、准备

使用非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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="原先基本字体"android:textSize="30dp" /><com.future.fontdemo.view.FontTextViewandroid: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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);drawText = (ImageView) findViewById(R.id.img_to_text);initLengthAndHeight();initFontType();}@Overrideprotected 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() {@Overridepublic 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,绘制内容的控制相对较难,且绘制汉字涉及下坡度、上坡度等细节内容,需要继续研究。

展示效果:

传送门

                风乍起,合当奋意向人生。


                                       起风了,唯有努力生存。 

使用非Android系统的其他字体相关推荐

  1. android系统自带字体设置,安卓手机系统字体设置-系统字体设置(Big Font) 安卓版v2.37-PC6安卓网...

    Big Font 改变系统字体大小:这个软件可以直接修改安卓系统字体的大小,相比原生系统的大中小肯那个还不能完全符合所有人.这个软件还不错. 官方介绍 透过改变系统字体大小Big Font应用,你可以 ...

  2. Android系统下不同字体的环境判断

    在java代码中就已经有了关于当前系统语言的判断: Locale locale = Locale.getDefault(); System.out.println(locale.getLanguage ...

  3. 【Android 系统】recovery字体大小修改

    简介 最近接到一个需求,客户觉得ota升级时,提示字体较小.需求增大提示字体的大小. 解决方案 上网找到直接修改build/core/Makefile文件中recovery_density的赋值.由 ...

  4. ios、android 系统字体说明

    后来了解到的手机系统 ios.android 等是不支持微软雅黑字体,为了满足产品的需要,保证视觉稿的还原度,手机端是如何定义微软雅黑字体呢? 相信大家会想到 @font-face 定义为微软雅黑字体 ...

  5. 替换android系统字体颜色,Android替换系统字体

    1.引言 最近老大安排一个任务,让我看看android 字体这块,将我们产品中的字体替换下.花了1.2天看懂,还得写篇文章,教程在组内进行分享.这次算是我进军Android系统的第一步.这篇文章基于A ...

  6. android 手写字体识别,一种基于Android系统的手写数学公式识别及生成MathML的方法...

    专利名称:一种基于Android系统的手写数学公式识别及生成MathML的方法 技术领域: 本发明属于模式识别技术领域,涉及数学公式中字符间的空间结构分析,具体涉及一种基于Android系统的手写数学 ...

  7. android自带中文字体,Android更换系统默认显示的字体使用自定义字体

    序言 上一篇Android 自定义字体,更换系统默认显示的字体使用自定义字体有讲到怎样指定控件显示指定字体,怎样整个软件显示指定字体,怎样WebView加载指定字体,但是还留下一个怎样修改整个系统的默 ...

  8. Android 系统(72)---Android系统增加字体库及修改系统默认字体

    Android系统增加字体库及修改系统默认字体 阅读数:53 转载出处:https://blog.csdn.net/hsaekong/article/details/80305263 前言 最近项目上 ...

  9. Android系统增加字体库及修改系统默认字体

    转载请注明出处:https://blog.csdn.net/hsaekong/article/details/80305263 前言 最近项目上需要修改android系统默认字体,现把总结记录下,便于 ...

最新文章

  1. python程序员在公司都是做什么的-为什么企业很难招聘到好的python程序员?
  2. PHP中使用ActiveMQ实现消息队列
  3. 5.关于QT中的网络编程,QTcpSocket,QUdpSocket
  4. JTA 深度历险 - 原理与实现
  5. VS封装给Unity使用的DLL
  6. trufflesuite/truffle-hdwallet-provider
  7. 人走茶凉!三星关闭最后一家中国手机工厂 因为打不过其他国产厂商?
  8. iOS已发布应用中对异常信息捕获和处理(转)
  9. 服装CAD计算机按钮在哪里,富仪服装CAD快捷键大全
  10. MATLAB 快速傅里叶变换分析
  11. 常用的三种非对称加密算法
  12. cαr怎么发音_韵母a的发音情况是()。
  13. C语言每日一练——第50天:八进制转十进制
  14. 解析Hl7消息,将消息可视化
  15. web网页添加天气插件(心知天气)
  16. python爬虫基本概述
  17. XtraReport控件详细属性说明
  18. 加装机械硬盘后如何给磁盘分区
  19. 【kimol君的无聊小发明】—用python写论文下载器(图形化界面)
  20. 将数组中的对象按照浏览器的x/y轴的显示方式进行排序

热门文章

  1. android studio登录注销,Android Studio开发基础之动态注册与注销BroadcastReceiver
  2. 京颐集团上云之路:如何助力中小型医疗行业信息化与全面上云?
  3. 天使投资人周哲:天使投资-为什么要找我
  4. 关于eclispe svn突然不能使用的解决方案SVN资源库报错:Could not create the view: org.tigris.subversion.subclipse.ui.repos
  5. 众多金融理财海外PPT模板素材一键即可获取
  6. HTML学习 1(基本格式、标题标签、段落标签、图片标签)
  7. 移动app实用测试指南
  8. 亚马逊物流背后的商业逻辑
  9. 云客网解析网站内容采集的注意要点
  10. 国产操作系统和CPU一览