Android version of the stock quotes chart Development _android

Source: Internet
Author: User
Tags dashed line drawtext getcolor

Now in the hand is a securities information type of app, which involves the stock market interface, the market has chart and so on, see a lot of people on the Internet to seek this information, so I specially wrote a demo here to share with you.

Here's how it works:

The background map is the use of canvas first draw a rectangle, and then draw a few dotted lines, the EMA is drawn through the path, in short the drawing is very simple, I do not introduce here, we can go to GitHub download source to see. Related to the EMA, the highest price, the lowest price, closing prices, the concept of open, we can Baidu.

What I'm going to introduce here is the computational problem:

We can see the time-sharing map, K, K, the left side of the transaction prices are not the same, and our K-line are drawn through this price, that is, the price is a moment of change, then our K-line drawing is also a change. Let's say we're going to figure out how to draw the minute of 25.69 in a time-sharing chart, which position to draw on the screen, and how this should be painted, the price is variable, and the position of the picture is changed, but there is a point that the size of our screen is unchanged. So we can use the height of the background graph to calculate where the line chart of a certain price should begin to draw. We can figure out how many prices a pixel corresponds to, and the analysis is as follows:

Price and pixel form a proportional calculation is:double heightscale = (endy-starty)/(Highprice-lowprice);

Therefore the price 25.69 should be drawn in mstarty = (float) (starty+ (highPrice-25.69) * Heightscale);

After that, the rest of the principles are the same, I do not introduce, the following is part of the code:

@Override protected void Drawkchatbackground () {Rect dirty = new Rect (left, Kcharttop, right, kchartbottom); 
    A rectangular mcanvas.drawrect (dirty, Linegraypaint) drawing a background picture; 
    Patheffect effects = new Dashpatheffect (new float[] {5, 5, 5, 5}, 1); 
    Linegraypaint.setpatheffect (effects); 
    Path PATH = new Path (); 
    int y = kcharttop + 15; 
    Draw the dotted line above Path.moveto (left, y); 
    Path.lineto (right, y); 
    String Text = Getpricetext (highprice); 
    int textHeight = (int) (Textgraypaint.descent ()-textgraypaint.ascent ()); 
    Mcanvas.drawtext (Text,left-textgraypaint.measuretext (text)-5,y + TEXTHEIGHT/2, textgraypaint); 
    Double max = Highprice-lowprice; 
      if (Max > 10) {//divided into four halves//draw the middle of three dotted int n = 4; Double sper = (highprice-lowprice)/4;//the price for each equal representation for (int i=1;i<n;i++) {y = i* ((kchartbottom-kcha 
        Rttop)/n) + kcharttop; 
        Path.moveto (left, y); 
        Path.lineto (Right,y); Text = Getpricetext (highprice-i*sper); 
      Mcanvas.drawtext (Text,left-textgraypaint.measuretext (text)-5,y + textheight/2,textgraypaint); 
      }else{//Divide into two halves//draw the middle dotted line y = (kchartbottom-kcharttop)/2 + kcharttop; 
      Path.moveto (left, y); 
      Path.lineto (right, y); 
      Text = Getpricetext (Highprice-(Highprice-lowprice)/2); 
    Mcanvas.drawtext (Text,left-textgraypaint.measuretext (text)-5,y + textheight/2,textgraypaint); 
    ///Draw the following dotted line y = KChartbottom-15; 
    Path.moveto (left, y); 
    Path.lineto (right, y); 
    Text = Getpricetext (lowprice); 
Mcanvas.drawtext (Text,left-textgraypaint.measuretext (text)-5,y + textheight/2,textgraypaint); 
      Draw the dotted line and the following date for (int i = num-1 i > 0; i--) {int x = left + perwidth * i; 
      Path.moveto (x, kcharttop); 
      Path.lineto (x, Kchartbottom); 
    PERXPOINT[I-1] = x; 
  } mcanvas.drawpath (path, linegraypaint); 

 }
@Override protected void Drawmachart () {//Draw ema path PATH5 = new Path (); 
    Path path10 = new Path (); 
    Path PATH20 = new Path (); 
    Double Heightscale = (kchartbottom-kcharttop)/(Highprice-lowprice); 
    int mastart = left; 
    float Mastarty; 
    Path5.moveto (Mastart, (float) (Kcharttop + (highprice-infos.get (0). GETMAVALUE5 ()) * Heightscale)); 
    Path10.moveto (Mastart, (float) (Kcharttop + (highprice-infos.get (0). GETMAVALUE10 ()) * Heightscale)); 
     
    Path20.moveto (Mastart, (float) (Kcharttop + (highprice-infos.get (0). GETMAVALUE20 ()) * Heightscale)); for (Singlestockinfo Info:infos) {Mastart + = per * perhalf;//The actual data for each day is 4/6, and the left and right margins are 1/6 mastarty = (float) (KC 
      Harttop + (Highprice-info.getmavalue5 ()) * Heightscale); 
      Path5.lineto (Mastart, Mastarty); 
      Mastarty = (float) (Kcharttop + (Highprice-info.getmavalue10 ()) * Heightscale); 
      Path10.lineto (Mastart, Mastarty); Mastarty = (float) (Kcharttop + (highPRICE-INFO.GETMAVALUE20 ()) * Heightscale); 
      Path20.lineto (Mastart, Mastarty); 
    Mastart + = per * PERHALF; 
    } Paint Paint = new Paint (); 
    Paint.setcolor (Color.Blue); 
    Paint.setantialias (TRUE); 
    Paint.setstrokewidth (2); 
    Paint.setstyle (Style.stroke); 
    Mcanvas.drawpath (Path5, paint); 
    Paint.setcolor (Color.magenta); 
    Mcanvas.drawpath (path10, paint); 
    Paint.setcolor (Color.green); 
  Mcanvas.drawpath (PATH20, paint); }
