a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁(yè) > 系統(tǒng) > Android > 正文

Android自定義帶拼音音調(diào)Textview

2019-10-21 21:31:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(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í)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美日韩一区二区三区免费视频 | 久久99国产一区二区三区 | 国产精品v欧美精品v日韩 | 在线播放国产精品 | 看a网站 | 精品视频一区二区在线观看 | a级片视频在线观看 | 91亚洲精品久久久 | 黄色污污视频在线观看 | 日韩免费网站 | 欧洲一区| 亚洲国产精品av | 国产一区二区三区精品久久久 | 日韩精品在线视频 | 国产日皮视频 | 亚洲国产精品久久久久 | 精品亚洲一区二区三区四区五区 | 2022久久国产露脸精品国产 | 国产精品一区二区日韩新区 | 欧美精品免费在线观看 | 国产区视频 | 国产日韩亚洲欧美 | 一区二区免费 | 韩国精品一区二区 | 超碰97国产精品人人cao | 日本在线看 | a级毛片视频免费观看 | 日韩人体在线 | 欧美综合国产精品久久丁香 | 亚洲va欧美va人人爽成人影院 | 欧美成年黄网站色视频 | 在线视频a | 欧美自拍视频 | 久久精品欧美一区二区三区不卡 | 精品欧美一区二区三区久久久小说 | 五月婷婷在线观看视频 | 国产在线高清 | 亚洲 欧美日韩 国产 中文 | 草草浮力影院 | 午夜在线一区 | 国产片一区二区三区 |