@@ -28,8 +28,6 @@ public class LineView extends View {
2828 private boolean autoSetGridWidth = true ;
2929 private int dataOfAGird = 10 ;
3030 private int bottomTextHeight = 0 ;
31- // private float backgroundGridHeight;
32- private int horizontalGridNum ;
3331 private ArrayList <String > bottomTextList ;
3432 private ArrayList <Integer > dataList ;
3533 private ArrayList <Integer > xCoordinateList = new ArrayList <Integer >();
@@ -90,10 +88,7 @@ public LineView(Context context, AttributeSet attrs){
9088 public void setBottomTextList (ArrayList <String > bottomTextList ){
9189 this .dataList = null ;
9290 this .bottomTextList = bottomTextList ;
93- horizontalGridNum = bottomTextList .size ()-1 ;
94- if (horizontalGridNum <MIN_HORIZONTAL_GRID_NUM ){
95- horizontalGridNum = MIN_HORIZONTAL_GRID_NUM ;
96- }
91+
9792 Rect r = new Rect ();
9893 int longestWidth = 0 ;
9994 String longestStr = "" ;
@@ -111,6 +106,7 @@ public void setBottomTextList(ArrayList<String> bottomTextList){
111106 bottomTextDescent = Math .abs (r .bottom );
112107 }
113108 }
109+
114110 if (autoSetGridWidth ){
115111 if (backgroundGridWidth <longestWidth ){
116112 backgroundGridWidth = longestWidth +(int )bottomTextPaint .measureText (longestStr ,0 ,1 );
@@ -119,7 +115,8 @@ public void setBottomTextList(ArrayList<String> bottomTextList){
119115 sideLineLength = longestWidth /2 ;
120116 }
121117 }
122- refreshView ();
118+
119+ refreshXCoordinateList (getHorizontalGridNum ());
123120 }
124121
125122 /**
@@ -145,32 +142,54 @@ public void setDataList(ArrayList<Integer> dataList){
145142 dataOfAGird *= 10 ;
146143 }
147144 }
148-
149- refreshView ();
145+ int verticaGridNum = getVerticaGridlNum ();
146+ refreshYCoordinateList (verticaGridNum );
147+ refreshDrawDotList (verticaGridNum );
148+ refreshTopLineLength (verticaGridNum );
149+ showPopup = false ;
150+ setMinimumWidth (0 ); // It can help the LineView reset the Width,
151+ // I don't know the better way..
152+ postInvalidate ();
150153 }
151154
152- private void refreshView (){
153- int verticalGridNum = getVerticaGridlNum ();
154- // For prevent popup can't be completely showed when backgroundGridHeight is too small.
155- // But this code not so good.
156- if ((getHeight ()-topLineLength -bottomTextHeight -bottomTextTopMargin )/
157- (verticalGridNum +2 )<getPopupHeight ()){
158- topLineLength = getPopupHeight ()+DOT_OUTER_CIR_RADIUS +DOT_INNER_CIR_RADIUS +2 ;
159- }else {
160- topLineLength = MIN_TOP_LINE_LENGTH ;
155+ private int getVerticaGridlNum (){
156+ int verticalGridNum = MIN_VERTICAL_GRID_NUM ;
157+ if (dataList != null && !dataList .isEmpty ()){
158+ for (Integer integer :dataList ){
159+ if (verticalGridNum <(integer +1 )){
160+ verticalGridNum = integer +1 ;
161+ }
162+ }
163+ }
164+ return verticalGridNum ;
165+ }
166+
167+ private int getHorizontalGridNum (){
168+ int horizontalGridNum = bottomTextList .size ()-1 ;
169+ if (horizontalGridNum <MIN_HORIZONTAL_GRID_NUM ){
170+ horizontalGridNum = MIN_HORIZONTAL_GRID_NUM ;
161171 }
172+ return horizontalGridNum ;
173+ }
162174
175+ private void refreshXCoordinateList (int horizontalGridNum ){
163176 xCoordinateList .clear ();
164- yCoordinateList .clear ();
165177 for (int i =0 ;i <(horizontalGridNum +1 );i ++){
166178 xCoordinateList .add (sideLineLength + backgroundGridWidth *i );
167179 }
180+
181+ }
182+
183+ private void refreshYCoordinateList (int verticalGridNum ){
184+ yCoordinateList .clear ();
168185 for (int i =0 ;i <(verticalGridNum +1 );i ++){
169186 yCoordinateList .add (topLineLength +
170187 ((getHeight ()-topLineLength -bottomTextHeight -bottomTextTopMargin -
171- bottomLineLength -bottomTextDescent )*i /(verticalGridNum )));
188+ bottomLineLength -bottomTextDescent )*i /(verticalGridNum )));
172189 }
190+ }
173191
192+ private void refreshDrawDotList (int verticalGridNum ){
174193 drawDotList = new ArrayList <Dot >();
175194 if (dataList != null && !dataList .isEmpty ()){
176195 for (int i =0 ;i <dataList .size ();i ++){
@@ -179,23 +198,17 @@ private void refreshView(){
179198 drawDotList .add (new Dot (x ,y ,dataList .get (i )));
180199 }
181200 }
182-
183- showPopup = false ;
184- setMinimumWidth (0 ); // It can help the LineView reset the Width,
185- // I don't know the better way..
186- postInvalidate ();
187201 }
188202
189- private int getVerticaGridlNum ( ){
190- int verticalGridNum = MIN_VERTICAL_GRID_NUM ;
191- if ( dataList != null && ! dataList . isEmpty ()){
192- for ( Integer integer : dataList ){
193- if (verticalGridNum <( integer + 1 )){
194- verticalGridNum = integer + 1 ;
195- }
196- }
203+ private void refreshTopLineLength ( int verticalGridNum ){
204+ // For prevent popup can't be completely showed when backgroundGridHeight is too small.
205+ // But this code not so good.
206+ if (( getHeight ()- topLineLength - bottomTextHeight - bottomTextTopMargin )/
207+ (verticalGridNum + 2 )< getPopupHeight ( )){
208+ topLineLength = getPopupHeight ()+ DOT_OUTER_CIR_RADIUS + DOT_INNER_CIR_RADIUS + 2 ;
209+ } else {
210+ topLineLength = MIN_TOP_LINE_LENGTH ;
197211 }
198- return verticalGridNum ;
199212 }
200213
201214 @ Override
@@ -322,11 +335,15 @@ private void drawBackgroundLines(Canvas canvas){
322335 protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
323336 int mViewWidth = measureWidth (widthMeasureSpec );
324337 int mViewHeight = measureHeight (heightMeasureSpec );
325- refreshView ();
338+ int verticaGridNum = getVerticaGridlNum ();
339+ refreshYCoordinateList (verticaGridNum );
340+ refreshDrawDotList (verticaGridNum );
341+ refreshTopLineLength (verticaGridNum );
326342 setMeasuredDimension (mViewWidth ,mViewHeight );
327343 }
328344
329345 private int measureWidth (int measureSpec ){
346+ int horizontalGridNum = getHorizontalGridNum ();
330347 int preferred = backgroundGridWidth *horizontalGridNum +sideLineLength *2 ;
331348 return getMeasurement (measureSpec , preferred );
332349 }
0 commit comments