为了账号安全,请及时绑定邮箱和手机立即绑定

Android drawText 在指定位置进行画文字

标签:
Android

首先 祭出  这个和 咱们  认知不同 的坐标系

首先 祭出  这个和 咱们  认知不同 的坐标系5bab95ac000184c108190460.jpg

图画的很性感。自娱自乐 哈哈!

下一张图 是在网上好多地方都有的,可以搜搜去看下具体的5bab95ad000104b804580076.jpg

我这里要说的是

canvas.drawText( text, x,y, textPaint);  

这个方法 

这里的x,y 就是图里面的小红点

我项目中的应用

5bab95ad0001e30e03360331.jpg

网上很多 文字在中间的 我找了一个 在里面改了改 供大家参考

在1080*1920 和720*1280 上瞧着 是没啥问题,有看到和遇到这个的同学,如果有什么问题的可以告诉我哦 谢谢~-~


[代码]java代码:

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

@Override

    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

 

        /**

         *   画最外层的大圆环

         */

        int centre = getWidth() / 2; //获取圆心的x坐标

        int radius = (int) (centre - roundWidth / 4);   //圆环的半径

        paint.setColor(roundColor);   //设置圆环的颜色

        paint.setStyle(Paint.Style.STROKE);   //设置空心

        paint.setStrokeWidth(roundWidth   / 5); //设置圆环的宽度

        paint.setAntiAlias(true);    //消除锯齿

        canvas.drawCircle(centre,   centre, radius, paint); //画出圆环

 

        Log.e("log",   centre + "");

 

        /**

         *   画进度百分比文字

         */

        paint.setStrokeWidth(0);

        paint.setColor(textColor);

        if (upTextSize != null) {

            paint.setTextSize(

                    SizeUtils.sp2px(getIntstance().getApplicationContext(),   upTextSize));

        }   else {

            paint.setTextSize(50);

        }

        paint.setStyle(Paint.Style.FILL);   //设置字是空心还是实心

        paint.setTypeface(Typeface.DEFAULT_BOLD);   //设置字体

        int percent;

        if (withPercent) {

            percent   = (int) (((float) progress / (float) max) * 100);  //中间的进度百分比,先转换成float在进行除法运算,不然都为0

        }   else {

            percent   = progress;

        }

        float textWidth;

        if (withPercent) {

            textWidth   = paint.measureText(percent + "%");   //测量字体宽度,我们需要根据字体的宽度设置在圆环中间

        }   else {

            textWidth   = paint.measureText(changeCountToK(progress) + "");   //测量字体宽度,我们需要根据字体的宽度设置在圆环中间

        }

        if (textIsDisplayable && style ==   STROKE) {

            if (withPercent) {

                canvas.drawText(percent   + "%", centre - textWidth / 2, centre - 10, paint); //画出进度百分比

            }   else {

                canvas.drawText(percent   + "", centre - textWidth / 2 - 8, centre + 20, paint); //画出进度百分比

            }

        }

        /**

         *   画进度比下的文字

         */

        paint.setStrokeWidth(0);

        try {

            paint.setColor(ContextCompat.getColor(getIntstance().getApplicationContext(),   R.color.text_gray));

        }   catch (NullPointerException   e) {

            e.printStackTrace();

        }

        if (downTextSize != null) {

            paint.setTextSize(SizeUtils.sp2px(getIntstance().getApplicationContext(),   downTextSize));

        }   else {

            paint.setTextSize(35);

        }

        paint.setTypeface(Typeface.DEFAULT);   //设置字体

        float textWidth2 =   paint.measureText(textPercent);   //百分比下面的文字的高度

        float txH2 = paint.getFontMetrics().descent -   paint.getFontMetrics().ascent;//第二行汉子的高度

        if (textIsDisplayable && style ==   STROKE) {

            if (withPercent) {

                canvas.drawText(textPercent,   centre - textWidth2 / 2 + 2, centre + txH2, paint); //

            }   else {

                canvas.drawText(textPercent,   centre - textWidth / 2 + textWidth + 2, centre + 20, paint); //

            }

        }

 

 

        /**

         *   画圆弧 ,画圆环的进度

         */

 

        //设置进度是实心还是空心

        paint.setStrokeWidth(roundWidth   / 5); //设置圆环的宽度

        paint.setColor(roundProgressColor);    //设置进度的颜色

        RectF   oval = new RectF(centre   - radius, centre - radius, centre

                +   radius, centre + radius);  //用于定义的圆弧的形状和大小的界限

 

        switch (style) {

            case STROKE: {

                paint.setStyle(Paint.Style.STROKE);

                canvas.drawArc(oval,   0, 360 *   progress / max, false, paint);  //根据进度画圆弧

                break;

            }

            case FILL: {

                paint.setStyle(Paint.Style.FILL_AND_STROKE);

                if (progress != 0)

                    canvas.drawArc(oval,   0, 360 *   progress / max, true, paint);  //根据进度画圆弧

                break;

            }

        }

 

    }

原文链接:http://www.apkbus.com/blog-795429-62577.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消