自定义View入门(六) - 绘制文字

本章目录

  • Part One:自定义View绘制文字

自定义View无非就是绘制图形和文字,本来觉得文字这块没啥难度,后来想了想,文字摆放位置这块还是有点技术含量在里面。

Part One:自定义View绘制文字

文字的绘制步骤跟图形还是一样的,首先创建一个画笔和Context。Context是为了密度转换使用的,比如dp转px或者sp转px之类的,这个都有相应的工具类,知道咋用即可。

    //文字画笔
    private Paint textPaint;
    //为了密度转换,需要一个context
    private Context context;

然后在构造方法里为context初始化,这步写的时候别忘了~

this.context = context;

接着呢,初始化画笔,这里用DesnsityUtils工具类的sp2px方法,设定画笔的文字大小,其它的都和画图形一样:

        //初始化文字画笔
        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setColor(circleColor);
        textPaint.setStyle(Paint.Style.STROKE);
        textPaint.setTextSize(DensityUtils.sp2px(context, 22));

最后就是在onDraw方法里调用canvas的drawText方法了。

        //在中心点绘制文字
        String text = currentProgress + "%";
        canvas.drawText(text, x, y, textPaint);

这里面有4个参数,text和textPaint好理解,一个是要绘制的文字,一个是绘制文字的画笔,那么x和y分别是啥。

  1. 先来看x
    从注释里可以看出,x是文字的起始x坐标,也就是最左边的位置。
    如果想要文字居中要怎么做的,很简单,用View的x轴中心点,减去文字宽度的一半即可:
        //在中心点绘制文字
        String text = currentProgress + "%";
        //获取文字的宽度,text是文本,然后从0开始到文字结束
        float textWidth = textPaint.measureText(text, 0, text.length());
        canvas.drawText(text, (getWidth() - textWidth) / 2, y, textPaint);
  1. 接下来看y
    从注释里知道,y是文字的基线位置的y轴坐标,这个就要设计到FontMetrics了。
    FontMetrics是Paint的一个静态内部类


    FontMetrics.gif

    如上图所示,其中包含5个float值:

  • leading:留给文字音标的距离
  • ascent:从基线到文字最高字母的顶点,值为负数
  • top:从基线到字母最高点加上ascent
  • descent:从基线到字母最低点
  • bottom:从基线到字母最低点加上decent

咱们要使用的字符都是常规字符,leading没啥用,top和bottom也是,都是为极少数字符预留的。
另外,需要注意的是,基线位置为0。上面的值为负数就是ascent,下面的值为整数,就是descent。
来看一张图:


基线位置.png

我们文字公式需要的是基线的y轴位置,也就是用(View的中心点 - 2和3这两条线之间距离),就是基线位置。
|ascent| + descent = (descent + 2和3之间的距离) * 2
转换一下就是
(|ascent| - descent) / 2 = 2和3之间的距离。
最后y轴的基线位置就可以确定了,就是
getHeight() / 2 + (|ascent| - descent) / 2
所以,最终我们的文字就可以写成这样:

        //在中心点绘制文字
        String text = currentProgress + "%";
        //获取文字的宽度,text是文本,然后从0开始到文字结束
        float textWidth = textPaint.measureText(text, 0, text.length());
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        canvas.drawText(text, (getWidth() - textWidth) / 2,
                getHeight() / 2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2, textPaint);

总结一下我们写到现在的代码
CircleView.java:

