您好,欢迎来到源码搜藏!分享精神,快乐你我!提示:担心找不到本站?在百度搜索“源码搜藏”,网址永远不丢失!
  • 首 页
  • 在线工具
  • 当前位置:首页 > 安卓源码 > 技术博客 >

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

    时间:2016-12-06 22:18 来源:互联网 作者:源码搜藏 浏览:收藏 挑错 推荐 打印

    首先 祭出 这个和 咱们 认知不同 的坐标系 图画的很性感。自娱自乐 哈哈! 下一张图 是在网上好多地方都有的,可以搜搜去看下具体的 我这里要说的是 canvas.drawText( text, x,y, textPaint); 这个方法 这里的x,y 就是图里面的小红点 我项目中的应用 网上很 首先 祭出  这个和 咱们  认知不同 的坐标系Android drawText 在指定位置进行画文字
    图画的很性感。自娱自乐 哈哈!
    下一张图 是在网上好多地方都有的,可以搜搜去看下具体的Android drawText 在指定位置进行画文字
     
    我这里要说的是
    canvas.drawText( text, x,y, textPaint);  
    这个方法 
    这里的x,y 就是图里面的小红点
    我项目中的应用

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

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

    在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;
                }
            }
     
        }

    Android drawText 在指定位置进行画文字转载http://www.codesocang.com/anzhuoyuanma/boke/33966.html
    标签:网站源码