学习笔记---绘制文字
来源:互联网 发布:包天下网络怎么赚钱 编辑:程序博客网 时间:2024/05/16 15:03
Paint和TextPaint,不能直接让文字换行。需要其他额外操作。
代码:
FontView
package com.chen.view;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Typeface;import android.text.TextPaint;import android.util.AttributeSet;import android.view.View;public class FontView extends View { //文字内容 private static final String TEXT = "没有设置字体属性:哈123哈abc嗯"; private static final String TEXT_1 = "DEFAULT:哈123哈哈abc嗯"; private static final String TEXT_2 = "DEFAULT_BOLD:哈123哈abc嗯"; private static final String TEXT_3 = "SANS_SERIF:哈123哈abc嗯"; private static final String TEXT_4 = "SERIF:哈123哈abc嗯"; private static final String TEXT_5 = "MONOSPACE:哈123哈abc嗯"; //文字画笔 private TextPaint textPaint; private TextPaint textPaint_1; private TextPaint textPaint_2; private TextPaint textPaint_3; private TextPaint textPaint_4; private TextPaint textPaint_5; private int baseX, baseY; public FontView(Context context) { this(context, null); } public FontView(Context context, AttributeSet attrs) { this(context, attrs, -1); } public FontView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initPaint(context); } private void initPaint(Context context) { textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); textPaint.setTextSize(70); /** * 设置文本的对其方式,可供选的方式有三种:CENTER,LEFT和RIGHT * * 如果选择LEFT,从指定位置开始,往右绘制文字。即:文字从指定绘制位置起始位置开始,左对齐 * 如果选择RIGHT,从指定位置开始,往左绘制文字。即:文字从指定绘制位置起始位置开始,右对齐 * 如果选择CENTER,从指定位置开始,往左右两边绘制文字。即:文字从指定绘制位置起始位置开始,文字中间对齐 */ textPaint.setTextAlign(Paint.Align.CENTER); textPaint.setColor(Color.BLACK); textPaint_1 = new TextPaint(textPaint); textPaint_1.setTypeface(Typeface.DEFAULT); textPaint_2 = new TextPaint(textPaint); textPaint_2.setTypeface(Typeface.DEFAULT_BOLD); textPaint_3 = new TextPaint(textPaint); //The NORMAL style of the default sans serif typeface textPaint_3.setTypeface(Typeface.SANS_SERIF); // The NORMAL style of the default serif typeface textPaint_4 = new TextPaint(textPaint); textPaint_4.setTypeface(Typeface.SERIF); //The NORMAL style of the default monospace typeface textPaint_5 = new TextPaint(textPaint); textPaint_5.setTypeface(Typeface.MONOSPACE); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); baseX = w / 2; baseY = h / 2; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /* * @param text The text to be drawn * @param x The x-coordinate of the origin of the text being drawn * @param y The y-coordinate of the baseline of the text being drawn * @param paint The paint used for the text (e.g. color, size, style) */ canvas.drawText(TEXT, baseX, baseY / 6, textPaint); canvas.drawText(TEXT_1, baseX, baseY / 3, textPaint_1); canvas.drawText(TEXT_2, baseX, baseY / 2, textPaint_2); canvas.drawText(TEXT_3, baseX, baseY / 3 * 2, textPaint_3); canvas.drawText(TEXT_4, baseX, baseY / 6 * 5, textPaint_4); canvas.drawText(TEXT_5, baseX, baseY, textPaint_5); }}
布局代码中使用:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.chen.view.FontView android:layout_width="match_parent" android:layout_height="match_parent"/></LinearLayout>
阅读全文
0 0
- 学习笔记---绘制文字
- FreeType文字绘制笔记
- opencv学习笔记(三)-绘制基本图形及文字
- 自定义控件学习笔记(四)文字的绘制
- C#学习笔记:在控件上绘制文字
- OpenGL笔记16 文字绘制
- html5 canvas学习--绘制文字
- 【小白笔记】PHP学习之路 (30) --绘制文字及相关库函数
- opencv 2.x学习笔记(十)随机数发生器、绘制文字
- GDI+学习笔记6-统计报表的背景与文字的绘制
- opencv学习笔记-5,绘制线条、矩形、圆、椭圆,添加文字
- html5笔记4 — canvas绘制文字
- D3D学习笔记:绘制
- OpenCV学习:随机数发生器&绘制文字
- UI学习 -绘制文字功能练习
- Android 自定义View学习之文字绘制
- android自定义View学习3--文字绘制
- 绘制文字
- 弹出框闪退问题
- Flask代码结构总结
- mongodb3.4创建只读视图实例
- Linux——利用Shell脚本编写进度条
- lintcode[156]:合并区间
- 学习笔记---绘制文字
- Git修改分支名称
- 安卓低功耗蓝牙(BLE)开发教程
- unity Vuforia + 二维码解析 使用同一个摄像机
- PAT乙级1027. 打印沙漏(20)
- adb shell dumpsys SurfaceFlinge 分析
- 初见蓝桥——历年试题 小朋友排队
- linux之浅谈crond和crontab
- GreenDao3.0框架使用详解