Android自定義語音音訊對話識別翻譯動畫控制元件
阿新 • • 發佈:2019-01-01
最近做翻譯器專案,專案中用到科大語音翻譯,語音動畫需要自己寫,對於我稍微有點複雜,把程式碼貼出來供大家參考下,不足之處請指正!
自定義控制元件包含有語音動畫(音量大小波浪動畫),音柱(音量大小音柱改變高低),文字(請講話、識別中、翻譯中)。
/**
* 自定義帶音量大小動畫的控制元件
*/
public class SoundView3 extends View {
//畫筆
private Paint mPaint;
private Paint circlrPaint;
/**
* 聲音音柱高度
*/
private float[] voiceLength = new float[]{0.8f, 1.3f, 0.9f, 1.6f, 1, 2, 1};
/**
* 顏色值
*/
private int[] colorS = new int[]{0xff2EBEB0, 0xff28B3DB, 0xff25A1F0, 0xff2695F0,
0xff25A1F0, 0xff26ADF0, 0xff2EBEB0};
private int rect_width = 6; //矩形的寬度
private int rect_distance; //矩形之間的間距
private float radius; //圓角
private float widthRect; //矩形佔據總寬度7*8
private float distanceRect; //間隔間距的總尺寸6*12
private float drawWidth; //繪製的區域寬度尺寸 寬+間距,抖動部分總長度
private int soundNum = 1;
private int rect_height_defaule;
private int textSize = 18;
private String text_desc = "請講話";
private boolean isRun = true;
private MyThread myThread;
private int current_point = 0;
private boolean isTranslate = false;
public SoundView3(Context context) {
this(context, null);
}
public SoundView3(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public SoundView3(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
/**
* 初始化畫筆,計算繪製尺寸
*/
private void initView() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setDither(true);// 設定抖動,顏色過渡更均勻
circlrPaint = new Paint();
circlrPaint.setAntiAlias(true);
circlrPaint.setStrokeCap(Paint.Cap.ROUND);
circlrPaint.setDither(true);// 設定抖動,顏色過渡更均勻
rect_distance = rect_width * 2; //矩形之間的間距
radius = rect_width / 2; //圓角banjing
widthRect = (voiceLength.length) * rect_width; //矩形佔據總寬度7*8
distanceRect = (voiceLength.length - 1) * rect_distance; //間隔間距的總尺寸6*12
drawWidth = widthRect + distanceRect; //繪製的區域寬度尺寸 寬+間距
rect_height_defaule = rect_width * 2;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (myThread == null) {
myThread = new MyThread();
myThread.start();
} else {
drawCircle(canvas);//畫圓形波浪
if (isTranslate) {
drawWaitState(canvas);
} else {
drawVoice(canvas);//畫圓中間的抖動音
}
}
}
/**
* 開啟一個子執行緒繪製ui
*/
private class MyThread extends Thread {
@Override
public void run() {
while (isRun) {
logic();
postInvalidate();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void logic() {
current_point++;
if (current_point > voiceLength.length) {
current_point = 0;
}
}
/***
* 外部呼叫 音量大小值
* @param soundNum
*/
public void setVoiceNum(int soundNum) {
isTranslate = false;
this.soundNum = soundNum;
invalidate();
}
/**
* 是否是翻譯狀態
*/
public void transLateState() {
isTranslate = true;
}
/**
* 繪製等待狀態
*
* @param canvas
*/
private void drawWaitState(Canvas canvas) {
int height = getHeight();
float transSize = getWidth() / 2 - (drawWidth / 2);
canvas.translate(transSize, 0);
canvas.save();
mPaint.setStyle(Paint.Style.FILL);
for (int i = 0; i < voiceLength.length; i++) {
float height_up = (height / 2) - (rect_height_defaule / 2);
float height_down = (height / 2) + (rect_height_defaule / 2);
RectF oval3 = new RectF(0, height_up, rect_width, height_down);// 設定個新的長方形
if (current_point == i) {
mPaint.setColor(colorS[colorS.length - 1]);
} else {
mPaint.setColor(Color.GRAY);
}
canvas.drawRoundRect(oval3, radius, radius, mPaint);//第二個引數是x半徑,第三個引數是y半徑
canvas.translate((rect_distance + rect_width), 0);
}
canvas.restore();
}
/**
* 繪製聲音波浪圓
*
* @param canvas
*/
private void drawCircle(Canvas canvas) {
int rectWidth = (voiceLength.length * rect_distance)
+ (voiceLength.length * rect_width); //計算圓的直徑=7個間距+7個寬度
Log.e("SoundView", "rect_width:" + rect_width);
circlrPaint.setColor(0x992695F0);
//語音外圓 藍色
canvas.drawCircle(getWidth() / 2, getHeight() / 2, (rectWidth * 1 / 2) + (soundNum * 2), circlrPaint); //繪製外圈
//語言內圓 白色
circlrPaint.setColor(Color.WHITE);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, (rectWidth * 1 / 2), circlrPaint); //繪製內圈
//繪製文字
circlrPaint.setColor(0xff2695F0);
circlrPaint.setTextSize(textSize);
Rect bounds = new Rect();
circlrPaint.getTextBounds(text_desc, 0, text_desc.length(), bounds);//獲取字型尺寸同measureText函式
canvas.drawText(text_desc, (getWidth() / 2) - (bounds.width() / 2), (getHeight() / 2 - rectWidth / 2 + (bounds.height() * 2)), circlrPaint);
}
/**
* 改變text值
*
* @param text_desc
*/
public void setTextView(String text_desc) {
this.text_desc = text_desc;
}
/***
* 繪製抖動聲音
* @param canvas
*/
private void drawVoice(Canvas canvas) {
float height_up = 0;
float height_down = 0;
int height = getHeight();
float transSize = getWidth() / 2 - (drawWidth / 2);
canvas.translate(transSize, 0);//平移到原點
canvas.save();
mPaint.setStyle(Paint.Style.FILL);
for (int i = 0; i < voiceLength.length; i++) {
mPaint.setColor(colorS[i]);
height_up = (height / 2) - (voiceLength[i] * soundNum);
height_down = (height / 2) + (voiceLength[i] * soundNum);
RectF oval3 = new RectF(0, height_up, rect_width, height_down);// 設定個新的長方形
canvas.drawRoundRect(oval3, radius, radius, mPaint);//第二個引數是x半徑,第三個引數是y半徑
canvas.translate((rect_distance + rect_width), 0);//每次畫完一個平移rect_distance + rect_width
}
canvas.restore();
}
}
圖示