77import android .graphics .Rect ;
88import android .util .AttributeSet ;
99import android .view .View ;
10-
1110import java .util .ArrayList ;
1211
1312/**
1413 * Created by Dacer on 11/11/13.
1514 */
1615public class BarView extends View {
16+ private final int MINI_BAR_WIDTH ;
17+ private final int BAR_SIDE_MARGIN ;
18+ private final int TEXT_TOP_MARGIN ;
19+ private final int TEXT_COLOR = Color .parseColor ("#9B9A9B" );
20+ private final int BACKGROUND_COLOR = Color .parseColor ("#F6F6F6" );
21+ private final int FOREGROUND_COLOR = Color .parseColor ("#FC496D" );
1722 private ArrayList <Float > percentList ;
1823 private ArrayList <Float > targetPercentList ;
1924 private Paint textPaint ;
2025 private Paint bgPaint ;
2126 private Paint fgPaint ;
2227 private Rect rect ;
2328 private int barWidth ;
24- // private boolean showSideMargin = true;
2529 private int bottomTextDescent ;
2630 private boolean autoSetWidth = true ;
2731 private int topMargin ;
2832 private int bottomTextHeight ;
2933 private ArrayList <String > bottomTextList = new ArrayList <String >();
30- private final int MINI_BAR_WIDTH ;
31- private final int BAR_SIDE_MARGIN ;
32- private final int TEXT_TOP_MARGIN ;
33- private final int TEXT_COLOR = Color .parseColor ("#9B9A9B" );
34- private final int BACKGROUND_COLOR = Color .parseColor ("#F6F6F6" );
35- private final int FOREGROUND_COLOR = Color .parseColor ("#FC496D" );
36-
3734 private Runnable animator = new Runnable () {
38- @ Override
39- public void run () {
40- boolean needNewFrame = false ;
41- for (int i =0 ; i <targetPercentList .size ();i ++) {
42- if (percentList .get (i ) < targetPercentList .get (i )) {
43- percentList .set (i ,percentList .get (i )+0.02f );
44- needNewFrame = true ;
45- } else if (percentList .get (i ) > targetPercentList .get (i )){
46- percentList .set (i ,percentList .get (i )-0.02f );
47- needNewFrame = true ;
48- }
49- if (Math .abs (targetPercentList .get (i )-percentList .get (i ))<0.02f ){
50- percentList .set (i ,targetPercentList .get (i ));
51- }
35+ @ Override public void run () {
36+ boolean needNewFrame = false ;
37+ for (int i = 0 ; i < targetPercentList .size (); i ++) {
38+ if (percentList .get (i ) < targetPercentList .get (i )) {
39+ percentList .set (i , percentList .get (i ) + 0.02f );
40+ needNewFrame = true ;
41+ } else if (percentList .get (i ) > targetPercentList .get (i )) {
42+ percentList .set (i , percentList .get (i ) - 0.02f );
43+ needNewFrame = true ;
5244 }
53- if (needNewFrame ) {
54- postDelayed ( this , 20 );
45+ if (Math . abs ( targetPercentList . get ( i ) - percentList . get ( i )) < 0.02f ) {
46+ percentList . set ( i , targetPercentList . get ( i ) );
5547 }
56- invalidate ();
48+ }
49+ if (needNewFrame ) {
50+ postDelayed (this , 20 );
51+ }
52+ invalidate ();
5753 }
5854 };
5955
60- public BarView (Context context ){
61- this (context ,null );
56+ public BarView (Context context ) {
57+ this (context , null );
6258 }
63- public BarView (Context context , AttributeSet attrs ){
59+
60+ public BarView (Context context , AttributeSet attrs ) {
6461 super (context , attrs );
6562 bgPaint = new Paint ();
6663 bgPaint .setAntiAlias (true );
@@ -70,9 +67,9 @@ public BarView(Context context, AttributeSet attrs){
7067 rect = new Rect ();
7168 topMargin = MyUtils .dip2px (context , 5 );
7269 int textSize = MyUtils .sp2px (context , 15 );
73- barWidth = MyUtils .dip2px (context ,22 );
74- MINI_BAR_WIDTH = MyUtils .dip2px (context ,22 );
75- BAR_SIDE_MARGIN = MyUtils .dip2px (context ,22 );
70+ barWidth = MyUtils .dip2px (context , 22 );
71+ MINI_BAR_WIDTH = MyUtils .dip2px (context , 22 );
72+ BAR_SIDE_MARGIN = MyUtils .dip2px (context , 22 );
7673 TEXT_TOP_MARGIN = MyUtils .dip2px (context , 5 );
7774 textPaint = new Paint ();
7875 textPaint .setAntiAlias (true );
@@ -84,23 +81,24 @@ public BarView(Context context, AttributeSet attrs){
8481
8582 /**
8683 * dataList will be reset when called is method.
84+ *
8785 * @param bottomStringList The String ArrayList in the bottom.
8886 */
89- public void setBottomTextList (ArrayList <String > bottomStringList ){
90- // this.dataList = null;
87+ public void setBottomTextList (ArrayList <String > bottomStringList ) {
88+ // this.dataList = null;
9189 this .bottomTextList = bottomStringList ;
9290 Rect r = new Rect ();
9391 bottomTextDescent = 0 ;
9492 barWidth = MINI_BAR_WIDTH ;
95- for (String s : bottomTextList ){
96- textPaint .getTextBounds (s ,0 , s .length (),r );
97- if (bottomTextHeight < r .height ()){
93+ for (String s : bottomTextList ) {
94+ textPaint .getTextBounds (s , 0 , s .length (), r );
95+ if (bottomTextHeight < r .height ()) {
9896 bottomTextHeight = r .height ();
9997 }
100- if (autoSetWidth && (barWidth < r .width ())){
98+ if (autoSetWidth && (barWidth < r .width ())) {
10199 barWidth = r .width ();
102100 }
103- if (bottomTextDescent < (Math .abs (r .bottom ))){
101+ if (bottomTextDescent < (Math .abs (r .bottom ))) {
104102 bottomTextDescent = Math .abs (r .bottom );
105103 }
106104 }
@@ -109,95 +107,93 @@ public void setBottomTextList(ArrayList<String> bottomStringList){
109107 }
110108
111109 /**
112- *
113110 * @param list The ArrayList of Integer with the range of [0-max].
114111 */
115- public void setDataList (ArrayList <Integer > list , int max ){
112+ public void setDataList (ArrayList <Integer > list , int max ) {
116113 targetPercentList = new ArrayList <Float >();
117- if (max == 0 ) max = 1 ;
114+ if (max == 0 ) max = 1 ;
118115
119- for (Integer integer : list ){
120- targetPercentList .add (1 - (float )integer / (float )max );
116+ for (Integer integer : list ) {
117+ targetPercentList .add (1 - (float ) integer / (float ) max );
121118 }
122119
123120 // Make sure percentList.size() == targetPercentList.size()
124- if (percentList .isEmpty () || percentList .size ()< targetPercentList .size ()){
125- int temp = targetPercentList .size ()- percentList .size ();
126- for (int i = 0 ; i < temp ;i ++){
121+ if (percentList .isEmpty () || percentList .size () < targetPercentList .size ()) {
122+ int temp = targetPercentList .size () - percentList .size ();
123+ for (int i = 0 ; i < temp ; i ++) {
127124 percentList .add (1f );
128125 }
129- } else if (percentList .size ()> targetPercentList .size ()){
130- int temp = percentList .size ()- targetPercentList .size ();
131- for (int i = 0 ; i < temp ;i ++){
132- percentList .remove (percentList .size ()- 1 );
126+ } else if (percentList .size () > targetPercentList .size ()) {
127+ int temp = percentList .size () - targetPercentList .size ();
128+ for (int i = 0 ; i < temp ; i ++) {
129+ percentList .remove (percentList .size () - 1 );
133130 }
134131 }
135132 setMinimumWidth (2 );
136133 removeCallbacks (animator );
137134 post (animator );
138135 }
139136
140- @ Override
141- protected void onDraw (Canvas canvas ) {
137+ @ Override protected void onDraw (Canvas canvas ) {
142138 int i = 1 ;
143- if (percentList != null && !percentList .isEmpty ()){
144- for (Float f :percentList ){
145- rect .set (BAR_SIDE_MARGIN *i +barWidth *(i -1 ),
146- topMargin ,
147- (BAR_SIDE_MARGIN +barWidth )* i ,
148- getHeight ()-bottomTextHeight -TEXT_TOP_MARGIN );
149- canvas .drawRect (rect ,bgPaint );
139+ if (percentList != null && !percentList .isEmpty ()) {
140+ for (Float f : percentList ) {
141+ rect .set (BAR_SIDE_MARGIN * i + barWidth * (i - 1 ), topMargin ,
142+ (BAR_SIDE_MARGIN + barWidth ) * i ,
143+ getHeight () - bottomTextHeight - TEXT_TOP_MARGIN );
144+ canvas .drawRect (rect , bgPaint );
150145 /*rect.set(BAR_SIDE_MARGIN*i+barWidth*(i-1),
151146 topMargin+(int)((getHeight()-topMargin)*percentList.get(i-1)),
152147 (BAR_SIDE_MARGIN+barWidth)* i,
153148 getHeight()-bottomTextHeight-TEXT_TOP_MARGIN);*/
154- /**
155- * The correct total height is "getHeight()-topMargin-bottomTextHeight-TEXT_TOP_MARGIN",not "getHeight()-topMargin".
156- * fix by [email protected] on 11/11/13. 157- */
158- rect .set (BAR_SIDE_MARGIN *i +barWidth *(i -1 ),
159- topMargin +(int )((getHeight ()-topMargin -bottomTextHeight -TEXT_TOP_MARGIN )*percentList .get (i -1 )),
160- (BAR_SIDE_MARGIN +barWidth )* i ,
161- getHeight ()-bottomTextHeight -TEXT_TOP_MARGIN );
162- canvas .drawRect (rect ,fgPaint );
149+ /**
150+ * The correct total height is "getHeight()-topMargin-bottomTextHeight-TEXT_TOP_MARGIN",not "getHeight()-topMargin".
151+ * fix by [email protected] on 11/11/13. 152+ */
153+ rect .set (BAR_SIDE_MARGIN * i + barWidth * (i - 1 ), topMargin + (int ) ((getHeight ()
154+ - topMargin
155+ - bottomTextHeight
156+ - TEXT_TOP_MARGIN ) * percentList .get (i - 1 )),
157+ (BAR_SIDE_MARGIN + barWidth ) * i ,
158+ getHeight () - bottomTextHeight - TEXT_TOP_MARGIN );
159+ canvas .drawRect (rect , fgPaint );
163160 i ++;
164161 }
165162 }
166163
167- if (bottomTextList != null && !bottomTextList .isEmpty ()){
164+ if (bottomTextList != null && !bottomTextList .isEmpty ()) {
168165 i = 1 ;
169- for (String s : bottomTextList ){
170- canvas .drawText (s ,BAR_SIDE_MARGIN * i + barWidth *( i - 1 )+ barWidth / 2 ,
171- getHeight ()- bottomTextDescent ,textPaint );
166+ for (String s : bottomTextList ) {
167+ canvas .drawText (s , BAR_SIDE_MARGIN * i + barWidth * ( i - 1 ) + barWidth / 2 ,
168+ getHeight () - bottomTextDescent , textPaint );
172169 i ++;
173170 }
174171 }
175172 }
176173
177- @ Override
178- protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
174+ @ Override protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
179175 int mViewWidth = measureWidth (widthMeasureSpec );
180176 int mViewHeight = measureHeight (heightMeasureSpec );
181- setMeasuredDimension (mViewWidth ,mViewHeight );
177+ setMeasuredDimension (mViewWidth , mViewHeight );
182178 }
183179
184- private int measureWidth (int measureSpec ){
180+ private int measureWidth (int measureSpec ) {
185181 int preferred = 0 ;
186- if (bottomTextList != null ){
187- preferred = bottomTextList .size ()* (barWidth + BAR_SIDE_MARGIN );
182+ if (bottomTextList != null ) {
183+ preferred = bottomTextList .size () * (barWidth + BAR_SIDE_MARGIN );
188184 }
189185 return getMeasurement (measureSpec , preferred );
190186 }
191187
192- private int measureHeight (int measureSpec ){
188+ private int measureHeight (int measureSpec ) {
193189 int preferred = 222 ;
194190 return getMeasurement (measureSpec , preferred );
195191 }
196192
197- private int getMeasurement (int measureSpec , int preferred ){
193+ private int getMeasurement (int measureSpec , int preferred ) {
198194 int specSize = MeasureSpec .getSize (measureSpec );
199195 int measurement ;
200- switch (MeasureSpec .getMode (measureSpec )){
196+ switch (MeasureSpec .getMode (measureSpec )) {
201197 case MeasureSpec .EXACTLY :
202198 measurement = specSize ;
203199 break ;
@@ -210,5 +206,4 @@ private int getMeasurement(int measureSpec, int preferred){
210206 }
211207 return measurement ;
212208 }
213-
214209}
0 commit comments