Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize Textview with Pinyin tone in Android

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to customize Textview with Pinyin tone in Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Pinyin textview, which simply combines Pinyin array with Chinese character array for multi-line display.

Import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.text.TextPaint;import android.util.AttributeSet;import android.widget.TextView;import com.cgtn.chineselearning.utils.ChineseCharacter2Spell;import com.cgtn.common.utils.ConvertUtils;@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 = Color.parseColor ("# 1b97d6"); private int colorChinese = Color.parseColor ("# 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; textPaintSpell.setStrokeWidth (denity); textPaintChinese.setStrokeWidth (denity); textPaintSpell.setTextAlign (Paint.Align.LEFT); textPaintChinese.setTextAlign (Paint.Align.LEFT); / / set font size textPaintSpell.setTextSize (fontSizeSpell); textPaintChinese.setTextSize (fontSizeChinese) TextPaintSpell.setColor (colorSpell); textPaintChinese.setColor (colorChinese);} @ Override protected void onDraw (Canvas canvas) {float widthMesure = 0f; int comlum = 1; float pinyinWidth; if (pinyin! = null & & pinyin.length > 0) {for (int index = 0; index)

< pinyin.length; index++) { pinyinWidth = widthMesure + textPaintSpell.measureText(pinyin[index]); if (pinyinWidth >

GetWidth () {comlum++; widthMesure = 0;} canvas.drawText (pinyin [index], widthMesure, (comlum * 2-1) * (textPaintChinese.getFontSpacing ()), textPaintSpell) Canvas.drawText (chinese [index], widthMesure + (textPaintSpell.measureText)-textPaintChinese.measureText) / 2, (comlum * 2) * (textPaintChinese.getFontSpacing ()), textPaintChinese); if (index + 1)

< pinyin.length) { widthMesure = widthMesure + textPaintSpell.measureText(pinyin[index] + 1); } else { widthMesure = widthMesure + textPaintSpell.measureText(pinyin[index]); } } } } //拼音和汉字的资源 public void setSpellAndChinese(String[] pinYin, String[] chinese) { this.pinyin = pinYin; this.chinese = chinese; } //设置文字资源 public void setStringResource(String string) { initTextPaint(); String[] spellArray = ChineseCharacter2Spell.getPinyinString(string); StringBuilder stringBuilder = new StringBuilder(); for (String s : spellArray){ stringBuilder.append(s); stringBuilder.append(" "); } char[] chars = string.toCharArray(); String[] chineseArray = new String[chars.length]; for (int i = 0; i < chars.length;i++){ chineseArray[i] = String.valueOf(chars[i]); } setSpellAndChinese(spellArray,chineseArray); } //设置文字颜色 public void setStringColor(int spellColor,int chineseColor) { textPaintSpell.setColor(spellColor); textPaintChinese.setColor(chineseColor); } //设置文字大小 public void setFontSize(float spellFontSize,float chineseFontSize) { textPaintSpell.setTextSize(ConvertUtils.dp2px(spellFontSize)); textPaintChinese.setTextSize(ConvertUtils.dp2px(chineseFontSize)); }} 2.汉字转拼音使用 implementation 'com.belerweb:pinyin4j:2.5.0' public static String[] getPinyinString(String character) { if (character != null && character.length() >

0) {String [] pinyin = new String [character.length ()]; HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat (); format.setCaseType (HanyuPinyinCaseType.LOWERCASE); format.setToneType (HanyuPinyinToneType.WITH_TONE_MARK); format.setVCharType (HanyuPinyinVCharType.WITH_U_UNICODE); for (int index = 0; index < character.length (); index++) {char c = character.charAt (index); try {String [] pinyinUnit = PinyinHelper.toHanyuPinyinStringArray (c, format) If (pinyinUnit = = null) {pinyin [index] = "";} else {pinyin [index] = pinyinUnit [0];}} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {badHanyuPinyinOutputFormatCombination.printStackTrace ();}} return pinyin;} else {return null;}} "how to customize Textview with Pinyin tone in Android" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report