欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android版的股票行情K線圖開發(fā)

 更新時(shí)間:2016年01月08日 16:02:22   作者:andywuchuanlong  
這篇文章主要介紹了Android版的股票行情K線圖開發(fā),感興趣的小伙伴們可以參考一下

現(xiàn)在在手上的是一個(gè)證券資訊類型的app,其中有涉及到股票行情界面,行情中有K線圖等,看到網(wǎng)上很多人在求這方面的資料,所以我特地寫了一個(gè)demo在此處給大家分享一下。

下面是做出來的效果圖:

背景圖是利用canvas先畫出一個(gè)矩形,然后再畫幾根虛線,均線圖是通過path來繪制的,總之圖的繪制是很簡單的,我就不在這里作介紹了,大家可以去github下載源碼看看。涉及到均線、最高價(jià)、最低價(jià)、收盤價(jià)、開盤價(jià)的概念大家可以百度一下。

我再這里要介紹的是計(jì)算問題:

大家可以看到分時(shí)圖、日K、月K的左邊的成交價(jià)格都是不一樣的,而我們的k線都是通過這個(gè)價(jià)格來繪制的,也就是說價(jià)格是時(shí)刻變動(dòng),那么我們的k線繪制也是變動(dòng)的。假設(shè)我們要計(jì)算分時(shí)圖中價(jià)格為25.69的那一分鐘應(yīng)該如何畫,畫在屏幕中的哪一個(gè)位置,那么這個(gè)應(yīng)該怎么畫呢,價(jià)格是變動(dòng)的,畫的位置也是變動(dòng)的,但是有一點(diǎn)我們屏幕的大小是不變的。所以我們可以通過背景圖的高度來計(jì)算某個(gè)價(jià)格的線圖應(yīng)該從哪個(gè)地方開始畫。我們可以計(jì)算出一個(gè)像素點(diǎn)對(duì)應(yīng)多少個(gè)價(jià)格,分析圖如下:

價(jià)格和像素形成個(gè)一個(gè)比例計(jì)算是:double   heightScale = (endY - startY)/(highPrice - lowPrice);

所以價(jià)格25.69應(yīng)該是畫在mStartY = (float) (startY+ (highPrice - 25.69) * heightScale);

這個(gè)明白了之后其他的原理都是一樣的,我就不介紹了,下面是部分代碼:

@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;// 每一等分代表的價(jià)格 
      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;// 每一天實(shí)際所占的數(shù)據(jù)是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;// 每一天實(shí)際所占的數(shù)據(jù)是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;// 每一天實(shí)際所占的數(shù)據(jù)是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 
      } 
    } 
  } 
/** 
   * 分時(shí)圖 
   */ 
  @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坐標(biāo) 
    for(MinuteInfo info:minuteInfos){ 
      cLeft += per * per16; 
      cTop = (int) (kChartTop + (highPrice - info.getNow()) * heightScale); 
      path.lineTo(cLeft + per * per26, cTop); 
      if (cLeft >= perPointX){ 
        // 恰好畫到第一條垂直虛線的地方,需要畫下面的時(shí)間 
        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); 
  } 

新年伊始,中國股市走出世界罕見,前無古人后無來者的極端行情,股市有風(fēng)險(xiǎn),投資需謹(jǐn)慎。
這句話是題外話了,重點(diǎn)還是希望對(duì)大家學(xué)習(xí)Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論