欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android版的股票行情K线图开发

程序员文章站 2023-12-18 20:42:04
现在在手上的是一个证券资讯类型的app,其中有涉及到股票行情界面,行情中有k线图等,看到网上很多人在求这方面的资料,所以我特地写了一个demo在此处给大家分享一下。 下面...

现在在手上的是一个证券资讯类型的app,其中有涉及到股票行情界面,行情中有k线图等,看到网上很多人在求这方面的资料,所以我特地写了一个demo在此处给大家分享一下。

下面是做出来的效果图:

Android版的股票行情K线图开发

背景图是利用canvas先画出一个矩形,然后再画几根虚线,均线图是通过path来绘制的,总之图的绘制是很简单的,我就不在这里作介绍了,大家可以去github下载源码看看。涉及到均线、最高价、最低价、收盘价、开盘价的概念大家可以百度一下。

我再这里要介绍的是计算问题:

大家可以看到分时图、日k、月k的左边的成交价格都是不一样的,而我们的k线都是通过这个价格来绘制的,也就是说价格是时刻变动,那么我们的k线绘制也是变动的。假设我们要计算分时图中价格为25.69的那一分钟应该如何画,画在屏幕中的哪一个位置,那么这个应该怎么画呢,价格是变动的,画的位置也是变动的,但是有一点我们屏幕的大小是不变的。所以我们可以通过背景图的高度来计算某个价格的线图应该从哪个地方开始画。我们可以计算出一个像素点对应多少个价格,分析图如下:

Android版的股票行情K线图开发

价格和像素形成个一个比例计算是:double   heightscale = (endy - starty)/(highprice - lowprice);

所以价格25.69应该是画在mstarty = (float) (starty+ (highprice - 25.69) * heightscale);

这个明白了之后其他的原理都是一样的,我就不介绍了,下面是部分代码:

@override 
  protected void drawkchatbackground() { 
    rect dirty = new rect(left, kcharttop, right, kchartbottom); 
    // 画背景图的矩形 
    mcanvas.drawrect(dirty, linegraypaint); 
    patheffect effects = new dashpatheffect(new float[] { 5, 5, 5, 5 }, 1); 
    linegraypaint.setpatheffect(effects); 
    path path = new path(); 
    int y = kcharttop + 15; 
    // 画上面的虚线 
    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){ 
      // 分成四等分 
      // 画中间的三根虚线 
      int n = 4; 
      double sper = (highprice - lowprice) / 4;// 每一等分代表的价格 
      for(int i=1;i<n;i++){ 
        y = i*((kchartbottom - kcharttop)/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{ 
      // 分成两等分 
      // 画中间的虚线 
      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); 
    } 
    // 画下面的虚线 
    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); 
//   // 画等分的虚线和下面的日期 
    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() { 
    // 画均线 
    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;// 每一天实际所占的数据是4/6,左右边距各1/6  
      mastarty = (float) (kcharttop + (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); 
  }
/** 
   * 下面的柱形图 
   */ 
  @override 
  protected void drawpillarschart(int flag) { 
    linegraypaint.setpatheffect(null); 
    rect dirty = new rect(left, pillarscharttop, right, pillarschartbottom); 
    // 画背景图的矩形 
    mcanvas.drawrect(dirty, linegraypaint); 
     
    int y = pillarscharttop + (pillarschartbottom - pillarscharttop)/2; 
    mcanvas.drawline(left,y,right, y, linegraypaint); 
     
    // 中间的值 
    string totalcount = getpricetext(maxcount/2/10000); 
    float maginleft = left - textgraypaint.measuretext(totalcount)- 5; 
    mcanvas.drawtext(totalcount, maginleft, y,textgraypaint); 
    // 上面的值 
    totalcount = getpricetext(maxcount/10000); 
    maginleft = left - textgraypaint.measuretext(totalcount)- 5; 
    mcanvas.drawtext(totalcount, maginleft, pillarscharttop,textgraypaint); 
    // 下面的值 
    totalcount = "万手"; 
    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;// 每一天实际所占的数据是4/6,加上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()); 
        // 画背景图的矩形 
        mcanvas.drawrect(dirty, paint); 
        pstart += per * per56;// 右边的间距 5/6 
      } 
    }else{ 
      for(singlestockinfo info:infos){ 
        pstart += per * per16;// 每一天实际所占的数据是4/6,加上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()); 
        // 画背景图的矩形 
        mcanvas.drawrect(dirty, paint); 
        pstart += per * per56;// 右边的间距 5/6 
      } 
    } 
  } 
/** 
   * 分时图 
   */ 
  @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];// 记录第一条垂直虚线的x坐标 
    for(minuteinfo info:minuteinfos){ 
      cleft += per * per16; 
      ctop = (int) (kcharttop + (highprice - info.getnow()) * heightscale); 
      path.lineto(cleft + per * per26, ctop); 
      if (cleft >= perpointx){ 
        // 恰好画到第一条垂直虚线的地方,需要画下面的时间 
        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 += per * per56;// 右边的间距 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); 
  } 

新年伊始,中国股市走出世界罕见,前无古人后无来者的极端行情,股市有风险,投资需谨慎。
这句话是题外话了,重点还是希望对大家学习android程序设计有所帮助。

上一篇:

下一篇: