Skip to content

Commit dc52621

Browse files
committed
Broken Line Photo
1 parent 79ad36a commit dc52621

File tree

7 files changed

+161
-40
lines changed

7 files changed

+161
-40
lines changed

res/layout/stock_activity.xml

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5-
android:orientation="vertical" >
5+
android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res/com.stock.turtle">
66

7-
<com.stock.drawing.BrokenLineView
7+
<com.stock.view.BrokenLineView
88
android:id="@+id/brokenLineView1"
99
android:layout_width="wrap_content"
10-
android:layout_height="wrap_content" />
10+
android:layout_height="wrap_content"
11+
app:sh_stocks="601299,600010"
12+
app:sz_stocks="000731" />
1113

1214
</LinearLayout>
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.stock.data;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
import android.util.Log;
8+
9+
public class StockListLoader implements Runnable {
10+
public static final String TAG = "StockLoader";
11+
12+
public List<StockData> stocks = new ArrayList<StockData>();
13+
14+
public StockListLoader(List<StockData> list) {
15+
stocks = list;
16+
}
17+
18+
public StockListLoader(StockData...args) {
19+
for(StockData stock:args) {
20+
stocks.add(stock);
21+
}
22+
}
23+
24+
@Override
25+
public void run() {
26+
Log.i(TAG, "loading stock data...");
27+
Iterator<StockData> iter = stocks.iterator();
28+
while(iter.hasNext()) {
29+
iter.next().load();
30+
}
31+
Log.i(TAG, "load stock succeed.");
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.stock.turtle;
2+
3+
import com.stock.data.StockListLoader;
4+
import com.stock.view.BrokenLineView;
5+
6+
import android.app.Activity;
7+
import android.os.Bundle;
8+
import android.os.Handler;
9+
import android.os.HandlerThread;
10+
import android.util.Log;
11+
12+
public class BrokenLineActivity extends Activity {
13+
public static final String TAG = "Turtle:MainActivity";
14+
private Handler mHandler;
15+
private BrokenLineView brokenline;
16+
17+
@Override
18+
protected void onCreate(Bundle savedInstanceState) {
19+
super.onCreate(savedInstanceState);
20+
setContentView(R.layout.stock_activity);
21+
Log.i(TAG, "onCreate");
22+
23+
HandlerThread mHandlerThread = new HandlerThread("LoadStockData");
24+
mHandlerThread.start();
25+
mHandler = new Handler(mHandlerThread.getLooper());
26+
27+
brokenline = (BrokenLineView) findViewById(R.id.brokenLineView1);
28+
}
29+
30+
@Override
31+
protected void onStart() {
32+
super.onStart();
33+
Log.i(TAG, "onStart");
34+
35+
mHandler.post(new StockListLoader(brokenline.stocks));
36+
}
37+
38+
39+
}

src/com/stock/turtle/MainActivity.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ private List<Map<String, Object>> LoadStockList() {
9999
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
100100

101101
for( int i = 0; i < 3; i ++ ) {
102-
Map<String, Object> map = null;
103-
map = loadStockInfo(codes[i], markets[i],
102+
Map<String, Object> map = loadStockInfo(codes[i], markets[i],
104103
buy_index, sell_index);
105104
map.put("name", names[i]);
106105
list.add(map);

src/com/stock/drawing/BrokenLineView.java renamed to src/com/stock/view/BrokenLineView.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package com.stock.drawing;
1+
package com.stock.view;
22

33
import java.util.ArrayList;
4+
import java.util.Iterator;
45
import java.util.List;
56
import java.util.regex.Matcher;
67
import java.util.regex.Pattern;
@@ -13,11 +14,10 @@
1314
import android.content.res.TypedArray;
1415
import android.graphics.Canvas;
1516
import android.util.AttributeSet;
16-
import android.view.View;
1717

18-
public class BrokenLineView extends View {
18+
public class BrokenLineView extends StockPhoto {
1919

20-
List<StockData> stocks = new ArrayList<StockData>();
20+
public List<StockData> stocks = new ArrayList<StockData>();
2121

2222
public BrokenLineView(Context context) {
2323
super(context);
@@ -56,8 +56,20 @@ public BrokenLineView(Context context, AttributeSet attrs){
5656
stocks.add(new StockData("600036", DataSource.MARKET_SHANGHAI));
5757
}
5858

59+
private void DrawBrokenLine(Canvas canvas, StockData stock) {
60+
// if( )
61+
}
62+
5963
@Override
6064
protected void onDraw(Canvas canvas) {
6165
super.onDraw(canvas);
66+
67+
Iterator<StockData> iter = stocks.iterator();
68+
while( iter.hasNext() ) {
69+
StockData stock = iter.next();
70+
if( stock.getBarSet().size() == 0 )
71+
continue;
72+
DrawBrokenLine(canvas, stock);
73+
}
6274
}
6375
}

src/com/stock/drawing/CandleImage.java renamed to src/com/stock/view/CandleImage.java

+15-31
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
1-
package com.stock.drawing;
1+
package com.stock.view;
22

33
import java.util.ArrayList;
44
import java.util.Iterator;
55
import java.util.List;
66

7+
import android.content.Context;
78
import android.graphics.Bitmap;
89
import android.graphics.Bitmap.Config;
910
import android.graphics.Canvas;
1011
import android.graphics.Color;
1112
import android.graphics.Paint;
1213
import android.graphics.Rect;
14+
import android.util.AttributeSet;
1315

1416
import com.stock.data.PriceBar;
1517
import com.stock.data.StockData;
1618
import com.stock.index.StockIndex;
1719

18-
public class CandleImage {
20+
public class CandleImage extends StockPhoto {
21+
22+
public CandleImage(Context context) {
23+
super(context);
24+
}
25+
26+
public CandleImage(Context context, AttributeSet attrs) {
27+
super(context, attrs);
28+
}
1929

2030
private List<PriceBar> bar_list;
2131
private List<StockIndex> indexes = new ArrayList<StockIndex>();
2232

23-
private int step = 8;
2433
private int trans = 0; ///< transform the initialize position of candle image to left
2534
private int scoll = 0; ///< scoll screen
26-
private float scale = .8f;
27-
private int background = Color.BLACK;
2835

29-
public CandleImage(StockData data) {
30-
this.bar_list = data.getBarSet();
31-
}
36+
// public CandleImage(StockData data) {
37+
// this.bar_list = data.getBarSet();
38+
// }
3239

3340
public void AddIndex(StockIndex index) {
3441
index.calcIndex(bar_list);
@@ -169,27 +176,4 @@ private void drawCandle(Canvas g, Paint p, PriceBar[] bars, Rect rect, double hi
169176
}
170177
}
171178

172-
private void drawVolume(Canvas g, Paint p, PriceBar[] bars, Rect rect, double maxv) {
173-
int delta = step / 8, bar_width = step * 3 / 4;
174-
int x = rect.width() - step + delta, bottom = rect.top + rect.height();
175-
double vhp = rect.height() / maxv * 0.8f;
176-
177-
// ((Graphics2D)g).setStroke(new BasicStroke(1));
178-
179-
for( int i = 0; i < bars.length; i ++ ) {
180-
PriceBar curr = bars[i];
181-
182-
if( curr.close > curr.open )
183-
p.setColor(Color.RED);
184-
else
185-
p.setColor(Color.GREEN);
186-
187-
int vh = (int) Math.round( curr.volume * vhp );
188-
p.setStyle(Paint.Style.FILL);
189-
g.drawRect(x, bottom - vh, bar_width, vh, p);
190-
191-
x -= step;
192-
}
193-
}
194-
195179
}

src/com/stock/view/StockPhoto.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.stock.view;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Color;
6+
import android.graphics.Paint;
7+
import android.graphics.Rect;
8+
import android.util.AttributeSet;
9+
import android.view.View;
10+
11+
import com.stock.data.PriceBar;
12+
13+
public abstract class StockPhoto extends View {
14+
15+
public StockPhoto(Context context) {
16+
super(context);
17+
}
18+
19+
public StockPhoto(Context context, AttributeSet attrs) {
20+
super(context, attrs);
21+
}
22+
23+
protected int step = 8;
24+
protected float scale = .8f;
25+
protected int background = Color.BLACK;
26+
27+
protected void drawVolume(Canvas _canvas, Paint _paint, PriceBar[] bars,
28+
Rect rect, double maxv) {
29+
int delta = step / 8, bar_width = step * 3 / 4;
30+
int x = rect.width() - step + delta, bottom = rect.top + rect.height();
31+
double vhp = rect.height() / maxv * 0.8f;
32+
33+
// ((Graphics2D)g).setStroke(new BasicStroke(1));
34+
35+
_paint.setStyle(Paint.Style.FILL);
36+
for( int i = 0; i < bars.length; i ++ ) {
37+
PriceBar curr = bars[i];
38+
39+
if( curr.close > curr.open )
40+
_paint.setColor(Color.RED);
41+
else
42+
_paint.setColor(Color.GREEN);
43+
44+
int vh = (int) Math.round( curr.volume * vhp );
45+
_canvas.drawRect(x, bottom - vh, bar_width, vh, _paint);
46+
47+
x -= step;
48+
}
49+
}
50+
51+
}

0 commit comments

Comments
 (0)