Android自定义带拼音音调Textview

发布时间:2019-08-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android自定义带拼音音调Textview脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例为大家分享了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教程
脚本网站
android studio

脚本宝典总结

以上是脚本宝典为你收集整理的Android自定义带拼音音调Textview全部内容,希望文章能够帮你解决Android自定义带拼音音调Textview所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。