效果图

代码
public class VoiceView extends View {
private int mWidth;
private int mRectHeight;
private int mRectCount = 10;
private int mRectWidth;
private final Paint mPaint;
private LinearGradient mGradient;
private int offset = 5;
private float mCurrentHeight;
private double mRandom;
public VoiceView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.GREEN);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = getWidth();
mRectHeight = getHeight();
mRectWidth = (int) (mWidth * 0.6 / mRectCount);
mGradient = new LinearGradient(0,0,mRectWidth,mRectHeight, Color.YELLOW,Color.GREEN, Shader.TileMode.CLAMP);
mPaint.setShader(mGradient);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < mRectCount; i++) {
mRandom = Math.random();
mCurrentHeight = (float) (mRectHeight * mRandom);
canvas.drawRect((int)(mWidth*0.4/2+mRectWidth*i+offset),mCurrentHeight,
(int)(mWidth*0.4/2+mRectWidth*(i+1)),mRectHeight,mPaint);
}
postInvalidateDelayed(500);
}
}