package com.terana.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class CircleView extends View{
    //画圆的画笔
    private Paint circlePaint;
    //圆的半径
    private float radius;
    //圆的颜色
    private int circleColor;
    //圆的宽度
    private float strokeWidth;
    //动态圆的颜色
    private int progressColor;
    //动态圆的画笔
    private Paint progressPaint;
    //动态圆的当前进度值
    private int currentProgress;
    //动态圆的范围
    private RectF initRectF;
    //文字画笔
    private Paint textPaint;
    //为了密度转换,需要一个context
    private Context context;

    public CircleView(Context context) {
        this(context, null);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
        initAttrs(context, attrs);
        initVariables();
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.CircleView, 0, 0);//获取TypedArray对象
        radius = typedArray.getDimension(R.styleable.CircleView_radius,
                100);//获取半径,默认值为100
        strokeWidth = typedArray.getDimension(R.styleable.CircleView_strokeWidth,
                2);//获取圆环的宽度,默认为2
        circleColor = typedArray.getColor(R.styleable.CircleView_circleColor,
               Color.BLACK);//获取圆环的颜色,默认为红色
        progressColor = typedArray.getColor(R.styleable.CircleView_progressColor,
                Color.RED);//获取圆环的颜色,默认为红色
        typedArray.recycle();//TypedArray对象是共享的资源,所以在获取完值之后必须要调用recycle()方法来回收。
    }

    private void initVariables() {
        //创建画圆的画笔
        circlePaint = new Paint();
        circlePaint.setAntiAlias(true);//画笔去除锯齿
        circlePaint.setColor(circleColor);//画笔颜色为红色
        circlePaint.setStyle(Paint.Style.STROKE);//画的圆是空心圆,FILL为实心圆
        circlePaint.setStrokeWidth(strokeWidth);//设置圆的线条宽度为2

        //创建动态圆的范围
        initRectF = new RectF();

        //创建动态圆的画笔
        progressPaint = new Paint();
        progressPaint.setAntiAlias(true);//画笔去除锯齿
        progressPaint.setColor(progressColor);//画笔颜色为红色
        progressPaint.setStyle(Paint.Style.STROKE);//画的圆是空心圆,FILL为实心圆
        progressPaint.setStrokeWidth(strokeWidth);//设置圆的线条宽度为2

        //初始化文字画笔
        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setColor(circleColor);
        textPaint.setStyle(Paint.Style.STROKE);
        textPaint.setTextSize(DensityUtils.sp2px(context, 22));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //getSuggestedMinimumWidth用于返回View推荐的最小宽度
        int width = getMeasuresize(getSuggestedMinimumWidth(), widthMeasureSpec);
        //getSuggestedMinimumHeight用于返回View推荐的最小高度
        int height = getMeasuresize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(width, height);//必须调用此方法,否则会抛出异常
    }

    private int getMeasuresize(int size, int measureSpec) {
        int result = size;
        //从MeasureSpec中获取测量模式
        int specMode = MeasureSpec.getMode(measureSpec);
        //从MeasureSpec中获取测量大小
        int specSize = MeasureSpec.getSize(measureSpec);
        switch (specMode){
            //父容器没有对当前View有任何限制,要多大就多大,这种情况一般用于系统内部,表示一种测量状态。
            case MeasureSpec.UNSPECIFIED:
                result = size;//用推荐值即可
                break;
            //父容器已经检测出View所需要的精确大小,这个时候View的最终大小就是SpecSize的值。
            //对应match_parent和具体的数值。
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
            //父容器指定了一个可用大小即SpecSize,View的大小不能大于这个值。对应wrap_content。
            case MeasureSpec.AT_MOST:
                result = Math.min(200, specSize);
                break;
        }
        return result;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int minimum = Math.min(getWidth() / 2, getHeight() / 2);
        radius = radius <= minimum ? radius : minimum;
        //画圆
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius - strokeWidth / 2, circlePaint);

        //动态圆的总进度
       int totalProgress = 100;
        //获取动态圆的矩形区域
        initRectF.top = getWidth() / 2 - radius + strokeWidth / 2;
        initRectF.left = getHeight() / 2 - radius + strokeWidth / 2;
        initRectF.right = getWidth() / 2 + radius - strokeWidth / 2;
        initRectF.bottom = getHeight() / 2 + radius - strokeWidth / 2;
        //本质其实是画一个圆弧形的矩形
        canvas.drawArc(initRectF, -90,((float) currentProgress / totalProgress)
                * 360 , false, progressPaint);

        //在中心点绘制文字
        String text = currentProgress + "%";
        //获取文字的宽度,text是文本,然后从0开始到文字结束
        float textWidth = textPaint.measureText(text, 0, text.length());
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        canvas.drawText(text, (getWidth() - textWidth) / 2,
                getHeight() / 2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent) / 2, textPaint);
    }

    public void setRadius(float mRadius) {
        this.radius = mRadius;
        invalidate();//重绘
    }

    public void setCircleColor(int mCircleColor) {
        circlePaint.setColor(mCircleColor);
        invalidate();//重绘
    }

    public void setStrokeWidth(float mStrokeWidth) {
        circlePaint.setStrokeWidth(mStrokeWidth);
        invalidate();//重绘
    }

    public void updateProgress(int mCurrentProgress) {
        this.currentProgress = mCurrentProgress;
        postInvalidate();
    }
}

MainActivity.java:

package com.terana.customview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private CircleView circleView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    @Override
    protected void onResume() {
        super.onResume();
        new Thread(new Runnable() {
            @Override
            public void run() {
                int temp = 0;
                while(temp <=60){
                    circleView.updateProgress(temp++);
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    private void initViews() {
        circleView = findViewById(R.id.circleView);
    }
}

DensityUtils.java:

package com.terana.customview;

import android.content.Context;
import android.util.TypedValue;

public class DensityUtils
{
    private DensityUtils()
    {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static int dp2px(Context context, float dpVal)
    {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, context.getResources().getDisplayMetrics());
    }

    public static int sp2px(Context context, float spVal)
    {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                spVal, context.getResources().getDisplayMetrics());
    }

    public static float px2dp(Context context, float pxVal)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (pxVal / scale);
    }

    public static float px2sp(Context context, float pxVal)
    {
        return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
    }

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.terana.customview.MainActivity">

    <com.terana.customview.CircleView
        android:id="@+id/circleView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        app:circleColor="#696969"
        app:progressColor="#1E88E5"
        app:radius="44dp"
        app:strokeWidth="6dp" />

</RelativeLayout>

attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CircleView">
        <attr name="circleColor" format="color"/>
        <attr name="radius" format="dimension"/>
        <attr name="strokeWidth" format="dimension"/>
        <attr name="progressColor" format="color"/>
    </declare-styleable>
</resources>

最终的效果为:


添加文字.gif

绘制的部分差不多了,有兴趣的话可以再自行扩充,下一节会说说自定义View的点击事件。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容