1
1
package com .stock .view ;
2
2
3
+ import java .util .Arrays ;
4
+ import java .util .Iterator ;
5
+ import java .util .List ;
6
+
3
7
import android .content .Context ;
4
8
import android .graphics .Canvas ;
5
9
import android .graphics .Color ;
9
13
import android .view .View ;
10
14
11
15
import com .stock .data .PriceBar ;
16
+ import com .stock .data .StockData ;
17
+ import com .stock .index .StockIndex ;
12
18
13
19
public abstract class StockPhoto extends View {
14
20
@@ -20,9 +26,145 @@ public StockPhoto(Context context, AttributeSet attrs) {
20
26
super (context , attrs );
21
27
}
22
28
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
+ }
26
168
27
169
protected void drawVolume (Canvas _canvas , Paint _paint , PriceBar [] bars ,
28
170
Rect rect , double maxv ) {
0 commit comments