diff --git a/build.gradle b/build.gradle index 9eb3ced..36cf59a 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.1' + classpath 'com.android.tools.build:gradle:2.3.3' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' // NOTE: Do not place your application dependencies here; they belong diff --git a/pinview/src/main/java/com/chaos/view/PinView.java b/pinview/src/main/java/com/chaos/view/PinView.java index 547adc1..5ca207e 100644 --- a/pinview/src/main/java/com/chaos/view/PinView.java +++ b/pinview/src/main/java/com/chaos/view/PinView.java @@ -45,21 +45,7 @@ * * @author Chaos Leong * 01/04/2017 - * - *

- * XML attributes - *

- * See EditText Attributes, - * TextView Attributes, - * View Attributes - * @attr ref R.styleable#PinView_boxCount - * @attr ref R.styleable#PinView_boxHeight - * @attr ref R.styleable#PinView_boxRadius - * @attr ref R.styleable#PinView_boxMargin - * @attr ref R.styleable#PinView_borderWidth - * @attr ref R.styleable#PinView_borderColor */ - public class PinView extends AppCompatEditText { private static final String TAG = "PinView"; @@ -70,11 +56,11 @@ public class PinView extends AppCompatEditText { private static final InputFilter[] NO_FILTERS = new InputFilter[0]; - private int mPinBoxCount; + private int mPinItemCount; - private float mPinBoxHeight; - private int mPinBoxRadius; - private int mPinBoxMargin; + private float mPinItemSize; + private int mPinItemRadius; + private int mPinItemSpacing; private final Paint mPaint; private final TextPaint mTextPaint; @@ -119,20 +105,37 @@ public PinView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) TypedArray a = theme.obtainStyledAttributes(attrs, R.styleable.PinView, defStyleAttr, 0); - mPinBoxCount = a.getInt(R.styleable.PinView_boxCount, DEFAULT_COUNT); - mPinBoxHeight = a.getDimensionPixelSize(R.styleable.PinView_boxHeight, - res.getDimensionPixelOffset(R.dimen.pv_pin_view_box_height)); - mPinBoxMargin = a.getDimensionPixelOffset(R.styleable.PinView_boxMargin, - res.getDimensionPixelOffset(R.dimen.pv_pin_view_box_margin)); + mPinItemCount = a.getInt(R.styleable.PinView_boxCount, DEFAULT_COUNT); + mPinItemSize = a.getDimensionPixelSize(R.styleable.PinView_boxHeight, + res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_size)); + mPinItemSpacing = a.getDimensionPixelOffset(R.styleable.PinView_boxMargin, + res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_spacing)); + mPinItemRadius = a.getDimensionPixelOffset(R.styleable.PinView_boxRadius, + res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_radius)); + + if (a.hasValue(R.styleable.PinView_itemCount)) { + mPinItemCount = a.getInt(R.styleable.PinView_itemCount, DEFAULT_COUNT); + } + if (a.hasValue(R.styleable.PinView_itemSize)) { + mPinItemSize = a.getDimensionPixelSize(R.styleable.PinView_itemSize, + res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_size)); + } + if (a.hasValue(R.styleable.PinView_itemSpacing)) { + mPinItemSpacing = a.getDimensionPixelOffset(R.styleable.PinView_itemSpacing, + res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_spacing)); + } + if (a.hasValue(R.styleable.PinView_itemRadius)) { + mPinItemRadius = a.getDimensionPixelOffset(R.styleable.PinView_itemRadius, + res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_radius)); + } + mBorderWidth = a.getDimensionPixelOffset(R.styleable.PinView_borderWidth, - res.getDimensionPixelOffset(R.dimen.pv_pin_view_box_border_width)); - mPinBoxRadius = a.getDimensionPixelOffset(R.styleable.PinView_boxRadius, - res.getDimensionPixelOffset(R.dimen.pv_pin_view_box_radius)); + res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_border_width)); mBorderColor = a.getColorStateList(R.styleable.PinView_borderColor); a.recycle(); - setMaxLength(mPinBoxCount); + setMaxLength(mPinItemCount); mPaint.setStrokeWidth(mBorderWidth); setupAnimator(); @@ -174,16 +177,16 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width; int height; - float boxHeight = mPinBoxHeight; + float boxHeight = mPinItemSize; if (widthMode == MeasureSpec.EXACTLY) { // Parent has told us how big to be. So be it. width = widthSize; } else { - float boxesWidth = (mPinBoxCount - 1) * mPinBoxMargin + mPinBoxCount * boxHeight; + float boxesWidth = (mPinItemCount - 1) * mPinItemSpacing + mPinItemCount * boxHeight; width = Math.round(boxesWidth + getPaddingRight() + getPaddingLeft()); - if (mPinBoxMargin == 0) { - width -= 2 * (mPinBoxCount - 1) * mBorderWidth; + if (mPinItemSpacing == 0) { + width -= 2 * (mPinItemCount - 1) * mBorderWidth; } } @@ -264,7 +267,7 @@ private void updatePaints() { } private void drawPinView(Canvas canvas) { - for (int i = 0; i < mPinBoxCount; i++) { + for (int i = 0; i < mPinItemCount; i++) { updateBoxRectF(i); updateCenterPoint(); @@ -280,7 +283,7 @@ private void drawPinView(Canvas canvas) { } else { drawText(canvas, i); } - } else if (!TextUtils.isEmpty(getHint()) && getHint().length() == mPinBoxCount) { + } else if (!TextUtils.isEmpty(getHint()) && getHint().length() == mPinItemCount) { drawHint(canvas, i); } } @@ -289,12 +292,12 @@ private void drawPinView(Canvas canvas) { private void drawPinBox(Canvas canvas, int i) { boolean l, r; l = r = true; - if (mPinBoxMargin == 0) { - if (mPinBoxCount > 1) { + if (mPinItemSpacing == 0) { + if (mPinItemCount > 1) { if (i == 0) { // draw only left round r = false; - } else if (i == mPinBoxCount - 1) { + } else if (i == mPinItemCount - 1) { // draw only right round l = false; } else { @@ -303,7 +306,7 @@ private void drawPinBox(Canvas canvas, int i) { } } } - updateRoundRectPath(mBoxBorderRect, mPinBoxRadius, mPinBoxRadius, l, r); + updateRoundRectPath(mBoxBorderRect, mPinItemRadius, mPinItemRadius, l, r); canvas.drawPath(mPath, mPaint); } @@ -368,17 +371,17 @@ private void updateRoundRectPath(RectF rectF, float rx, float ry, } private void updateBoxRectF(int i) { - float startX = (getWidth() - (mPinBoxCount - 1) * mPinBoxMargin - mPinBoxCount * mPinBoxHeight) / 2; - if (mPinBoxMargin == 0) { - startX += (mPinBoxCount - 1) * mBorderWidth; + float startX = (getWidth() - (mPinItemCount - 1) * mPinItemSpacing - mPinItemCount * mPinItemSize) / 2; + if (mPinItemSpacing == 0) { + startX += (mPinItemCount - 1) * mBorderWidth; } - float left = startX + mPinBoxHeight * i + mPinBoxMargin * i + mBorderWidth; - if (mPinBoxMargin == 0 && i > 0) { + float left = startX + mPinItemSize * i + mPinItemSpacing * i + mBorderWidth; + if (mPinItemSpacing == 0 && i > 0) { left = left - 2 * mBorderWidth * i; } - float right = left + mPinBoxHeight - 2 * mBorderWidth; + float right = left + mPinItemSize - 2 * mBorderWidth; float top = mBorderWidth + getPaddingTop(); - float bottom = top + mPinBoxHeight - 2 * mBorderWidth; + float bottom = top + mPinItemSize - 2 * mBorderWidth; mBoxBorderRect.set(left, top, right, bottom); } @@ -570,19 +573,41 @@ public int getBorderWidth() { * * @attr ref R.styleable#PinView_boxCount * @see #getBoxCount() + * @deprecated Use {@link #setItemCount(int)} instead. */ + @Deprecated public void setBoxCount(int len) { - mPinBoxCount = len; - setMaxLength(len); - requestLayout(); + setItemCount(len); } /** * @return Returns the count of the boxes. * @see #setBoxCount(int) + * @deprecated Use {@link #getItemCount()} instead. */ + @Deprecated public int getBoxCount() { - return mPinBoxCount; + return getItemCount(); + } + + /** + * Sets the count of items. + * + * @attr ref R.styleable#PinView_itemCount + * @see #getItemCount() + */ + public void setItemCount(int count) { + mPinItemCount = count; + setMaxLength(count); + requestLayout(); + } + + /** + * @return Returns the count of items. + * @see #setItemCount(int) + */ + public int getItemCount() { + return mPinItemCount; } /** @@ -590,18 +615,41 @@ public int getBoxCount() { * * @attr ref R.styleable#PinView_boxRadius * @see #getBoxRadius() + * @deprecated Use {@link #setItemRadius(int)} instead. */ + @Deprecated public void setBoxRadius(@Px int pinBoxRadius) { - mPinBoxRadius = pinBoxRadius; + setItemRadius(pinBoxRadius); } /** * @return Returns the radius of boxes's border. * @see #setBoxRadius(int) + * @deprecated Use {@link #getItemRadius()} instead. */ @Px + @Deprecated public int getBoxRadius() { - return mPinBoxRadius; + return getItemRadius(); + } + + /** + * Sets the radius of square. + * + * @attr ref R.styleable#PinView_itemRadius + * @see #getItemRadius() + */ + public void setItemRadius(@Px int itemRadius) { + mPinItemRadius = itemRadius; + } + + /** + * @return Returns the radius of square. + * @see #setItemRadius(int) + */ + @Px + public int getItemRadius() { + return mPinItemRadius; } /** @@ -609,19 +657,43 @@ public int getBoxRadius() { * * @attr ref R.styleable#PinView_boxMargin * @see #getBoxMargin() + * @deprecated Use {@link #setItemSpacing(int)} instead. */ + @Deprecated public void setBoxMargin(@Px int pinBoxMargin) { - mPinBoxMargin = pinBoxMargin; - requestLayout(); + setItemSpacing(pinBoxMargin); } /** * @return Returns the margin between of the boxes. * @see #setBoxMargin(int) + * @deprecated Use {@link #getItemSpacing()} instead. */ @Px + @Deprecated public int getBoxMargin() { - return mPinBoxMargin; + return getItemSpacing(); + } + + + /** + * Specifies extra space between two items. + * + * @attr ref R.styleable#PinView_itemSpacing + * @see #getItemSpacing() + */ + public void setItemSpacing(@Px int itemSpacing) { + mPinItemSpacing = itemSpacing; + requestLayout(); + } + + /** + * @return Returns the spacing between two items. + * @see #setItemSpacing(int) + */ + @Px + public int getItemSpacing() { + return mPinItemSpacing; } /** @@ -629,17 +701,39 @@ public int getBoxMargin() { * * @attr ref R.styleable#PinView_boxHeight * @see #getBoxHeight() + * @deprecated Use {@link #setItemSize(float)} instead. */ + @Deprecated public void setBoxHeight(float boxHeight) { - mPinBoxHeight = boxHeight; + setItemSize(boxHeight); } /** * @return Returns the height of box. * @see #setBoxHeight(float) + * @deprecated Use {@link #getItemSize()} instead. */ + @Deprecated public float getBoxHeight() { - return mPinBoxHeight; + return getItemSize(); + } + + /** + * Sets the height and width of item. + * + * @attr ref R.styleable#PinView_itemSize + * @see #getItemSize() + */ + public void setItemSize(float itemSize) { + mPinItemSize = itemSize; + } + + /** + * @return Returns the size of item. + * @see #setBoxHeight(float) + */ + public float getItemSize() { + return mPinItemSize; } /** diff --git a/pinview/src/main/res/values/attrs.xml b/pinview/src/main/res/values/attrs.xml index bbe4a1a..f8e98d8 100644 --- a/pinview/src/main/res/values/attrs.xml +++ b/pinview/src/main/res/values/attrs.xml @@ -21,10 +21,18 @@ + + + + + + + + diff --git a/pinview/src/main/res/values/dimens.xml b/pinview/src/main/res/values/dimens.xml index 0124496..27e3c37 100644 --- a/pinview/src/main/res/values/dimens.xml +++ b/pinview/src/main/res/values/dimens.xml @@ -16,8 +16,8 @@ --> - 48dp - 5dp - 5dp - 2dp + 48dp + 5dp + 5dp + 2dp \ No newline at end of file diff --git a/simple/src/main/java/com/chaos/view/example/MainActivity.java b/simple/src/main/java/com/chaos/view/example/MainActivity.java index 4fb1e52..1b0cee7 100644 --- a/simple/src/main/java/com/chaos/view/example/MainActivity.java +++ b/simple/src/main/java/com/chaos/view/example/MainActivity.java @@ -41,11 +41,11 @@ protected void onCreate(Bundle savedInstanceState) { ResourcesCompat.getColor(getResources(), R.color.colorPrimary, getTheme())); pinView.setBorderColor( ResourcesCompat.getColorStateList(getResources(), R.color.text_colors, getTheme())); - pinView.setBoxCount(4); - pinView.setBoxHeight(getResources().getDimensionPixelSize(R.dimen.pv_pin_view_box_height)); - pinView.setBoxRadius(getResources().getDimensionPixelSize(R.dimen.pv_pin_view_box_radius)); - pinView.setBoxMargin(getResources().getDimensionPixelSize(R.dimen.pv_pin_view_box_margin)); - pinView.setBorderWidth(getResources().getDimensionPixelSize(R.dimen.pv_pin_view_box_border_width)); + pinView.setItemCount(4); + pinView.setItemSize(getResources().getDimensionPixelSize(R.dimen.pv_pin_view_item_size)); + pinView.setItemRadius(getResources().getDimensionPixelSize(R.dimen.pv_pin_view_item_radius)); + pinView.setItemSpacing(getResources().getDimensionPixelSize(R.dimen.pv_pin_view_item_spacing)); + pinView.setBorderWidth(getResources().getDimensionPixelSize(R.dimen.pv_pin_view_item_border_width)); pinView.setAnimationEnable(true);// start animation when adding text } diff --git a/simple/src/main/res/layout/activity_main.xml b/simple/src/main/res/layout/activity_main.xml index 17e5c70..6d037dd 100644 --- a/simple/src/main/res/layout/activity_main.xml +++ b/simple/src/main/res/layout/activity_main.xml @@ -37,10 +37,10 @@ android:textSize="18sp" app:borderColor="@color/border_colors" app:borderWidth="2dp" - app:boxCount="5" - app:boxHeight="48dp" - app:boxMargin="0dp" - app:boxRadius="4dp" /> + app:itemCount="5" + app:itemSize="48dp" + app:itemSpacing="0dp" + app:itemRadius="4dp" /> + app:itemCount="3"/> + app:itemCount="2" /> + app:itemCount="1" /> \ No newline at end of file