学习笔记---绘制文字

这里写图片描述

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值