1、所谓无图无真相,先上效果图。我们要实现的就是中间那个录音的按钮,周边会显示一圈音量大小的波形

2、VolumCircleBar继承自View,我们进行了自定义,代码如下

package com.rdinfo.ccenglish.ui.ccprofile.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View; import com.panshi.xuexiao.R;
/**
* 麦克风音量圆形按钮
* @author hiphonezhu@gmail.com
* @version [CCEnglish, 2014-7-25]
*/
public class VolumCircleBar extends View
{
private double volumRate; // 音量百分比
private boolean isRecording; // 录音标志
private Object lock = new Object();
private Thread uiThread;
private Paint mPaint;
private RectF arcRect;
private Matrix matrix = new Matrix();
private final int VOLUM_INDICATE_LENGTH = ; // 音量大小线长度
private final int CIRCLE_INNER_DISTANCE_TO_OUTSIDE = ; // 内切圆距离外圆的距离
public VolumCircleBar(Context context)
{
this(context, null);
} public VolumCircleBar(Context context, AttributeSet attrs)
{
this(context, attrs, );
} public VolumCircleBar(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.VolumCircleBar, defStyle, );
init(typedArray);
} private int recordingColor; // 录音背景色
private int stoppedColor; // 停止背景色
private Bitmap centerRes; // 中间麦克风图片
private int totalBlockCount; // 块数量
private int spliteAngle; // 块之间的间隔角度大小
private int circleWidth; // 直径
/**
* 初始化
*/
private void init(TypedArray typedArray)
{
for (int i = ; i < typedArray.length(); i++)
{
int attr = typedArray.getIndex(i);
switch (attr)
{
case R.styleable.VolumCircleBar_recordingColor:
recordingColor = typedArray.getColor(i, Color.GREEN);
break;
case R.styleable.VolumCircleBar_stoppedColor:
stoppedColor = typedArray.getColor(i, Color.GRAY);
break;
case R.styleable.VolumCircleBar_centerRes:
centerRes = BitmapFactory.decodeResource(getContext().getResources(), typedArray.getResourceId(i, R.drawable.ic_launcher));
break;
case R.styleable.VolumCircleBar_blockCount:
totalBlockCount = typedArray.getInt(i, );
break;
case R.styleable.VolumCircleBar_splitAngle:
spliteAngle = typedArray.getInt(i, );
break;
}
}
typedArray.recycle();
uiThread = Thread.currentThread();
mPaint = new Paint();
if (spliteAngle * totalBlockCount > )
{
throw new IllegalArgumentException("spliteAngle * blockCount > 360, while the result should be less than 360.");
} // debug for test
isRecording = true;
volumRate = 0.5;
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// 直径
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
circleWidth = width > height? width : height;
if (arcRect == null)
{
arcRect = new RectF(CIRCLE_INNER_DISTANCE_TO_OUTSIDE, CIRCLE_INNER_DISTANCE_TO_OUTSIDE,
circleWidth - CIRCLE_INNER_DISTANCE_TO_OUTSIDE, circleWidth - CIRCLE_INNER_DISTANCE_TO_OUTSIDE); // 音量显示区域, 内偏移几个像素
// 图片处理矩阵
initBitmapMatrix();
}
// 强制设置view大小
setMeasuredDimension(circleWidth, circleWidth);
} /**
* 中间图片压缩处理
*/
private void initBitmapMatrix()
{
float innerCircleRadius = (circleWidth - * (VOLUM_INDICATE_LENGTH + CIRCLE_INNER_DISTANCE_TO_OUTSIDE)) / 2f; // 内圆的半径
float innerRectangleWidth = (float)Math.cos((Math.PI / ) * ) * innerCircleRadius * ; // 内圆的内切正方形的边长
float translateOffset = VOLUM_INDICATE_LENGTH + CIRCLE_INNER_DISTANCE_TO_OUTSIDE + innerCircleRadius - innerRectangleWidth / ; // 偏移的offset
if (centerRes.getWidth() > (innerRectangleWidth) || centerRes.getHeight() > (innerRectangleWidth))
{
// 图片宽度或高度大于(直径-内偏移), 等比压缩
if (centerRes.getWidth() > centerRes.getHeight())
{
// 按照宽度压缩
float ratio = innerRectangleWidth / centerRes.getWidth();
matrix.postScale(ratio, ratio);
float translateY = (innerRectangleWidth - (centerRes.getHeight() * ratio)) / 2f;
// 在纵坐标方向上进行偏移,以保证图片居中显示
matrix.postTranslate(translateOffset, translateY + translateOffset);
}
else
{
// 按照高度压缩
float ratio = innerRectangleWidth / (centerRes.getHeight() * 1.0f);
matrix.postScale(ratio, ratio);
float translateX = (innerRectangleWidth - (centerRes.getWidth() * ratio)) / 2f;
// 在横坐标方向上进行偏移,以保证图片居中显示
matrix.postTranslate(translateX + translateOffset, translateOffset);
}
}
else
{
// 当图片的宽高都小于屏幕宽高时,直接让图片居中显示
float translateX = (innerRectangleWidth - centerRes.getWidth()) / 2f;
float translateY = (innerRectangleWidth - centerRes.getHeight()) / 2f;
matrix.postTranslate(translateX + translateOffset, translateY + translateOffset);
}
} /**
* 设置音量百分比
* @param rate
*/
public void updateVolumRate(double rate)
{
synchronized (lock)
{
this.volumRate = rate;
if (Thread.currentThread() != uiThread)
{
postInvalidate();
}
else
{
invalidate();
}
}
} /**
* 开始、停止录音
*/
public void toggleRecord()
{
synchronized (lock)
{
isRecording = !isRecording;
if (Thread.currentThread() != uiThread)
{
postInvalidate();
}
else
{
invalidate();
}
}
} @Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawColor(Color.TRANSPARENT);
synchronized (lock)
{
if (isRecording) // 正在录音
{
//1.绘制绿色圆圈
mPaint.setAntiAlias(true); //消除锯齿
mPaint.setColor(recordingColor);
mPaint.setStrokeWidth();
mPaint.setStyle(Paint.Style.FILL); // 填充
canvas.drawCircle(circleWidth / 2f, circleWidth / 2f, circleWidth / 2f, mPaint);
//2.根据音量百分比、块数量、块间隔大小计算角度动态绘制音量大小
// 计算块的角度
float blockAngle = ( * 1.0f - spliteAngle * totalBlockCount) / totalBlockCount;
int drawBlockCount = (int)(totalBlockCount * volumRate); // 绘制的block数量
mPaint.setStrokeWidth(VOLUM_INDICATE_LENGTH);
mPaint.setColor(stoppedColor);
mPaint.setStyle(Paint.Style.STROKE); // 空心
for (int i = ; i < drawBlockCount; i++)
{
canvas.drawArc(arcRect, i * (blockAngle + spliteAngle) - , blockAngle, false, mPaint);
}
}
else // 录音停止
{
//1.绘制灰色圆圈
mPaint.setColor(stoppedColor);
mPaint.setStrokeWidth();
mPaint.setStyle(Paint.Style.FILL); // 填充
canvas.drawCircle(circleWidth / 2f, circleWidth / 2f, circleWidth / 2f, mPaint);
}
}
// 绘制中间话筒
canvas.drawBitmap(centerRes, matrix, null);
}
}

