服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android自定义带拼音音调Textview

Android自定义带拼音音调Textview

2022-09-09 15:10maoamade Android

这篇文章主要介绍了Android自定义带拼音音调的Textview,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android自定义带拼音音调Textview的具体代码,供大家参考,具体内容如下

1.拼音textview,简单的为把拼音数组和汉字数组结合在一起多行显示

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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);
    //设置字体大小
    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;
  }
 
  //设置文字资源
  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);
  }
 
  //设置文字颜色
  public void setStringColor(int spellColor,int chineseColor) {
    textPaintSpell.setColor(spellColor);
    textPaintChinese.setColor(chineseColor);
  }
 
  //设置文字大小
  public void setFontSize(float spellFontSize,float chineseFontSize) {
    textPaintSpell.setTextSize(ConvertUtils.dp2px(spellFontSize));
    textPaintChinese.setTextSize(ConvertUtils.dp2px(chineseFontSize));
  }
}

2.汉字转拼音使用 implementation ‘com.belerweb:pinyin4j:2.5.0'

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_35372662/article/details/85547048

延伸 · 阅读

精彩推荐