Android自定义带拼音音调Textview
本文实例为大家分享了Android自定义带拼音音调Textview的具体代码,供大家参考,具体内容如下
1.拼音textview,简单的为把拼音数组和汉字数组结合在一起多行显示
importandroid.annotation.SuppressLint;
importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.text.TextPaint;
importandroid.util.AttributeSet;
importandroid.widget.TextView;
importcom.cgtn.chineselearning.utils.ChineseCharacter2Spell;
importcom.cgtn.common.utils.ConvertUtils;
@SuppressLint("AppCompatCustomView")
publicclassSpellTextViewextendsTextView{
privateString[]pinyin;
privateString[]chinese;
privateTextPainttextPaintSpell=newTextPaint(Paint.ANTI_ALIAS_FLAG);
privateTextPainttextPaintChinese=newTextPaint(Paint.ANTI_ALIAS_FLAG);
privateintfontSizeSpell=ConvertUtils.dp2px(12);
privateintfontSizeChinese=ConvertUtils.dp2px(12);
privateintcolorSpell=Color.parseColor("#1b97d6");
privateintcolorChinese=Color.parseColor("#000000");
publicSpellTextView(Contextcontext){
super(context);
}
publicSpellTextView(Contextcontext,AttributeSetattrs){
super(context,attrs);
}
publicSpellTextView(Contextcontext,AttributeSetattrs,intdefStyleAttr){
super(context,attrs,defStyleAttr);
initTextPaint();
}
publicvoidinitTextPaint(){
floatdenity=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
protectedvoidonDraw(Canvascanvas){
floatwidthMesure=0f;
intcomlum=1;
floatpinyinWidth;
if(pinyin!=null&&pinyin.length>0){
for(intindex=0;indexgetWidth()){
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
2.汉字转拼音使用implementation‘com.belerweb:pinyin4j:2.5.0'
publicstaticString[]getPinyinString(Stringcharacter){
if(character!=null&&character.length()>0){
String[]pinyin=newString[character.length()];
HanyuPinyinOutputFormatformat=newHanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
for(intindex=0;index
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。