Skip to content

Commit d95419b

Browse files
committed
draw broken line.
1 parent 282f49e commit d95419b

File tree

5 files changed

+186
-39
lines changed

5 files changed

+186
-39
lines changed

AndroidManifest.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
android:versionCode="1"
66
android:versionName="1.0" >
77

8-
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
8+
<uses-sdk android:minSdkVersion="12" android:targetSdkVersion="17" />
99

1010
<uses-permission android:name="android.permission.INTERNET" />
1111
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

bin/AndroidManifest.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
android:versionCode="1"
66
android:versionName="1.0" >
77

8-
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
8+
<uses-sdk android:minSdkVersion="12" android:targetSdkVersion="17" />
99

1010
<uses-permission android:name="android.permission.INTERNET" />
1111
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

src/com/stock/view/BrokenLineView.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
import java.util.regex.Matcher;
77
import java.util.regex.Pattern;
88

9+
import com.stock.data.PriceBar;
910
import com.stock.data.StockData;
1011
import com.stock.source.DataSource;
1112
import com.stock.turtle.R;
1213

1314
import android.content.Context;
1415
import android.content.res.TypedArray;
1516
import android.graphics.Canvas;
17+
import android.graphics.Color;
18+
import android.graphics.Paint;
19+
import android.graphics.Rect;
1620
import android.util.AttributeSet;
1721

1822
public class BrokenLineView extends StockPhoto {
@@ -56,8 +60,18 @@ public BrokenLineView(Context context, AttributeSet attrs){
5660
stocks.add(new StockData("600036", DataSource.MARKET_SHANGHAI));
5761
}
5862

59-
private void DrawBrokenLine(Canvas canvas, StockData stock) {
60-
// if( )
63+
protected void draw(Canvas canvas, Paint p, PriceBar[] bars, Rect rect) {
64+
double bhp = rect.height() / (high - low);
65+
int height = rect.top + rect.height(), middle = rect.width() - step / 2;
66+
PriceBar prev = bars[0];
67+
68+
for( int i = 1; i < bars.length; i ++ ) {
69+
PriceBar curr = bars[i];
70+
int y1 = (int) Math.round( (prev.close - low) * bhp );
71+
int y2 = (int) Math.round( (curr.close - low) * bhp );
72+
canvas.drawLine(middle, height - y1, middle, height - y2, p);
73+
middle -= step;
74+
}
6175
}
6276

6377
@Override
@@ -69,7 +83,7 @@ protected void onDraw(Canvas canvas) {
6983
StockData stock = iter.next();
7084
if( stock.getBarSet().size() == 0 )
7185
continue;
72-
DrawBrokenLine(canvas, stock);
86+
// DrawBrokenLine(canvas, stock);
7387
}
7488
}
7589
}

src/com/stock/view/CandleImage.java

