這篇文章主要為大家展示了“Android中語(yǔ)音聲波控件以及條形波控件怎么用”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Android中語(yǔ)音聲波控件以及條形波控件怎么用”這篇文章吧。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序制作、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了涵江免費(fèi)建站歡迎大家使用!
SoundWavesView
/**
* 語(yǔ)音通話的聲波控件
* Created by Mr.LongFace on 2017/9/16.
*/
public class SoundWavesView extends View {
private int mMini; // 最短值
private int mMax; // 最大值
private int mLineWidth; // 每條聲波的寬度
private int mSoundNum = 5; // 聲波的數(shù)量
private int mSpac; // 每條聲波的中點(diǎn)
private int mWidth , mHeight; // 控件寬高
private boolean isRun = false;
private Paint mPaint;
private RectF mRectF;
private List<SoundLine> mSoundList = new ArrayList<>();
private Handler mHandler = new Handler();
private Runnable mInvalidateRun = new Runnable() {
@Override
public void run() {
postInvalidate();
}
};
public SoundWavesView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(getResources().getColor(R.color.color_red));
mPaint.setStyle(Paint.Style.FILL);
mRectF = new RectF();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (widthMeasureSpec > 0 && heightMeasureSpec > 0) {
initParam();
}
}
private void initParam() {
mWidth = getWidth();
mHeight = getHeight();
mMini = (int) (mHeight * 0.3f);
mMax = mHeight;
initLines();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < mSoundNum; i++) {
SoundLine sound = mSoundList.get(i);
mRectF.left = sound.left;
mRectF.right = sound.right;
mRectF.top = sound.top;
mRectF.bottom = sound.bottom;
canvas.drawRoundRect(mRectF , mLineWidth / 2 , mLineWidth / 2 , mPaint);
}
if (isRun) {
mHandler.postDelayed(mInvalidateRun, 10);
}
}
@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (isRun) {
if (visibility == VISIBLE) {
if (mWidth == 0) {
initParam();
}
if (mSoundList != null && mSoundList.size() > 0) {
for (SoundLine soundLine : mSoundList) {
soundLine.start();
}
}
}else{
if (mSoundList != null && mSoundList.size() > 0) {
for (SoundLine soundLine : mSoundList) {
soundLine.stop();
}
}
}
}
}
public void start() {
if (!isRun) {
isRun = true;
for (SoundLine sound : mSoundList) {
sound.start();
}
postInvalidate();
}
}
public void stop(){
if (isRun) {
isRun = false;
for (SoundLine sound : mSoundList) {
sound.stop();
}
}
}
private void initLines() {
mLineWidth = (int) (mWidth / mSoundNum * 0.7f);
mSpac = mWidth / (mSoundNum - 1);
mSoundList.clear();
chaos();
}
/**
* 生成凌亂的
*/
private void chaos() {
for (int i = 0; i < mSoundNum; i++) {
int left = i * mSpac - mLineWidth / 2;
int right = i * mSpac + mLineWidth / 2;
SoundLine s = new SoundLine(left , right , 0 , mHeight);
s.setMode(SoundLine.SPEED_RAN);
s.setBorder(mMini , mMax);
mSoundList.add(s);
}
}
/**
* 生成波浪的
*/
private void wave(){
// TODO 防止UI抽風(fēng)
}
/**
* 生成有序的
*/
private void order(){
// TODO 防止UI抽風(fēng)
}
}SoundLine
/**
* 語(yǔ)音音頻波紋的單個(gè)音波屬性
* Created by Mr.LongFace on 2017/9/16.
*/
public class SoundLine implements ValueAnimator.AnimatorUpdateListener{
// 低 中 高 隨機(jī) 4擋
public static final int SPEED_LOW = 500;
public static final int SPEED_MID = 200;
public static final int SPEED_HEI = 0;
public static final int SPEED_RAN = 0;
private Random mRandom;
private ValueAnimator mAnim;
public int left , right , top , bottom;
private int min , max;
public SoundLine(int left , int right , int top , int bottom){
this.left = left;
this.right = right;
this.top = top;
this.bottom = bottom;
mRandom = new Random();
initAnim();
}
private void initAnim() {
mAnim = ValueAnimator.ofFloat(0.0f , 1.0f);
setMode(SPEED_MID);
mAnim.setRepeatCount(-1);
mAnim.setRepeatMode(ValueAnimator.REVERSE);
mAnim.addUpdateListener(this);
}
public void setMode(int mode){
if (mode == SPEED_RAN) {
mode = mRandom.nextInt(400);
}
mAnim.setDuration(300 + mode);
}
public void start(){
if (mAnim.isRunning()){
mAnim.end();
}
mAnim.start();
}
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float f = (float) valueAnimator.getAnimatedValue();
top = (int) (f * (max - min) / 2);
bottom = max - top;
}
public void setBorder(int min, int max) {
this.min = min;
this.max = max;
}
public void stop() {
mAnim.end();
mAnim.cancel();
}
}以上是“Android中語(yǔ)音聲波控件以及條形波控件怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享題目:Android中語(yǔ)音聲波控件以及條形波控件怎么用
分享網(wǎng)址:http://www.chinadenli.net/article8/peesip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站建設(shè)、做網(wǎng)站、虛擬主機(jī)、自適應(yīng)網(wǎng)站、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)