欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义带拼音音调Textview

程序员文章站 2022-04-12 11:45:35
本文实例为大家分享了android自定义带拼音音调textview的具体代码,供大家参考,具体内容如下 1.拼音textview,简单的为把拼音数组和汉字数组结合在一起多...

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

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

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'

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;
  }
}

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