Android 麦克风录音带音量大小动态显示的圆形自定义View的更多相关文章

  1. Android零基础入门第24节:自定义View简单使用

    原文:Android零基础入门第24节:自定义View简单使用 当我们开发中遇到Android原生的组件无法满足需求时,这时候就应该自定义View来满足这些特殊的组件需求. 一.概述 很多初入Andr ...

  2. Android UI 绘制过程浅析(五)自定义View

    前言 这已经是Android UI 绘制过程浅析系列文章的第五篇了,不出意外的话也是最后一篇.再次声明一下,这一系列文章,是我在拜读了csdn大牛郭霖的博客文章<带你一步步深入了解View> ...

  3. Android简易实战教程--第二十七话《自定义View入门案例之开关按钮详细分析》

    转载此博客请注明出处点击打开链接       http://blog.csdn.net/qq_32059827/article/details/52444145 对于自定义view,可能是一个比较大的 ...

  4. [转]Android自定义控件三部曲系列完全解析(动画, 绘图, 自定义View)

    来源:http://blog.csdn.net/harvic880925/article/details/50995268 一.自定义控件三部曲之动画篇 1.<自定义控件三部曲之动画篇(一)—— ...

  5. Android开发之漫漫长途 番外篇——自定义View的各种姿势1

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  6. Android开发之漫漫长途 番外篇——自定义View的各种姿势2

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  7. Android 自定义View修炼-Android开发之自定义View开发及实例详解

    在开发Android应用的过程中,难免需要自定义View,其实自定义View不难,只要了解原理,实现起来就没有那么难. 其主要原理就是继承View,重写构造方法.onDraw,(onMeasure)等 ...

  8. Android零基础入门第52节:自定义酷炫进度条

    原文:Android零基础入门第52节:自定义酷炫进度条 Android系统默认的ProgressBar往往都不能满足实际开发需要,一般都会开发者自定义ProgressBar. 在Android开发中 ...

  9. Android零基础入门第40节:自定义ArrayAdapter

    原文:Android零基础入门第40节:自定义ArrayAdapter ListView用起来还是比较简单的,也是Android应用程序中最重要的一个组件,但其他ListView可以随你所愿,能够完成 ...

