Android自定义带拼音音调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;
  }
}

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

(0)

相关推荐

  • Android TextView设置背景色与边框的方法详解

    1.在drawable文件夹下面创建setbar_bg.xml 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- 背景色 -->    <solid android:color="#FFE4B5&q

  • android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法

    布局文件中的TextView属性 复制代码 代码如下: <TextViewandroid:id="@+id/businesscardsingle_content_abstract"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:lineSpacingMu

  • Android TextView字体颜色设置方法小结

    本文实例总结了Android TextView字体颜色设置方法.分享给大家供大家参考,具体如下: 对于setTextView(int a)这里的a是传进去颜色的值.例如,红色0xff0000是指0xff0000如何直接传入R.color.red是没有办法设置颜色的,只有通过文章中的第三种方法先拿到资源的颜色值再传进去. tv.setTextColor(this.getResources().getColor(R.color.red)); 关键字: android textview color T

  • android TextView设置中文字体加粗实现方法

    英文设置加粗可以在xml里面设置: 复制代码 代码如下: <SPAN style="FONT-SIZE: 18px">android:textStyle="bold"</SPAN> 英文还可以直接在String文件里面直接这样填写: 复制代码 代码如下: <string name="styled_text">Plain, <b>bold</b>, <i>italic</

  • Android编程开发之TextView文字显示和修改方法(附TextView属性介绍)

    本文实例讲述了Android编程开发之TextView文字显示和修改方法.分享给大家供大家参考,具体如下: 一. 新建一个Activity 和 Layout 首先在layout文件夹中新建一个activity_main.xml,在新建工程的时候一般默认会新建此xml文件,修改其代码如下: activity_main.xml 代码 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x

  • Android自定义带拼音音调Textview

    本文实例为大家分享了Android自定义带拼音音调Textview的具体代码,供大家参考,具体内容如下 1.拼音textview,简单的为把拼音数组和汉字数组结合在一起多行显示 import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Pa

  • Android自定义带增长动画和点击弹窗提示效果的柱状图DEMO

    项目中最近用到各种图表,本来打算用第三方的,例如MPAndroid,这是一个十分强大的图表库,应用起来十分方便,但是最终发现和设计不太一样,没办法,只能自己写了.今天将写好的柱状图的demo贴在这,该柱状图可根据数据的功能有一下几点: 1. 根据数据的多少,动态的绘制柱状图柱子的条数: 2. 柱状图每条柱子的绘制都有动态的动画效果: 3. 每条柱子有点击事件,点击时弹出提示框,显示相关信息,规定时间后,弹窗自动消失. 好了,先上演示图: 下边贴出相关代码: 自定义柱状图类: package co

  • Android自定义带水滴的进度条样式(带渐变色效果)

    一.直接看效果 二.直接上代码 1.自定义控件部分 package com.susan.project.myapplication; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.grap

  • Android自定义带进度条WebView仿微信加载过程

    在正常开发中,我们客户端需要用webView加载网页,再遇到网络慢或者访问的服务器响应时,页面是空白的,所以为了用户更好的体验,我们可以提供一个正在加载的进度条,提示用户正在加载. 本文结构: 1.自定义webView 2.在应用中的使用 3.效果展示 一.自定义webView 1.首先定义一个类,继承webView,并首先构造方法 public class ProgressBarWebView extends WebView{} 自定义控件,先实现构造方法, 第一中是程序内部实例化采用,传入c

  • Android自定义带圆点的半圆形进度条

    本文实例为大家分享了Android自定义带圆点的半圆形进度条,供大家参考,具体内容如下 仅限用于半圆形,如须要带圆点的圆形进度条,圆点会出现错位现象,此代码仅供,带圆点的圆形进度条有空研究一下!图片效果在下方, import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import and

  • Android自定义带动画效果的圆形ProgressBar

    本文实例为大家分享了Android自定义带动画效果的圆形ProgressBar,供大家参考,具体内容如下 最近有个需求显示进度,尾部还要有一标示,像下边这样 使用自定义View的方式实现,代码如下,很简单注释的很清楚 文章最后我们拓展一下功能,实现一个带动画效果的进度条 package com.example.fwc.allexample.progressbar; import android.animation.ValueAnimator; import android.annotation.

  • android自定义带箭头对话框

    本文实例为大家分享了android自定义带箭头对话框的具体代码,供大家参考,具体内容如下 import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; import android.support.annotation.Nullabl

  • Android自定义带加载动画效果的环状进度条

    最近闲来无事,自定义了一个环状进度条,话不多说直接上代码 : public class CircleProgressView extends View{ private Paint mCirPaint; private Paint mArcPaint; private Paint mTextPaint; private float radius=200; private int textsize=60; private int progress=68; private int stokeWidt

  • Android自定义带圆角的ImageView

    最近有一个实现一个带有圆角的ImageView的需求,在网上找了找三方,虽然Demo都是正确的,但是移植过来就不可以了,因为请求链接的时候用的是xUtils中Bitmap来进行解析的,这样就总是会报类型转换异常的错误. 就这样只能自己定义一个了. Demo: package com.yizooo.yizooo.ui; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bi

  • Android自定义View之继承TextView绘制背景

    本文实例为大家分享了TextView绘制背景的方法,供大家参考,具体内容如下 效果: 实现流程: 1.初始化:对画笔进行设置 mPaintIn = new Paint(); mPaintIn.setAntiAlias(true); mPaintIn.setDither(true); mPaintIn.setStyle(Paint.Style.FILL); mPaintIn.setColor(getResources().getColor(R.color.colorPrimary)); mPain

随机推荐