
可以输入百分比来控制麦克风中黄色进度条的长度。这样,如果是声音,可以先将分贝转为百分比。
调用方法 setVolumePercent(double volumePercent)来设置百分比。
如下代码:
package com.lenovo.logistics.view.iflytek;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.MeasureSpec;
public class SpeechingWidget extends View{
private int selfWidth;
private int selfHeight;
private int border = 4;
private Paint paint = new Paint();
private int lineColor = 0xFFFFFFFF;
private int margin01 = 10;//tuo与外圈的间距
private int margin02 = 10;//内圈与外圈的间距
private double volumePercent = 0;//音量大小的百分率
public SpeechingWidget(Context context) {
super(context);
}
public SpeechingWidget(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SpeechingWidget(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.selfWidth = MeasureSpec.getSize(widthMeasureSpec);
this.selfHeight = MeasureSpec.getSize(heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int startBottom = this.selfHeight;
startBottom = drawDizuo(canvas, startBottom);
startBottom = drawDizhu(canvas, startBottom);
startBottom = drawTuo(canvas, startBottom);
startBottom = drawWaiquan(canvas, startBottom);
drawNeiquan(canvas, startBottom);
}
/**
* 画底座
*/
private int drawDizuo(Canvas canvas, int startBottom){
paint.setStyle(Paint.Style.FILL);
paint.setColor(lineColor);
int width = this.selfWidth/5*3;
int top = startBottom - border;
int left = this.selfWidth /2 - width/2;
int right = left + width;
RectF rectF = new RectF(left, top, right , startBottom);
canvas.drawRect(rectF, paint);
return top;
}
/**
* 画底柱
*/
private int drawDizhu(Canvas canvas, int startBottom){
paint.setStyle(Paint.Style.FILL);
paint.setColor(lineColor);
int height = this.selfHeight /10;
int top = startBottom - height;
int left = this.selfWidth/2 - border/2;
RectF rectF = new RectF(left, top, left+border , startBottom);
canvas.drawRect(rectF, paint);
return top;
}
/**
* 画麦克风的托
*/
private int drawTuo(Canvas canvas, int startBottom){
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth((float) border);
paint.setAntiAlias(true);
paint.setColor(lineColor);
int left = 0;
int top = startBottom - this.selfWidth;
int right = this.selfWidth;
int bottom = startBottom;
RectF oval=new RectF();
oval.left = left + border/2;
oval.top = top + border/2;
oval.right = right - border/2;
oval.bottom = bottom - border/2;
canvas.drawArc(oval, 0, 180, false, paint);
return startBottom-border-margin01;
}
/**
* 画外圈
*/
private int drawWaiquan(Canvas canvas, int startBottom){
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth((float) border);
paint.setAntiAlias(true);
paint.setColor(lineColor);
int width = this.selfWidth - border*2 - margin01*2;
//画下半部分的圆角
int left = this.selfWidth/2 - width/2;
int top = startBottom - width;
int rectBottom = startBottom - width/2;
int right = left + width;
int bottom = startBottom;
RectF oval=new RectF();
oval.left = left + border/2;
oval.top = top + border/2;
oval.right = right - border/2;
oval.bottom = bottom - border/2;
canvas.drawArc(oval, 0, 180, false, paint);
//画上半部分圆角
top = 0;
bottom = top + width;
oval = new RectF();
oval.left = left + border/2;
oval.top = top + border/2;
oval.right = right - border/2;
oval.bottom = bottom - border/2;
canvas.drawArc(oval, 180, 180, false, paint);
//画矩形
paint.setStyle(Paint.Style.FILL);
top = width/2;
bottom = rectBottom;
RectF rectF = new RectF(left, top, left+border , bottom);
canvas.drawRect(rectF, paint);
rectF = new RectF(right-border, top, right , bottom);
canvas.drawRect(rectF, paint);
return startBottom - border - margin02;
}
/**
* 画内圈
*/
private void drawNeiquan(Canvas canvas, int startBottom){
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setColor(0xFFFFA500);
paint.setStrokeWidth((float) border);
int width = this.selfWidth - border*2 - margin01*2 - margin02*2;
int radius = width/2;
int height = startBottom - border - margin02;
//没有音量,直接返回
if(this.volumePercent == 0)
return ;
double percent =radius*1.0/height * 100;
if(percent == 0)
return ;
if(this.volumePercent<=percent){
//画下半部分的圆角
int left = this.selfWidth/2 - width/2;
int top = startBottom - width;
int topTemp = top + width/2;
int right = left + width;
int bottom = startBottom;
RectF oval=new RectF();
oval.left = left;
oval.top = top;
oval.right = right;
oval.bottom = bottom;
int per = (int)(90*1.0/percent);
int angle = (int)(this.volumePercent * per);
angle = 90-angle;
canvas.drawArc(oval, angle, 180-angle*2, false, paint);
return ;
}
double rectPercent =(height - width)*1.0/height * 100;
if(this.volumePercent<=percent+rectPercent){
//画下半部分的圆角
int left = this.selfWidth/2 - width/2;
int top = startBottom - width;
int topTemp = startBottom - width/2;
int right = left + width;
int bottom = startBottom;
RectF oval=new RectF();
oval.left = left;
oval.top = top;
oval.right = right;
oval.bottom = bottom;
canvas.drawArc(oval, 0, 180, false, paint);
//画矩形
bottom = topTemp;
int addHeight = (int)(((this.volumePercent-percent)*1.0/rectPercent)*(height-width));
RectF rectF = new RectF(left, bottom-addHeight, right , bottom);
canvas.drawRect(rectF, paint);
return ;
}
if(this.volumePercent>percent+rectPercent){
//画下半部分的圆角
int left = this.selfWidth/2 - width/2;
int top = startBottom - width;
int right = left + width;
int bottom = startBottom;
RectF oval=new RectF();
oval.left = left;
oval.top = top;
oval.right = right;
oval.bottom = bottom;
canvas.drawArc(oval, 0, 180, false, paint);
int rectBottom = bottom - width/2;
//画上半部分圆角
top = border+margin02;
bottom = top + width;
oval = new RectF();
oval.left = left;
oval.top = top;
oval.right = right;
oval.bottom = bottom;
//canvas.drawArc(oval, -90, 180, false, paint);
double per = 90*1.0/percent;
int angle = (int)((this.volumePercent-percent-rectPercent) * per);
canvas.drawArc(oval, 0-angle, 180+angle*2, false, paint);
int rectTop = top + width/2;
//画矩形
paint.setColor(0xFFFFA500);
RectF rectF = new RectF(left, rectTop, right , rectBottom);
canvas.drawRect(rectF, paint);
return ;
}
}
public void setVolumePercent(double volumePercent) {
this.volumePercent = volumePercent;
this.invalidate();
}
}