46
46
* Provides a widget for enter PIN/OTP/password etc.
47
47
*
48
48
* @author Chaos Leong
49
- * 01/04/2017
49
+ * 01/04/2017
50
50
*/
51
51
public class PinView extends AppCompatEditText {
52
52
@@ -67,8 +67,8 @@ public class PinView extends AppCompatEditText {
67
67
68
68
private int mPinItemCount ;
69
69
70
- private float mPinItemWidth ;
71
- private float mPinItemHeight ;
70
+ private int mPinItemWidth ;
71
+ private int mPinItemHeight ;
72
72
private int mPinItemRadius ;
73
73
private int mPinItemSpacing ;
74
74
@@ -125,14 +125,14 @@ public PinView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
125
125
126
126
mViewType = a .getInt (R .styleable .PinView_viewType , VIEW_TYPE_RECTANGLE );
127
127
mPinItemCount = a .getInt (R .styleable .PinView_itemCount , DEFAULT_COUNT );
128
- mPinItemHeight = a . getDimensionPixelSize (R .styleable .PinView_itemHeight ,
128
+ mPinItemHeight = ( int ) a . getDimension (R .styleable .PinView_itemHeight ,
129
129
res .getDimensionPixelSize (R .dimen .pv_pin_view_item_size ));
130
- mPinItemWidth = a . getDimensionPixelSize (R .styleable .PinView_itemWidth ,
130
+ mPinItemWidth = ( int ) a . getDimension (R .styleable .PinView_itemWidth ,
131
131
res .getDimensionPixelSize (R .dimen .pv_pin_view_item_size ));
132
132
mPinItemSpacing = a .getDimensionPixelSize (R .styleable .PinView_itemSpacing ,
133
133
res .getDimensionPixelSize (R .dimen .pv_pin_view_item_spacing ));
134
- mPinItemRadius = a . getDimensionPixelSize (R .styleable .PinView_itemRadius , 0 );
135
- mLineWidth = a . getDimensionPixelSize (R .styleable .PinView_lineWidth ,
134
+ mPinItemRadius = ( int ) a . getDimension (R .styleable .PinView_itemRadius , 0 );
135
+ mLineWidth = ( int ) a . getDimension (R .styleable .PinView_lineWidth ,
136
136
res .getDimensionPixelSize (R .dimen .pv_pin_view_item_line_width ));
137
137
mLineColor = a .getColorStateList (R .styleable .PinView_lineColor );
138
138
isCursorVisible = a .getBoolean (R .styleable .PinView_android_cursorVisible , true );
@@ -183,14 +183,14 @@ public void onAnimationUpdate(ValueAnimator animation) {
183
183
184
184
private void checkItemRadius () {
185
185
if (mViewType == VIEW_TYPE_LINE ) {
186
- int halfOfLineWidth = mLineWidth / 2 ;
186
+ float halfOfLineWidth = (( float ) mLineWidth ) / 2 ;
187
187
if (mPinItemRadius > halfOfLineWidth ) {
188
- throw new RuntimeException ("The itemRadius can not be greater than lineWidth when viewType is line" );
188
+ throw new IllegalArgumentException ("The itemRadius can not be greater than lineWidth when viewType is line" );
189
189
}
190
190
}
191
- int halfOfItemWidth = (int ) ( mPinItemWidth / 2 ) ;
191
+ float halfOfItemWidth = (( float ) mPinItemWidth ) / 2 ;
192
192
if (mPinItemRadius > halfOfItemWidth ) {
193
- throw new RuntimeException ("The itemRadius can not be greater than itemWidth" );
193
+ throw new IllegalArgumentException ("The itemRadius can not be greater than itemWidth" );
194
194
}
195
195
}
196
196
@@ -204,14 +204,14 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
204
204
int width ;
205
205
int height ;
206
206
207
- float boxHeight = mPinItemHeight ;
207
+ int boxHeight = mPinItemHeight ;
208
208
209
209
if (widthMode == MeasureSpec .EXACTLY ) {
210
210
// Parent has told us how big to be. So be it.
211
211
width = widthSize ;
212
212
} else {
213
- float boxesWidth = (mPinItemCount - 1 ) * mPinItemSpacing + mPinItemCount * mPinItemWidth ;
214
- width = Math . round ( boxesWidth + ViewCompat .getPaddingEnd (this ) + ViewCompat .getPaddingStart (this ) );
213
+ int boxesWidth = (mPinItemCount - 1 ) * mPinItemSpacing + mPinItemCount * mPinItemWidth ;
214
+ width = boxesWidth + ViewCompat .getPaddingEnd (this ) + ViewCompat .getPaddingStart (this );
215
215
if (mPinItemSpacing == 0 ) {
216
216
width -= (mPinItemCount - 1 ) * mLineWidth ;
217
217
}
@@ -221,7 +221,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
221
221
// Parent has told us how big to be. So be it.
222
222
height = heightSize ;
223
223
} else {
224
- height = Math . round ( boxHeight + getPaddingTop () + getPaddingBottom () );
224
+ height = boxHeight + getPaddingTop () + getPaddingBottom ();
225
225
}
226
226
227
227
setMeasuredDimension (width , height );
@@ -377,7 +377,7 @@ private void drawPinLine(Canvas canvas, int i) {
377
377
}
378
378
mPaint .setStyle (Paint .Style .FILL );
379
379
mPaint .setStrokeWidth (((float ) mLineWidth ) / 10 );
380
- float halfLineWidth = (float ) mLineWidth / 2 ;
380
+ float halfLineWidth = (( float ) mLineWidth ) / 2 ;
381
381
mItemLineRect .set (mItemBorderRect .left , mItemBorderRect .bottom - halfLineWidth , mItemBorderRect .right , mItemBorderRect .bottom + halfLineWidth );
382
382
383
383
updateRoundRectPath (mItemLineRect , mPinItemRadius , mPinItemRadius , l , r );
@@ -464,7 +464,7 @@ private void updateRoundRectPath(RectF rectF, float rx, float ry,
464
464
}
465
465
466
466
private void updateItemRectF (int i ) {
467
- float halfLineWidth = (float ) mLineWidth / 2 ;
467
+ float halfLineWidth = (( float ) mLineWidth ) / 2 ;
468
468
float left = getScrollX () + ViewCompat .getPaddingStart (this ) + i * (mPinItemSpacing + mPinItemWidth ) + halfLineWidth ;
469
469
if (mPinItemSpacing == 0 && i > 0 ) {
470
470
left = left - (mLineWidth ) * i ;
@@ -497,8 +497,8 @@ private void drawTextAtBox(Canvas canvas, Paint paint, CharSequence text, int ch
497
497
paint .getTextBounds (text .toString (), charAt , charAt + 1 , mTextRect );
498
498
float cx = mItemCenterPoint .x ;
499
499
float cy = mItemCenterPoint .y ;
500
- float x = cx - Math .abs (mTextRect .width ()) / 2 - mTextRect .left ;
501
- float y = cy + Math .abs (mTextRect .height ()) / 2 - mTextRect .bottom ;// always center vertical
500
+ float x = cx - Math .abs (( float ) mTextRect .width ()) / 2 - mTextRect .left ;
501
+ float y = cy + Math .abs (( float ) mTextRect .height ()) / 2 - mTextRect .bottom ;// always center vertical
502
502
canvas .drawText (text , charAt , charAt + 1 , x , y , paint );
503
503
}
504
504
@@ -654,7 +654,6 @@ public void setLineWidth(@Px int borderWidth) {
654
654
* @return Returns the width of the item's line.
655
655
* @see #setLineWidth(int)
656
656
*/
657
- @ Px
658
657
public int getLineWidth () {
659
658
return mLineWidth ;
660
659
}
@@ -695,7 +694,6 @@ public void setItemRadius(@Px int itemRadius) {
695
694
* @return Returns the radius of square.
696
695
* @see #setItemRadius(int)
697
696
*/
698
- @ Px
699
697
public int getItemRadius () {
700
698
return mPinItemRadius ;
701
699
}
@@ -726,17 +724,17 @@ public int getItemSpacing() {
726
724
* @attr ref R.styleable#PinView_itemHeight
727
725
* @see #getItemHeight()
728
726
*/
729
- public void setItemHeight (float itemHeight ) {
727
+ public void setItemHeight (@ Px int itemHeight ) {
730
728
mPinItemHeight = itemHeight ;
731
729
updateCursorHeight ();
732
730
requestLayout ();
733
731
}
734
732
735
733
/**
736
734
* @return Returns the height of item.
737
- * @see #setItemHeight(float )
735
+ * @see #setItemHeight(int )
738
736
*/
739
- public float getItemHeight () {
737
+ public int getItemHeight () {
740
738
return mPinItemHeight ;
741
739
}
742
740
@@ -746,17 +744,17 @@ public float getItemHeight() {
746
744
* @attr ref R.styleable#PinView_itemWidth
747
745
* @see #getItemWidth()
748
746
*/
749
- public void setItemWidth (float itemWidth ) {
747
+ public void setItemWidth (@ Px int itemWidth ) {
750
748
mPinItemWidth = itemWidth ;
751
749
checkItemRadius ();
752
750
requestLayout ();
753
751
}
754
752
755
753
/**
756
754
* @return Returns the width of item.
757
- * @see #setItemWidth(float )
755
+ * @see #setItemWidth(int )
758
756
*/
759
- public float getItemWidth () {
757
+ public int getItemWidth () {
760
758
return mPinItemWidth ;
761
759
}
762
760
0 commit comments