+22-31
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,11 @@ public CandleImage(Context context, AttributeSet attrs) {
3030
private List<PriceBar> bar_list;
3131
private List<StockIndex> indexes = new ArrayList<StockIndex>();
3232

33-
private int trans = 0; ///< transform the initialize position of candle image to left
34-
private int scoll = 0; ///< scoll screen
35-
36-
// public CandleImage(StockData data) {
37-
// this.bar_list = data.getBarSet();
38-
// }
39-
4033
public void AddIndex(StockIndex index) {
4134
index.calcIndex(bar_list);
4235
indexes.add(index);
4336
}
4437

45-
public void setBarWidth(int width) {
46-
this.step = width;
47-
}
48-
49-
public void tranCandle(int trans) {
50-
this.trans = trans;
51-
}
52-
53-
public void scollCandle(int move) {
54-
this.scoll = move;
55-
}
56-
5738
public void Save(String file, int width, int height) {
5839
Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
5940

@@ -74,7 +55,7 @@ public void Save(String file, int width, int height) {
7455
// }
7556
}
7657

77-
public void display(Canvas g, Rect rect) {
58+
public void display(Canvas canvas, Rect rect) {
7859
int divid = Math.round(rect.height() * 0.8f);
7960
float b = divid * 0.5f, s = b * scale;
8061
Rect topWindow = new Rect(rect.left, rect.top + Math.round(b - s), rect.width(), Math.round(2 * s));
@@ -84,12 +65,12 @@ public void display(Canvas g, Rect rect) {
8465
p.setColor(background);
8566
p.setStyle(Paint.Style.FILL);//设置填满
8667

87-
g.clipRect(rect);
88-
g.drawRect(rect, p);
68+
canvas.clipRect(rect);
69+
canvas.drawRect(rect, p);
8970

9071
p.setColor(Color.RED);
91-
g.drawRect(rect.left + 1, rect.top + 1, rect.width() - 2, divid - 2, p);
92-
g.drawRect(btmWindow.left + 1, btmWindow.top, btmWindow.width() - 2,
72+
canvas.drawRect(rect.left + 1, rect.top + 1, rect.width() - 2, divid - 2, p);
73+
canvas.drawRect(btmWindow.left + 1, btmWindow.top, btmWindow.width() - 2,
9374
btmWindow.height() - 2, p);
9475

9576
int width = Math.min(rect.width(), rect.width() + (scoll - trans) * step);
@@ -117,9 +98,19 @@ public void display(Canvas g, Rect rect) {
11798
maxVolume = Math.max(bars[i].volume, maxVolume);
11899
}
119100

120-
drawVolume(g, p, bars, btmWindow, maxVolume);
121-
drawCandle(g, p, bars, topWindow, high, low);
101+
drawVolume(canvas, p, bars, btmWindow, maxVolume);
102+
drawCandle(canvas, p, bars, topWindow);
103+
104+
}
122105

106+
protected void drawIndexInTop(Canvas canvas, StockIndex index, Rect topWindow) {
107+
int right = topWindow.width() - step / 2;
108+
double scale1 = - topWindow.height() / (high - low);
109+
double base1 = topWindow.top + topWindow.height() - low * scale1;
110+
index.drawIndex(canvas, first, count, step, right, scale1, base1);
111+
}
112+
113+
protected void drawIndexes(Canvas canvas, Rect topWindow, Rect btmWindow) {
123114
int right = topWindow.width() - step / 2;
124115
double scale1 = - topWindow.height() / (high - low),
125116
scale2 = - btmWindow.height() * 0.8 / maxVolume;
@@ -131,16 +122,16 @@ public void display(Canvas g, Rect rect) {
131122
StockIndex index = _it.next();
132123

133124
if( index.getWindowIndex() == StockIndex.WINDOW_TOP )
134-
index.drawIndex(g, first, count, step, right, scale1, base1);
125+
index.drawIndex(canvas, first, count, step, right, scale1, base1);
135126
else if( index.getWindowIndex() == StockIndex.WINDOW_BOTTOM )
136-
index.drawIndex(g, first, count, step, right, scale2, base2);
127+
index.drawIndex(canvas, first, count, step, right, scale2, base2);
137128
}
138129
}
139130

140-
private void drawCandle(Canvas g, Paint p, PriceBar[] bars, Rect rect, double high, double low) {
131+
private void drawCandle(Canvas g, Paint p, PriceBar[] bars, Rect rect) {
141132
int delta = step / 8, bar_width = step * 3 / 4;
142-
int x = rect.width() - step + delta, height = rect.top + rect.height(),
143-
middle = rect.width() - step / 2;
133+
int x = rect.width() - step + delta;
134+
int height = rect.top + rect.height(), middle = rect.width() - step / 2;
144135
double bhp = rect.height() / (high - low);
145136

146137
// Graphics2D g2d = (Graphics2D)g;

src/com/stock/view/StockPhoto.java

+145-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.stock.view;
22

3+
import java.util.Arrays;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
37
import android.content.Context;
48
import android.graphics.Canvas;
59
import android.graphics.Color;
@@ -9,6 +13,8 @@
913
import android.view.View;
1014

1115
import com.stock.data.PriceBar;
16+
import com.stock.data.StockData;
17+
import com.stock.index.StockIndex;
1218

1319
public abstract class StockPhoto extends View {
1420

@@ -20,9 +26,145 @@ public StockPhoto(Context context, AttributeSet attrs) {
2026
super(context, attrs);
2127
}
2228

23-
protected int step = 8;
24-
protected float scale = .8f;
25-
protected int background = Color.BLACK;
29+
/** 平移
30+
* 最新一个价格线初始位置,为0时,最新一根价格线出现在图形区域的最右边,
31+
* 大于0时,为向左平移的单位数
32+
* */
33+
public int trans = 0;
34+
/**滚动
35+
* 整个股票图形可以向右方滚动
36+
*/
37+
public int scoll = 0;
38+
/**放缩
39+
* 图形中能放进去的价格线数目
40+
*/
41+
public int step = 8;
42+
/**最高价和最低价在图片区域上下方向所占比例
43+
*/
44+
public float scale = .8f;
45+
46+
public boolean volume_visible = true;
47+
48+
public int background = Color.WHITE;
49+
public boolean bound_visible = true;
50+
public int bound_color = Color.RED;
51+
52+
protected int first, count;
53+
protected double high, low;
54+
protected double maxVolume;
55+
56+
public void setBarWidth(int width) {
57+
this.step = width;
58+
}
59+
60+
public void tranCandle(int trans) {
61+
this.trans = trans;
62+
}
63+
64+
public void scollCandle(int move) {
65+
this.scoll = move;
66+
}
67+
68+
private void highLowPrice(PriceBar[] bars) {
69+
high = bars[first].high;
70+
low = bars[first].low;
71+
maxVolume = bars[first].volume;
72+
73+
for( int i = 1; i < count; i ++ ) {
74+
PriceBar bar = bars[first + i];
75+
high = Math.max(bar.get(PriceBar.PRICE_HIGH), high);
76+
low = Math.min(bar.get(PriceBar.PRICE_LOW ), low);
77+
maxVolume = Math.max(bar.volume, maxVolume);
78+
}
79+
}
80+
81+
public void display(Canvas canvas, Rect rect, StockData stock) {
82+
Paint paint = new Paint();
83+
84+
// 上下两个分离的窗口,分割位置
85+
int divid = Math.round(rect.height() * 0.8f);
86+
float b = divid * 0.5f, s = b * scale;
87+
Rect topWindow = new Rect(rect.left, rect.top + Math.round(b - s),
88+
rect.width(), Math.round(2 * s));
89+
Rect btmWindow = new Rect(rect.left, rect.top + divid + 10,
90+
rect.width(), rect.height() - divid - 10);
91+
92+
// 1, 先刷新背景,填充默认背景色
93+
paint.setColor(background);
94+
paint.setStyle(Paint.Style.FILL);//设置填满
95+
canvas.clipRect(rect);
96+
canvas.drawRect(rect, paint);
97+
98+
// 2, 绘制上下两个窗口的边框
99+
if( bound_visible ) {
100+
paint.setColor(bound_color);
101+
canvas.drawRect(rect.left + 1, rect.top + 1, rect.width() - 2,
102+
divid - 2, paint);
103+
canvas.drawRect(btmWindow.left + 1, btmWindow.top, btmWindow.width() - 2,
104+
btmWindow.height() - 2, paint);
105+
}
106+
107+
// 3, 计算上下窗口中,显示图形的具体区域
108+
List<PriceBar> bar_list = stock.getBarSet();
109+
int width = Math.min(rect.width(), rect.width() + (scoll - trans) * step);
110+
topWindow.right = topWindow.left + width;
111+
btmWindow.right = btmWindow.left + width;
112+
113+
// 4, 准备数据
114+
count = Math.min(topWindow.width() / step + 1, bar_list.size());
115+
first = Math.max(0, scoll - trans);
116+
PriceBar[] bar_array = new PriceBar[bar_list.size()];
117+
bar_array = bar_list.toArray(bar_array);
118+
119+
PriceBar[] bars = Arrays.copyOfRange(bar_array, first, first + count);
120+
highLowPrice(bars);
121+
122+
// 5, 绘制上图中的价格线
123+
// drawCandle(canvas, paint, bars, topWindow, high, low);
124+
// 6, 绘制下图中的交易量
125+
// drawVolume(canvas, paint, bars, btmWindow, maxVolume);
126+
// 7, 绘制指标,不同指标显示在不同窗口中
127+
// drawIndex();
128+
129+
// double high = 0, low = 0, maxVolume = 0;
130+
// Iterator<PriceBar> iter = bar_list.listIterator(first);
131+
// PriceBar[] bars = new PriceBar[count];
132+
//
133+
// if( iter.hasNext() ) {
134+
// bars[0] = iter.next();
135+
// high = bars[0].high;
136+
// low = bars[0].low;
137+
// maxVolume = bars[0].volume;
138+
// }
139+
//
140+
// for( int i = 1; i < count && iter.hasNext(); i ++ ) {
141+
// bars[i] = iter.next();
142+
// high = Math.max(bars[i].get(PriceBar.PRICE_HIGH), high);
143+
// low = Math.min(bars[i].get(PriceBar.PRICE_LOW), low);
144+
// maxVolume = Math.max(bars[i].volume, maxVolume);
145+
// }
146+
147+
148+
// int right = topWindow.width() - step / 2;
149+
// double scale1 = - topWindow.height() / (high - low),
150+
// scale2 = - btmWindow.height() * 0.8 / maxVolume;
151+
// double base1 = topWindow.top + topWindow.height() - low * scale1,
152+
// base2 = btmWindow.top + btmWindow.height();
153+
//
154+
// Iterator<StockIndex> _it = indexes.iterator();
155+
// while( _it.hasNext() ) {
156+
// StockIndex index = _it.next();
157+
//
158+
// if( index.getWindowIndex() == StockIndex.WINDOW_TOP )
159+
// index.drawIndex(canvas, first, count, step, right, scale1, base1);
160+
// else if( index.getWindowIndex() == StockIndex.WINDOW_BOTTOM )
161+
// index.drawIndex(canvas, first, count, step, right, scale2, base2);
162+
// }
163+
}
164+
165+
public void DrawStock() {
166+
167+
}
26168

27169
protected void drawVolume(Canvas _canvas, Paint _paint, PriceBar[] bars,
28170
Rect rect, double maxv) {

0 commit comments

Comments
 (0)