Android中怎么自定義帶拼音音調(diào)Textview,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括惠農(nóng)網(wǎng)站建設(shè)、惠農(nóng)網(wǎng)站制作、惠農(nóng)網(wǎng)頁(yè)制作以及惠農(nóng)網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,惠農(nóng)網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到惠農(nóng)省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
1.拼音textview,簡(jiǎn)單的為把拼音數(shù)組和漢字?jǐn)?shù)組結(jié)合在一起多行顯示
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);
//設(shè)置字體大小
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;
}
//設(shè)置文字資源
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);
}
//設(shè)置文字顏色
public void setStringColor(int spellColor,int chineseColor) {
textPaintSpell.setColor(spellColor);
textPaintChinese.setColor(chineseColor);
}
//設(shè)置文字大小
public void setFontSize(float spellFontSize,float chineseFontSize) {
textPaintSpell.setTextSize(ConvertUtils.dp2px(spellFontSize));
textPaintChinese.setTextSize(ConvertUtils.dp2px(chineseFontSize));
}
}2.漢字轉(zhuǎn)拼音使用 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;
}
}關(guān)于Android中怎么自定義帶拼音音調(diào)Textview問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
網(wǎng)頁(yè)名稱:Android中怎么自定義帶拼音音調(diào)Textview
網(wǎng)頁(yè)URL:http://www.chinadenli.net/article36/iphjpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、標(biāo)簽優(yōu)化、網(wǎng)站改版、移動(dòng)網(wǎng)站建設(shè)、外貿(mào)建站、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)