Weekend play a little Hi, did not update the blog, today to make up, this oscilloscope is in the university when the teacher taught, but out of work has not been used, and gradually forget, and now learn again. Look at the effect chart:
Here is a custom histogram, and then there is a button, click the button, where the pillars will keep moving, similar to the music player in the oscilloscope beat.
Similar to the previous custom view, rewrite the Onsizechange () method and the OnDraw () method
Let's make a list of the variables we're going to use.
Paint Mpaint;
Mwidth;
Mrectwidth;
Mrectheight;
Mrectcount =;
offsets =;
LinearGradient mlineargradient;
Mrandom;
All the variables are here.
The following is initialized for the brush
() {
mpaint = Paint ();
Mpaint.setantialias ();
}
Then assign a value to the variable in Onsizechange ()
(W, H, oldw, oldh) {
. onsizechanged (W, H, OLDW, OLDH);
Mwidth = GetWidth ();
Mrectheight = GetHeight ();
Mrectwidth = () (Mwidth * /mrectcount);
Mlineargradient = lineargradient (,, Mrectwidth, Mrectheight,
Color.yellow, Color.Blue, Shader.TileMode.CLAMP);
Mpaint.setshader (mlineargradient);
}
Finally draw the column chart
(Canvas Canvas) {
. OnDraw (canvas);
(i =; i < Mrectcount; i++) {
mrandom = Math.random ();
Currentheight = () (Mrectheight * mrandom);
Canvas.drawrect (
() (Mwidth * / + mrectwidth * i + offsets),
Currentheight, () (Mwidth * / + mrectwidth
* (i +)), mrectheight, mpaint);
}
This time, a music player's oscilloscope has been completed, but this is static, next, exposing a method to the outside, for refreshing view, to achieve dynamic effects.
() {
postinvalidatedelayed ();
}
300ms each interval to redraw the view, you can have a better visual effect.
Okay, finally, I'll put all the code on it:
{Paint mpaint;
Mwidth;
Mrectwidth;
Mrectheight;
Mrectcount =;
offsets =;
LinearGradient mlineargradient;
Mrandom; (Context, AttributeSet attrs, Defstyle)
{(context, attrs, Defstyle);
Initview ();
(Context, AttributeSet attrs) {(context, attrs);
Initview ();
} (context context);
Initview ();
} () {mpaint = Paint ();
Mpaint.setantialias ();
(W, H, OLDW, OLDH) {. onsizechanged (W, H, OLDW, OLDH);
Mwidth = GetWidth ();
Mrectheight = GetHeight ();
Mrectwidth = () (Mwidth */mrectcount); Mlineargradient = LinearGradient (,, Mrectwidth, Mrectheight, Color.yellow, Color.Blue, shader.tilemode.c
LAMP);
Mpaint.setshader (mlineargradient);
} (Canvas Canvas) {. OnDraw (Canvas); (i =; I < MREctcount;
i++) {mrandom = Math.random ();
Currentheight = () (Mrectheight * mrandom); Canvas.drawrect (() (Mwidth */+ mrectwidth * i + offsets), Currentheight, (
Mwidth */+ Mrectwidth * (i +)), mrectheight, mpaint);
} () {postinvalidatedelayed (); }
}
So far, it's all done.