Android自定義帶拼音音調(diào)Textview
本文實(shí)例為大家分享了Android自定義帶拼音音調(diào)Textview的具體代碼,供大家參考,具體內(nèi)容如下
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; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 啟動(dòng) Service(startservice和bindservice) 兩種方式的區(qū)別
andrid service 兩種啟動(dòng)方式:第一種startservice,第二種bindservice,大家對(duì)這兩種啟動(dòng)方式的區(qū)別了解嗎,下面跟著小編一起學(xué)習(xí)吧2015-11-11Android使用TransitionDrawable漸變切換多張圖片
這篇文章主要為大家詳細(xì)介紹了Android使用TransitionDrawable漸變切換多張圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08android自定義view用path畫(huà)長(zhǎng)方形
這篇文章主要為大家詳細(xì)介紹了android自定義view用path畫(huà)長(zhǎng)方形,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04Android 對(duì)話框(Dialog)大全示例(建立你自己的對(duì)話框)
android開(kāi)發(fā)中,對(duì)話框的使用還是很平凡的,本篇文章介紹了Android 對(duì)話框的實(shí)例,詳細(xì)的介紹了多種對(duì)話框的方法,有興趣的可以了解一下。2016-11-11Android入門(mén)之TabHost與TabWidget實(shí)例解析
這篇文章主要介紹了Android入門(mén)之TabHost與TabWidget,對(duì)于Android初學(xué)者有一定的學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下2014-08-08Android實(shí)現(xiàn)光點(diǎn)模糊漸變的自旋轉(zhuǎn)圓環(huán)特效
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)光點(diǎn)模糊漸變的自旋轉(zhuǎn)圓環(huán)特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06