Android Draw music player Oscilloscope _android

Source: Internet
Author: User

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:

 /** Brush * *
 private Paint mpaint;
 /** Control Width * *
 private float mwidth;
 /** the width of a single pillar * *
 private float mrectwidth;
 /** Single Column Height * *
 private float mrectheight;
 The total number of/** pillars * *
 private float mrectcount = ten;
 /** column between the interval * *
 private int offsets = 2;
 /**android in the linear gradient
 /private lineargradient mlineargradient;
 /** Random Column Height *
 /private double mrandom;

All the variables are here.

The following is initialized for the brush

 /**
  * Initialization brush/
 private void Initview () {
  mpaint = new Paint ();
  Mpaint.setantialias (true);
 

Then assign a value to the variable in Onsizechange ()

 @Override
 protected void onsizechanged (int w, int h, int oldw, int oldh) {
  super.onsizechanged (W, H, OLDW, OLDH); C3/>mwidth = GetWidth ();
  Mrectheight = GetHeight ();
  Mrectwidth = (int) (mwidth * 0.6/mrectcount);
  Mlineargradient = new LinearGradient (0, 0, mrectwidth, mrectheight,
    Color.yellow, Color.Blue, Shader.TileMode.CLAMP);
  Mpaint.setshader (mlineargradient);

 }

Finally draw the column chart

 @Override
 protected void OnDraw (Canvas Canvas) {
  super.ondraw (Canvas);
  for (int i = 0; i < Mrectcount i++) {
   mrandom = Math.random ();
   float currentheight = (float) (mrectheight * mrandom);
   Canvas.drawrect (
     (float) (Mwidth * 0.4/2 + mrectwidth * i + offsets),
     currentheight, (float) (Mwidth * 0.4/2 + mrectwidth
       * (i + 1)), 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.

public void OnStart () {
Postinvalidatedelayed (300);
}

300ms each interval to redraw the view, you can have a better visual effect.

Okay, finally, I'll put all the code on it:

public class Musicline extends View {private Paint mpaint;
 private float mwidth;
 private float mrectwidth;
 private float mrectheight;
 Private float mrectcount = 10;
 private int offsets = 2;
 Private LinearGradient mlineargradient;

 Private double mrandom;

  Public Musicline (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
 Initview ();
  Public Musicline (context, AttributeSet attrs) {Super (context, attrs);
 Initview ();
  Public Musicline {Super (context);
 Initview ();
  /** * Initialization Tool class * * private void Initview () {mpaint = new Paint ();
 Mpaint.setantialias (TRUE);
  @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); Mlineargradient = new LinearGradient (0, 0, mrectwidth, Mrectheight, Color.yellow, Color.Blue, Shader.tilemOde.
  CLAMP);

 Mpaint.setshader (mlineargradient);
  } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
   for (int i = 0; i < Mrectcount i++) {mrandom = Math.random ();
   float currentheight = (float) (mrectheight * mrandom);  Canvas.drawrect ((float) (Mwidth * 0.4/2 + mrectwidth * i + offsets), currentheight, (float) (Mwidth * 0.4/2
  + Mrectwidth * (i + 1)), Mrectheight, mpaint);
 } public void OnStart () {postinvalidatedelayed (300);

 }

}

So far, it's all done.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.