Android Custom View drawing music player oscilloscope

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.