K线的视图和分时图基本一致,主要是矩形的绘制和阴线阳线的结合,eg:用红色柱子举例,先取一个柱子的中间横坐标X,线的绘制很简单,阴线和阳线都一样 横坐标都设为X纵坐标就是算出来的y值,能绘制出两条不相连的竖线,中间部分绘制矩形,用new RectF(left,top,right,bttom)方法 左右为x 上线为y,left为x-a (a为k线宽度的一半)right:x+a,top为阳线的下部y坐标 需要一致,同理bottom为阴线的上方y坐标。 这样形成一个k柱整体。
public class KLineViewPortrait extends View
{
// private float width=0 , height=0;
Resources res = Main.getInstance().getResources();
public KLineViewPortrait(Context context)
{
super(context);
}
public KLineViewPortrait(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public KLineViewPortrait(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
ArrayList<KPointModel> list = Tool_K.k_map.get(Tool.commodityid);
draw_data(canvas,list);
}
/**
* 绘制数据
* @param canvas
* @param list
*/
private void draw_data(Canvas canvas,ArrayList<KPointModel> list)
{
//绘制k线
draw_k3(canvas,list);
//绘制MA
draw_ma(canvas);
if(Tool_K.KPI == Tool_K.KPI_MACD) //绘制MACD或者KDJ
{
draw_macd(canvas);
}else
{
draw_kdj(canvas);
}
draw_prices(canvas);
}
/**
* 绘制K线中的价格
* @param canvas
*/
private void draw_prices(Canvas canvas)
{
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(1);
paint.setTextSize(20);
paint.setTextAlign(Align.RIGHT);
paint.setColor(Color.WHITE);
float diff = (Tool.bottomLine_k_port - Tool.topLine_k_port)/6; //6等分
for(int i=0;i<7;i++)
{
canvas.drawText(Tool_K.prices[i],Tool.startLine_k_port,Tool.topLine_k_port+(6-i)*diff-3, paint);
}
if(Tool_K.KPI == Tool_K.KPI_MACD)
{//MACD
canvas.drawText(Tool_K.macd_prices[0],Tool.startLine_k_port,Tool.bottomLine_k_port+17, paint);
canvas.drawText(Tool_K.macd_prices[1],Tool.startLine_k_port,mid+8,paint);
canvas.drawText(Tool_K.macd_prices[2],Tool.startLine_k_port,Tool.bottom_kpi_port-5, paint);
}else
{//KDJ
canvas.drawText("100",Tool.startLine_k_port,Tool.bottomLine_k_port+15, paint);
canvas.drawText("50",Tool.startLine_k_port,(Tool.bottom_kpi_port+Tool.bottomLine_k_port)/2+8, paint);
canvas.drawText("0",Tool.startLine_k_port,Tool.bottom_kpi_port-5, paint);
}
}
/**
* 绘制K线
* @param canvas
* @param list
*/
private void draw_k3(Canvas canvas,ArrayList<KPointModel> list)
{
Paint paint = new Paint();
Resources res = Main.getInstance().getResources();
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
for(int i=0;i<list.size();i++)
{
KPointModel point = list.get(i);
if(point.getOpenY_port()>point.getCloseY_port()) //收盘坐标小于开盘坐标 收盘价格高 上涨
{
paint.setColor(res.getColor(R.color.k_red));
canvas.drawLine(Tool_K.widths_port[i], point.getHighY_port(),Tool_K.widths_port[i], point.getCloseY_port(), paint);
canvas.drawLine(Tool_K.widths_port[i], point.getOpenY_port(),Tool_K.widths_port[i], point.getLowY_port(), paint);
canvas.drawRect(new RectF(Tool_K.widths_port[i]-Tool_K.each_width_port/2+1, point.getCloseY_port(),Tool_K.widths_port[i]+Tool_K.each_width_port/2-1, point.getOpenY_port()), paint);
}else if(point.getOpenY_port()<point.getCloseY_port())
{
paint.setColor(res.getColor(R.color.k_green));
canvas.drawLine(Tool_K.widths_port[i],point.getHighY_port(),Tool_K.widths_port[i],point.getOpenY_port(), paint);
canvas.drawLine(Tool_K.widths_port[i],point.getCloseY_port(),Tool_K.widths_port[i],point.getLowY_port(), paint);
canvas.drawRect(new RectF(Tool_K.widths_port[i]-Tool_K.each_width_port/2+1, point.getOpenY_port(),Tool_K.widths_port[i]+Tool_K.each_width_port/2-1, point.getCloseY_port()), paint);
}else //持平时,我司业务逻辑视为红色,绘制3条线即可
{
paint.setColor(res.getColor(R.color.k_red));
canvas.drawLine(Tool_K.widths_port[i],point.getHighY_port(),Tool_K.widths_port[i],point.getOpenY_port(), paint);
canvas.drawLine(Tool_K.widths_port[i],point.getCloseY_port(),Tool_K.widths_port[i],point.getLowY_port(), paint);
canvas.drawLine(Tool_K.widths_port[i]-Tool_K.each_width_port/2+1, point.getCloseY_port(), Tool_K.widths_port[i]+Tool_K.each_width_port/2-1, point.getCloseY_port(), paint);
}
}
}
private void draw_ma(Canvas canvas)
{
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(Tool.strokeWidth);
paint.setColor(res.getColor(R.color.white));
canvas.drawLines(Tool_K.ma1_port, paint);
paint.setColor(res.getColor(R.color.yellow));
canvas.drawLines(Tool_K.ma2_port, paint);
paint.setColor(res.getColor(R.color.purple));
canvas.drawLines(Tool_K.ma3_port, paint);
paint.setColor(res.getColor(R.color.blue2));
canvas.drawLines(Tool_K.ma4_port, paint);
}
float mid;
private void draw_macd(Canvas canvas)
{
mid = Tool_K.get_pointY(0, Tool_K.KPI_MACD, Tool.STYPE_K_PORT);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(Tool.strokeWidth);
for(int i=0;i<Tool_K.macd_data_port.length;i++)
{
Path path = new Path();
path.moveTo(Tool_K.widths_port[i]-Tool_K.each_width_port/2+1,mid);
path.lineTo(Tool_K.widths_port[i]-Tool_K.each_width_port/2+1,Tool_K.macd_data_port[i]);
path.lineTo(Tool_K.widths_port[i]+Tool_K.each_width_port/2-1,Tool_K.macd_data_port[i]);
path.lineTo(Tool_K.widths_port[i]+Tool_K.each_width_port/2-1,mid);
path.close();
if(Tool_K.macd_data_port[i]<mid)
{
paint.setColor(res.getColor(R.color.red2));
}else
{
paint.setColor(res.getColor(R.color.green));
}
canvas.drawPath(path, paint);
}
paint.setColor(res.getColor(R.color.white));
canvas.drawLines(Tool_K.diff_data_port, paint); // 绘制diff线
paint.setColor(res.getColor(R.color.yellow));
canvas.drawLines(Tool_K.dea_data_port, paint); // 绘制dea线
Path path = new Path();
path.moveTo(0,mid);
path.lineTo(Tool_K.view_width,mid);
PathEffect effects = new DashPathEffect(new float[] { 5, 5, 5, 5 }, 1);
Paint paint1 = new Paint();
paint1.setColor(Color.GRAY);
paint1.setStrokeWidth(1);
paint1.setPathEffect(effects);
paint1.setStyle(Paint.Style.STROKE);
canvas.drawPath(path, paint1);
}
private void draw_kdj(Canvas canvas)
{
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(Tool.strokeWidth);
paint.setColor(res.getColor(R.color.white));
canvas.drawLines(Tool_K.k_data_port, paint); // 绘制k线
paint.setColor(res.getColor(R.color.yellow));
canvas.drawLines(Tool_K.d_data_port, paint); // 绘制d线
paint.setColor(res.getColor(R.color.purple));
canvas.drawLines(Tool_K.j_data_port, paint); // 绘制j线
Path path = new Path();
path.moveTo(0,(Tool.bottom_kpi_port+Tool.bottomLine_k_port)/2);
path.lineTo(Tool.stopLine_k_port,(Tool.bottom_kpi_port+Tool.bottomLine_k_port)/2);
PathEffect effects = new DashPathEffect(new float[] { 5, 5, 5, 5 }, 1);
Paint paint1 = new Paint();
paint1.setColor(Color.GRAY);
paint1.setStrokeWidth(1);
paint1.setPathEffect(effects);
paint1.setStyle(Paint.Style.STROKE);
canvas.drawPath(path, paint1);
}
}