Android实现音频条形图效果(仿音频动画无监听音频输入)

Android实现音频条形图效果(仿音频动画无监听音频输入),第1张

概述音频条形图如下图所示就是这次的音频条形图:由于只是自定义View的用法,我们就不去真实地监听音频输入了,随机模拟一些数字即可。

音频条形图

如下图所示就是这次的音频条形图:

由于只是自定义view的用法,我们就不去真实地监听音频输入了,随机模拟一些数字即可。

如果要实现一个如上图的静态音频条形图,相信大家应该可以很快找到思路,也就是绘制一个个的矩形,每个矩形之间稍微偏移一点距离即可。如下代码就展示了一种计算坐标的方法。

for (int i = 0; i < mRectCount; i++) {// 矩形的绘制是从左边开始到上、右、下边(左右边距离左边画布边界的距离,上下边距离上边画布边界的距离)canvas.drawRect((float) (mRectWIDth * i + offset),currentHeight,(float) ( mRectWIDth * (i + 1)),mRectHeight,mRectPaint);}

如上代码中,我们通过循环创建这些小的矩形,其中currentHeight就是每个小矩形的高,通过横坐标的不断偏移,就绘制出了这些静态的小矩形。下面再通过矩形的高度随机变化模拟音频,这里直接利用Math.randoom()方法来随机改变这些高度,并赋值给currentHeight,代码如下所示。

// 由于只是简单的案例就不监听音频输入,随机模拟一些数字即可mRandom = Math.random();currentHeight = (float) (mRectHeight * mRandom);

这样就能实现静态效果了,但是如何实现动态效果呢?其实也是非常简单的,只要在onDraw()方法中再去调用invalIDate()方法通知VIEw进行重绘就可以了。不过这里不需要每次一绘制完新的矩形就通知VIEw进行重绘,这样会因为刷新速度太快反而影响效果。因此,我们可以使用如下代码来进行VIEw的延迟重绘,代码如下:

posInvalIDateDelayed(300);

这样每隔300ms通知VIEw进行重绘,就可以得到一个比较好的视觉效果了。最后添加一个渐变效果可以使VIEw更加逼真,代码如下所示:

@OverrIDeprotected voID onSizeChanged(int w,int h,int olDW,int oldH) {super.onSizeChanged(w,h,olDW,oldH);// 渐变效果linearGradIEnt mlinearGradIEnt;// 画布的宽int mWIDth;// 获取画布的宽mWIDth = getWIDth();// 获取矩形的最大高度mRectHeight = getHeight();// 获取单个矩形的宽度(减去的部分为到右边界的间距)mRectWIDth = (mWIDth-offset) / mRectCount;// 实例化一个线性渐变mlinearGradIEnt = new linearGradIEnt(0,mRectWIDth,topcolor,downcolor,Shader.TileMode.CLAMP);// 添加进画笔的着色器mRectPaint.setShader(mlinearGradIEnt);}

从这个例子中,我们可以知道,在创建自定义view的时候,需要一步一步来,从一个基本的效果开始,慢慢地添加功能,绘制更复杂的效果。不论是多么复杂的自定义view都一定是慢慢迭代起来的功能,所以不要觉得自定义view有多难。千里之行始于足下,只要开始做,慢慢地就能越来越熟练。

代码

以下是这次的完整代码:

package com.example.customaf;import androID.content.Context;import androID.content.res.TypedArray;import androID.graphics.Canvas;import androID.graphics.linearGradIEnt;import androID.graphics.Paint;import androID.graphics.Shader;import androID.support.v4.content.ContextCompat;import androID.util.AttributeSet;import androID.vIEw.VIEw;import com.example.afanalog.R;/*** 自定义的音频模拟条形图* Created by shize on 2016/9/5.*/public class MyAF extends VIEw {// 音频矩形的数量private int mRectCount;// 音频矩形的画笔private Paint mRectPaint;// 渐变颜色的两种private int topcolor,downcolor;// 音频矩形的宽和高private int mRectWIDth,mRectHeight;// 偏移量private int offset;// 频率速度private int mSpeed;public MyAF(Context context) {super(context);}public MyAF(Context context,AttributeSet attrs) {super(context,attrs);setPaint(context,attrs);}public MyAF(Context context,AttributeSet attrs,int defStyleAttr) {super(context,attrs,defStyleAttr);setPaint(context,attrs);}public voID setPaint(Context context,AttributeSet attrs){// 将属性存储到TypedArray中TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.MyAF);mRectPaint = new Paint();// 添加矩形画笔的基础颜色mRectPaint.setcolor(ta.getcolor(R.styleable.MyAF_AFtopcolor,ContextCompat.getcolor(context,R.color.top_color)));// 添加矩形渐变色的上面部分topcolor=ta.getcolor(R.styleable.MyAF_AFtopcolor,R.color.top_color));// 添加矩形渐变色的下面部分downcolor=ta.getcolor(R.styleable.MyAF_AFDowncolor,R.color.down_color));// 设置矩形的数量mRectCount=ta.getInt(R.styleable.MyAF_AFCount,10);// 设置重绘的时间间隔,也就是变化速度mSpeed=ta.getInt(R.styleable.MyAF_AFSpeed,300);// 每个矩形的间隔offset=ta.getInt(R.styleable.MyAF_AFOffset,5);// 回收TypeArrayta.recycle();}@OverrIDeprotected voID onSizeChanged(int w,Shader.TileMode.CLAMP);// 添加进画笔的着色器mRectPaint.setShader(mlinearGradIEnt);}@OverrIDeprotected voID onDraw(Canvas canvas) {super.onDraw(canvas);double mRandom;float currentHeight;for (int i = 0; i < mRectCount; i++) {// 由于只是简单的案例就不监听音频输入,随机模拟一些数字即可mRandom = Math.random();currentHeight = (float) (mRectHeight * mRandom);// 矩形的绘制是从左边开始到上、右、下边(左右边距离左边画布边界的距离,上下边距离上边画布边界的距离)canvas.drawRect((float) (mRectWIDth * i + offset),mRectPaint);}// 使得vIEw延迟重绘postInvalIDateDelayed(mSpeed);}}

布局文件的完整代码:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"xmlns:custom="http://schemas.androID.com/apk/res-auto"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:orIEntation="vertical"tools:context="com.example.afanalog.MainActivity"><com.example.customaf.MyAFandroID:layout_wIDth="match_parent"androID:layout_height="match_parent"custom:AFCount="15"custom:AFDowncolor="@color/down_color"custom:AFSpeed="300"custom:AFtopcolor="@color/top_color"custom:AFOffset="15"/></linearLayout>

以上所述是小编给大家介绍的AndroID实现音频条形图效果(仿音频动画无监听音频输入),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android实现音频条形图效果(仿音频动画无监听音频输入)全部内容,希望文章能够帮你解决Android实现音频条形图效果(仿音频动画无监听音频输入)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1148112.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存