Packagecom.zzw.qunyingzhuan2;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;Importandroid.graphics.LinearGradient;ImportAndroid.graphics.Paint;ImportAndroid.graphics.Shader;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;/*** Created by ZZW on 2016/7/11. * Description:*/ Public classVoicebarviewextendsView {Private intMrectcount = 10; Private intoffset = 20; Private intMwidth; Private intMrectheight; Private intMrectwidth; Privatelineargradient mlineargradient; PrivatePaint paint; PublicVoicebarview (Context context) { This(Context,NULL); } PublicVoicebarview (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); Paint=NewPaint (); Paint.setantialias (true); Paint.setstyle (Paint.Style.FILL); Paint.setcolor (Color.gray); } PublicVoicebarview (Context context, AttributeSet attrs) { This(context, Attrs, 0); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); for(inti = 0; i < Mrectcount; i++) { floatCurrentheight = (float) (Math.random () *mrectheight); floatleft = (float) (Mwidth * 0.2 + mrectwidth * i +offset); floattop =Currentheight; floatright = (float) (Mwidth * 0.2 + mrectwidth * (i + 1)));//float right = left + Mrectwidth; floatBottom =Mrectheight; Canvas.drawrect (left, top, right, bottom, paint); } postinvalidatedelayed (300); } @Overrideprotected voidOnsizechanged (intWintHintOLDW,intOLDH) { Super. Onsizechanged (W, H, OLDW, OLDH); Mwidth=getwidth (); Mrectheight=getheight (); Mrectwidth= (int) (Mwidth * 0.6/mrectcount); Mlineargradient=NewLinearGradient (0, 0, Mrectwidth, Mrectheight, Color.yellow, Color.Blue, Shader.TileMode.CLAMP); Paint.setshader (mlineargradient); }}
Custom Audio Bar Chart--p52