最近更新|软件分类|单机游戏|软件专题|手机版|软件发布 安卓系统怎么做股票行情K线图?android股票行情K线图制作教程
首页>编程开发>安卓系统怎么做股票行情K线图?android股票行情K线图制作教程

安卓系统怎么做股票行情K线图?android股票行情K线图制作教程

我要评论2016/01/09 10:22:57来源:9553编辑:

安卓系统怎么做股票行情K线图?需要做股票行情app的,就来看下文教程学习android股票行情K线图的制作方法吧。

安卓系统怎么做股票行情K线图?android股票行情K线图制作教程

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

计算问题

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

价格和像素形成个一个比例计算是: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

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);

}

关键词: android 股票