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;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]);
}
}
}
}
//拼音和汉字的资源
publicvoidsetSpellAndChinese(String[]pinYin,String[]chinese){
this.pinyin=pinYin;
this.chinese=chinese;
}
//设置文字资源
publicvoidsetStringResource(Stringstring){
initTextPaint();
String[]spellArray=ChineseCharacter2Spell.getPinyinString(string);
StringBuilderstringBuilder=newStringBuilder();
for(Strings:spellArray){
stringBuilder.append(s);
stringBuilder.append("");
}
char[]chars=string.toCharArray();
String[]chineseArray=newString[chars.length];
for(inti=0;i<chars.length;i++){
chineseArray[i]=String.valueOf(chars[i]);
}
setSpellAndChinese(spellArray,chineseArray);
}
//设置文字颜色
publicvoidsetStringColor(intspellColor,intchineseColor){
textPaintSpell.setColor(spellColor);
textPaintChinese.setColor(chineseColor);
}
//设置文字大小
publicvoidsetFontSize(floatspellFontSize,floatchineseFontSize){
textPaintSpell.setTextSize(ConvertUtils.dp2px(spellFontSize));
textPaintChinese.setTextSize(ConvertUtils.dp2px(chineseFontSize));
}
}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<character.length();index++){
charc=character.charAt(index);
try{
String[]pinyinUnit=PinyinHelper.toHanyuPinyinStringArray(c,format);
if(pinyinUnit==null){
pinyin[index]="";
}else{
pinyin[index]=pinyinUnit[0];
}
}catch(BadHanyuPinyinOutputFormatCombinationbadHanyuPinyinOutputFormatCombination){
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
}
returnpinyin;
}else{
returnnull;
}
}关于Android中怎么自定义带拼音音调Textview问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注恰卡编程网行业资讯频道了解更多相关知识。
推荐阅读
-
我的世界windows版怎么下载手机版
-
android(如何快速开发框架 小米note开发版MIUI,安卓6.0,怎么安装Xposed框架)
稳定版,你必须先根除。你上网搜索安卓可以叫别人s框架,对方可以把框架做成jar包,把这个jar包加载到项目目录的libs文件中使...
-
android(studio 虚拟机启动不了 android studio可以当模拟器用吗)
androidstudio可以当模拟器用吗?AmdCUP引导模拟器有点复杂。雷电模拟器上的抖音怎么登录不上?不是,闪电模拟调用...
-
从实践中学习手机抓包与数据分析(android 手机抓包app)
android手机抓包app?netcapture抓包精灵app(手机抓包工具)又名sslcapture,是什么专业的安卓手机抓...
-
android(studio全局搜索 android studio怎么看app界面)
androidstudio怎么看app界面?在设备桌面点击运用直接进入到App界面,就也可以参与其他你的操作了。android-...
-
怎么把android框架源代码拉到本地(android studio如何运行别人的源代码)
androidstudio如何运行别人的源代码?androidstudio点击刚建在列表中你选择导入module,导入即可在用...
-
android(studio2022年使用教程 怎么安装Android studio详细教程)
怎么安装Androidstudio详细教程?androidstudio中haxm直接安装的方法追加:1、简单的方法打开Andr...
-
怎么使用Android基准配置文件Baseline Profile方案提升启动速度
怎么使用Android基准配置文件Baseline Profile方案提升启动速度...
-
HTML5如何实现禁止android视频另存为
HTML5如何实现禁止android视频另存为今天小编给大家分享一...
-
学java好还是学php好?
