|
| 1 | +package com.stock.drawing; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.regex.Matcher; |
| 6 | +import java.util.regex.Pattern; |
| 7 | + |
| 8 | +import com.stock.data.StockData; |
| 9 | +import com.stock.source.DataSource; |
| 10 | +import com.stock.turtle.R; |
| 11 | + |
| 12 | +import android.content.Context; |
| 13 | +import android.content.res.TypedArray; |
| 14 | +import android.graphics.Canvas; |
| 15 | +import android.util.AttributeSet; |
| 16 | +import android.view.View; |
| 17 | + |
| 18 | +public class BrokenLineView extends View { |
| 19 | + |
| 20 | + List<StockData> stocks = new ArrayList<StockData>(); |
| 21 | + |
| 22 | + public BrokenLineView(Context context) { |
| 23 | + super(context); |
| 24 | + stocks.add(new StockData("600036", DataSource.MARKET_SHANGHAI)); |
| 25 | + } |
| 26 | + |
| 27 | + public BrokenLineView(Context context, AttributeSet attrs){ |
| 28 | + super(context, attrs); |
| 29 | + |
| 30 | + //TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组 |
| 31 | + //在使用完成后,一定要调用recycle方法 |
| 32 | + //属性的名称是styleable中的名称+“_”+属性名称 |
| 33 | + TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BrokenLineView); |
| 34 | + String shstocks = array.getString(R.styleable.BrokenLineView_sh_stocks); |
| 35 | + String szstocks = array.getString(R.styleable.BrokenLineView_sz_stocks); |
| 36 | + array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响 |
| 37 | + |
| 38 | + Pattern pattern = Pattern.compile("([\\d]{6})"); |
| 39 | + if( shstocks != null && shstocks.length() > 0 ) { |
| 40 | + Matcher mCells = pattern.matcher(shstocks); |
| 41 | + while( mCells.find() ) { |
| 42 | + StockData stock = new StockData(mCells.group(), DataSource.MARKET_SHANGHAI); |
| 43 | + stocks.add(stock); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if( szstocks != null && szstocks.length() > 0 ) { |
| 48 | + Matcher mCells = pattern.matcher(szstocks); |
| 49 | + while( mCells.find() ) { |
| 50 | + StockData stock = new StockData(mCells.group(), DataSource.MARKET_SHENZHEN); |
| 51 | + stocks.add(stock); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if( stocks.size() == 0 ) |
| 56 | + stocks.add(new StockData("600036", DataSource.MARKET_SHANGHAI)); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected void onDraw(Canvas canvas) { |
| 61 | + super.onDraw(canvas); |
| 62 | + } |
| 63 | +} |
0 commit comments