您好,登錄后才能下訂單哦!
本篇內容介紹了“Android中如何自定義帶拼音音調Textview”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
1.拼音textview,簡單的為把拼音數組和漢字數組結合在一起多行顯示
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); //設置字體大小 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(pinyin[index]) - textPaintChinese.measureText(chinese[index])) / 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; } }
“Android中如何自定義帶拼音音調Textview”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。