This article shares the specific code of Android custom textview with pinyin tone for your reference. The specific content is as follows
1. Pinyin textview, simple to combine the pinyin array and the Chinese character array together to display multiple lines
import ; import ; import ; import ; import ; import ; import ; import ; import .ChineseCharacter2Spell; import ; @SuppressLint("AppCompatCustomView") public class SpellTextView extends TextView { private String[] pinyin; private String[] chinese; private TextPaint textPaintSpell = new TextPaint(Paint.ANTI_ALIAS_FLAG); private TextPaint textPaintChinese = new TextPaint(Paint.ANTI_ALIAS_FLAG); private int fontSizeSpell = ConvertUtils.dp2px(12); private int fontSizeChinese = ConvertUtils.dp2px(12); private int colorSpell = ("#1b97d6"); private int colorChinese = ("#000000"); public SpellTextView(Context context) { super(context); } public SpellTextView(Context context, AttributeSet attrs) { super(context, attrs); } public SpellTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initTextPaint(); } public void initTextPaint() { float denity = getResources().getDisplayMetrics().density; (denity); (denity); (); (); //Set font size (fontSizeSpell); (fontSizeChinese); (colorSpell); (colorChinese); } @Override protected void onDraw(Canvas canvas) { float widthMesure = 0f; int comlum = 1; float pinyinWidth; if (pinyin != null && > 0) { for (int index = 0; index < ; index++) { pinyinWidth = widthMesure + (pinyin[index]); if (pinyinWidth > getWidth()) { comlum++; widthMesure = 0; } (pinyin[index], widthMesure, (comlum * 2 - 1) * (()), textPaintSpell); (chinese[index], widthMesure + ((pinyin[index]) - (chinese[index])) / 2, (comlum * 2) * (()), textPaintChinese); if (index + 1 < ) { widthMesure = widthMesure + (pinyin[index] + 1); } else { widthMesure = widthMesure + (pinyin[index]); } } } } // Resources of pinyin and Chinese characters public void setSpellAndChinese(String[] pinYin, String[] chinese) { = pinYin; = chinese; } //Set text resources public void setStringResource(String string) { initTextPaint(); String[] spellArray = (string); StringBuilder stringBuilder = new StringBuilder(); for (String s : spellArray){ (s); (" "); } char[] chars = (); String[] chineseArray = new String[]; for (int i = 0; i < ;i++){ chineseArray[i] = (chars[i]); } setSpellAndChinese(spellArray,chineseArray); } //Set text color public void setStringColor(int spellColor,int chineseColor) { (spellColor); (chineseColor); } //Set the text size public void setFontSize(float spellFontSize,float chineseFontSize) { (ConvertUtils.dp2px(spellFontSize)); (ConvertUtils.dp2px(chineseFontSize)); } }
2. Use implementation ‘:pinyin4j:2.5.0’ to convert Chinese characters to pinyin implementation ‘:pinyin4j:2.5.0’
public static String[] getPinyinString(String character) { if (character != null && () > 0) { String[] pinyin = new String[()]; HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); (); (HanyuPinyinToneType.WITH_TONE_MARK); (HanyuPinyinVCharType.WITH_U_UNICODE); for (int index = 0; index < (); index++) { char c = (index); try { String[] pinyinUnit = (c, format); if (pinyinUnit == null) { pinyin[index] = " "; } else { pinyin[index] = pinyinUnit[0]; } } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) { (); } } return pinyin; } else { return null; } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.