随机推荐

  1. vue-cli创建项目 一直downloading解决办法

    vue-cli创建项目: 1 安装node(需要npm,npm是node的包管理股工具) 2 安装cnpm 3 安装vue-cli       cnpm install vue-cli -g 4 创建 ...

  2. CS231n 2016 通关 第五、六章 Batch Normalization 作业

    BN层在实际中应用广泛. 上一次总结了使得训练变得简单的方法,比如SGD+momentum RMSProp Adam,BN是另外的方法. cell 1 依旧是初始化设置 cell 2 读取cifar- ...

  3. HTML5.与JQUERY与AJAX常见面试题

    1. HTML5 1.1.简要描述 HTML5中的本地存储 参考答案: 很多时候我们会存储用户本地信息到电脑上,例如:比方说用户有一个填充了一半的长表格,然后突然网络连接断开了,这样用户希望你能存储这 ...

  4. In-App Purchase Programming Guide----(一) ---- About In-App Purchase

    About In-App Purchase In-App Purchase allows you to embed a store inside your app using the Store Ki ...

  5. g2o待总结

    http://blog.csdn.net/u010566411/article/details/53862601

  6. 「一入 Java 深似海 」系列课程 - 第一期

    10分30秒开始 小马哥技术博客: https://mercyblitz.github.io/ github地址: https://github.com/mercyblitz 33分28 https: ...

  7. Subresource Integrity(子资源一致性)和JS DDos 攻击

    以下文章转载自 http://www.cnblogs.com/zoucaitou/p/4505483.html 和 http://www.puronglong.com/blog//2015/04/12 ...

  8. roguelike地牢生成算法

    文章原地址 上一个地图生成算法,这一次是一个地牢的生成算法,是一个国外的人写的算法,用dart语言写,我把它改成了unity-c#. 原作者博客地址:Rooms and Mazes: A Proced ...

  9. 【WIP_S9】图论算法

    创建: 2018/06/01 图的概念 有向边 有向图 无向边 无向图 点的次数: 点连接的边的数量 闭路: 起点和重点一样 连接图: 任意两点之间都可到达 无闭路有向图: 没有闭路的有向图 森林: ...

  10. 利用YYLabel 进行图文混排+高度计算

    利用YYLabel 进行图文混排+高度计算 1.项目需求: 用一个控件显示图片和文字,并且依据图片和文字动态计算控件的高度. 2.方案: 利用YYLabel控件和属性字符串处理. 注:(在使用YYLa ...