/** * The column chart below */@Override protected void Drawpillarschart (int flag) {Linegraypaint.setpatheffect (null 
    ); 
    Rect dirty = new Rect (left, Pillarscharttop, right, pillarschartbottom); 
     
    A rectangular mcanvas.drawrect (dirty, Linegraypaint) drawing a background picture; 
    int y = pillarscharttop + (pillarschartbottom-pillarscharttop)/2; 
     
    Mcanvas.drawline (Left,y,right, y, linegraypaint); 
    Middle value String totalcount = Getpricetext (maxcount/2/10000); 
    float Maginleft = Left-textgraypaint.measuretext (totalcount)-5; 
    Mcanvas.drawtext (TotalCount, Maginleft, Y,textgraypaint); 
    The value above TotalCount = Getpricetext (maxcount/10000); 
    Maginleft = Left-textgraypaint.measuretext (totalcount)-5; 
    Mcanvas.drawtext (TotalCount, Maginleft, Pillarscharttop,textgraypaint); 
    The following value TotalCount = "million Hands"; 
    Maginleft = Left-textgraypaint.measuretext (totalcount)-5; 
  Mcanvas.drawtext (TotalCount, Maginleft, Pillarschartbottom,textgraypaint);  int pstart = left; 
    float Pstarty; 
    Double Heightscale = (pillarschartbottom-pillarscharttop)/maxcount; 
    Paint Paint = new Paint (); 
    Paint.setantialias (TRUE); 
    Paint.setstyle (Paint.Style.FILL); if (flag = = Stockservice.flag) {for (Minuteinfo Info:minuteinfos) {Pstart = per * per16;//The actual data taken each day is 4/6, 
        Plus 1/6 pstarty = (float) (Pillarscharttop + (Maxcount-info.getvolume ()) * Heightscale); 
        Dirty = new Rect (Pstart, (int) pstarty, (int) (Pstart + per * per46), pillarsChartbottom-2); 
        Paint.setcolor (Info.getcolor ()); 
        A rectangular mcanvas.drawrect (dirty, paint) drawing a background picture; Pstart + = per56;//right spacing 5/6}}else{for (Singlestockinfo info:infos) {Pstart = per * p er16;//the actual data per day is 4/6, plus 1/6 pstarty = (float) (Pillarscharttop + (Maxcount-info.gettotalcount ()) * Heightscale 
        ); Dirty = new Rect (Pstart, (int) pstarty, (int) (Pstart + per * per46), PillarschartbottoM-2); 
        Paint.setcolor (Info.getcolor ()); 
        A rectangular mcanvas.drawrect (dirty, paint) drawing a background picture; 
 Pstart + = per56;//to Right of 5/6}}}
/** * time-sharing map/@Override public void Drawhourschart () {Double Heightscale = (kchartbottom-kcharttop)/( 
    Highprice-lowprice); 
    int cleft = left; 
    int ctop = 0; 
    Path PATH = new Path (); 
    Path.moveto (cleft, KChartbottom-2); 
    int position = 0; 
      int perpointx = perxpoint[position];//records the x-coordinate of the first vertical dash for (Minuteinfo Info:minuteinfos) {cleft + = per * PER16; 
      Ctop = (int) (Kcharttop + (Highprice-info.getnow ()) * Heightscale); 
      Path.lineto (cleft + per * per26, ctop); if (cleft >= perpointx) {//is just drawn to the first vertical dashed line, you need to draw the following time String text = Kchartutil.getminute (info.getminute 
        ()); 
        float textWidth = textgraypaint.measuretext (text); 
        int textHeight = (int) (Textgraypaint.descent ()-textgraypaint.ascent ()); 
        Mcanvas.drawtext (text, PERPOINTX-TEXTWIDTH/2, Kchartbottom + textHeight, textgraypaint); if (!) ( Position = = perxpoint.length-1)) {LOG.E (TAG, perpointx+ "----------")+info.getminute () + "---" +text); 
        Perpointx = Perxpoint[++position]; 
    } cleft + = per56;//to the right margin of + = per * 5/6} path.lineto (cleft, KChartbottom-2); 
    Paint linepaint = new Paint (); 
    Linepaint.setcolor (Color.Blue); 
    Linepaint.setantialias (TRUE); 
    Linepaint.setstrokewidth (1); 
Linepaint.setstyle (Style.stroke); 
    Linepaint.setstyle (Style.stroke); 
    Mcanvas.drawpath (path, linepaint); 
    Linepaint.setalpha (50); 
    Linepaint.setstyle (Style.fill); 
  Mcanvas.drawpath (path, linepaint); 
 }

The beginning of the new Year, China's stock market out of the world rare, unprecedented after the extreme market, the stock market risk, investment need to be cautious.
This is a digression, the focus is still want to learn about Android programming help.

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.