日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 自定义带拼音Textview 带音调

發布時間:2023/12/10 Android 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 自定义带拼音Textview 带音调 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 拼音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);}@Overrideprotected 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));} }
  • 漢字轉拼音使用 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 带音调的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。