MPAndroidChart PieChart如何更改中心文本以显示不同的颜色和字体大小 [英] MPAndroidChart PieChart how to change the center text to display the different color and font size

查看:794
本文介绍了MPAndroidChart PieChart如何更改中心文本以显示不同的颜色和字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我使用GITHUB的MPAndroidChart并绘制饼图以显示两行文本,每行具有不同的颜色和不同的字体大小,我检查了源代码,中心文本是一个字符串对象,并尝试使用:

Currently I use the MPAndroidChart from GITHUB and draw the piechart to display the two line text, each line has different color and different font size,I check the source codes and the center text is a string object, and try to use:

PAINT paint = pie_chart.getPaint(Chart.PAINT_CENTER_TEXT);

但看起来似乎行不通,有人会做这件事并指导我吗?

but looks like not work, would someone has the experience to do it and guide me about it.

推荐答案

研究了源代码之后,我更改了PieChartRenderer.java来更改 mCenterTextPaint.setColor(Color.parseColor("#333333")); mCenterTextPaint.setTextSize(Utils.convertDpToPixel(20f)); mCenterTextPaint.setTextAlign(Align.CENTER);

after study the source codes, i change the PieChartRenderer.java to change the mCenterTextPaint.setColor(Color.parseColor("#333333")); mCenterTextPaint.setTextSize(Utils.convertDpToPixel(20f)); mCenterTextPaint.setTextAlign(Align.CENTER);

然后使用"\ n"作为分隔符以获取第一行

then use the "\n" as a split to get the first line

   int index = centerText.indexOf("\n");

   Spannable tempSpannable = new SpannableString(centerText);
   tempSpannable.setSpan(new RelativeSizeSpan(0.75f), 0, index, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
   tempSpannable.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")),
                        0, index, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

   // If width is 0, it will crash. Always have a minimum of 1
   mCenterTextLayout = new StaticLayout(tempSpannable, 0, centerText.length(),
                        mCenterTextPaint,
                        (int)Math.max(Math.ceil(mCenterTextLastBounds.width()), 1.f),
                        Layout.Alignment.ALIGN_NORMAL, 1.f, 0.f, false);

然后得到我想要的是

then get what i wanted is

这篇关于MPAndroidChart PieChart如何更改中心文本以显示不同的颜色和字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