Skip to content

Commit e688091

Browse files
authored
Merge pull request #25 from ChaosLeong/develop
[Release] 1.4.0
2 parents 720e9ef + 82e8539 commit e688091

File tree

10 files changed

+189
-28
lines changed

10 files changed

+189
-28
lines changed

.github/issue_template.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
What kind of issue is this?
2+
3+
- [ ] Question. If you want to ask how to do something, or to understand why something isn't
4+
working the way you expect it to.
5+
6+
- [ ] Bug report. Please provide your device name, device OS version, and describe the reproduction steps.
7+
8+
- [ ] Feature Request. Telling us what problem you’re trying to solve.
9+
10+
##### Info:
11+
12+
* PinView version:
13+
* Device OS version:
14+
* Device Name:
15+
16+
##### Description:
17+
18+
##### Reproduction Steps:
19+
20+
##### What did I do:

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ repositories {
1515
}
1616
1717
dependencies {
18-
compile 'com.chaos.view:pinview:1.3.2'
18+
compile 'com.chaos.view:pinview:1.4.0'
1919
}
2020
```
2121

@@ -56,7 +56,7 @@ Add PinView in your layout.
5656
#### Java
5757

5858
``` Java
59-
PinView pinView = (PinView) findViewById(R.id.secondPinView);
59+
final PinView pinView = findViewById(R.id.secondPinView);
6060
pinView.setTextColor(
6161
ResourcesCompat.getColor(getResources(), R.color.colorAccent, getTheme()));
6262
pinView.setTextColor(
@@ -77,6 +77,9 @@ pinView.setCursorColor(
7777
ResourcesCompat.getColor(getResources(), R.color.line_selected, getTheme()));
7878
pinView.setCursorWidth(getResources().getDimensionPixelSize(R.dimen.pv_pin_view_cursor_width));
7979
pinView.addTextChangedListener(new TextWatcher() {...});
80+
pinView.setItemBackgroundColor(Color.BLACK);
81+
pinView.setItemBackground(getResources().getDrawable(R.drawable.item_background));
82+
pinView.setItemBackgroundResources(R.drawable.item_background);
8083
```
8184

8285
### Step 2:
@@ -102,7 +105,7 @@ or use the `PinWidget.PinView` style.
102105

103106
### Step 3 (Optional):
104107

105-
To highlight current item,
108+
To highlight current item, use 'selected' state, in other words,
106109

107110
add `android:state_selected="true"` to `app:lineColor`
108111

@@ -125,9 +128,10 @@ or add `android:cursorVisible="true"`.
125128
* **itemSpacing**, the spacing between two items.
126129
* **lineWidth**, the line (border) width.
127130
* **lineColor**, the line (border) colors.
128-
* **viewType**, the view type of PinView, currently this will be one of `rectangle` or `line`.
131+
* **viewType**, the view type of PinView, currently this will be one of `rectangle`, `line` or `none`.
129132
* **cursorColor**, the cursor color.
130133
* **cursorWidth**, the width of cursor.
134+
* **itemBackground**, the item background.
131135

132136
## Thanks
133137

@@ -136,7 +140,7 @@ or add `android:cursorVisible="true"`.
136140
## License
137141

138142

139-
Copyright 2017 Chaos Leong
143+
Copyright 2017-2018 Chaos Leong
140144

141145
Licensed under the Apache License, Version 2.0 (the "License");
142146
you may not use this file except in compliance with the License.

pinview/publish.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def issueUrl = 'https://github.com/ChaosLeong/PinView/issues'
66
def gitUrl = '[email protected]:ChaosLeong/PinView.git'
77

88
group = "com.chaos.view"
9-
version = "1.3.2"
9+
version = "1.4.0"
1010

1111
install {
1212
repositories.mavenInstaller {

pinview/src/main/java/com/chaos/view/PinView.java

Lines changed: 124 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@
2828
import android.graphics.PointF;
2929
import android.graphics.Rect;
3030
import android.graphics.RectF;
31+
import android.graphics.Typeface;
32+
import android.graphics.drawable.ColorDrawable;
33+
import android.graphics.drawable.Drawable;
3134
import android.support.annotation.ColorInt;
35+
import android.support.annotation.DrawableRes;
3236
import android.support.annotation.Nullable;
3337
import android.support.annotation.Px;
38+
import android.support.v4.content.res.ResourcesCompat;
3439
import android.support.v4.view.ViewCompat;
3540
import android.support.v7.widget.AppCompatEditText;
3641
import android.text.InputFilter;
@@ -60,8 +65,12 @@ public class PinView extends AppCompatEditText {
6065

6166
private static final InputFilter[] NO_FILTERS = new InputFilter[0];
6267

68+
private static final int[] HIGHLIGHT_STATES = new int[]{
69+
android.R.attr.state_selected};
70+
6371
private static final int VIEW_TYPE_RECTANGLE = 0;
6472
private static final int VIEW_TYPE_LINE = 1;
73+
private static final int VIEW_TYPE_NONE = 2;
6574

6675
private int mViewType;
6776

@@ -73,8 +82,7 @@ public class PinView extends AppCompatEditText {
7382
private int mPinItemSpacing;
7483

7584
private final Paint mPaint;
76-
private final TextPaint mTextPaint;
77-
private final Paint mAnimatorTextPaint;
85+
private final TextPaint mAnimatorTextPaint = new TextPaint();
7886

7987
private ColorStateList mLineColor;
8088
private int mCurLineColor = Color.BLACK;
@@ -96,6 +104,9 @@ public class PinView extends AppCompatEditText {
96104
private int mCursorWidth;
97105
private int mCursorColor;
98106

107+
private int mItemBackgroundResource;
108+
private Drawable mItemBackground;
109+
99110
public PinView(Context context) {
100111
this(context, null);
101112
}
@@ -112,12 +123,7 @@ public PinView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
112123
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
113124
mPaint.setStyle(Paint.Style.STROKE);
114125

115-
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
116-
mTextPaint.density = res.getDisplayMetrics().density;
117-
mTextPaint.setStyle(Paint.Style.FILL);
118-
mTextPaint.setTextSize(getTextSize());
119-
120-
mAnimatorTextPaint = new TextPaint(mTextPaint);
126+
mAnimatorTextPaint.set(getPaint());
121127

122128
final Resources.Theme theme = context.getTheme();
123129

@@ -140,6 +146,8 @@ public PinView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
140146
mCursorWidth = a.getDimensionPixelSize(R.styleable.PinView_cursorWidth,
141147
res.getDimensionPixelSize(R.dimen.pv_pin_view_cursor_width));
142148

149+
mItemBackground = a.getDrawable(R.styleable.PinView_itemBackground);
150+
143151
a.recycle();
144152

145153
if (mLineColor != null) {
@@ -157,6 +165,19 @@ public PinView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
157165
setTextIsSelectable(false);
158166
}
159167

168+
@Override
169+
public void setTypeface(Typeface tf, int style) {
170+
super.setTypeface(tf, style);
171+
}
172+
173+
@Override
174+
public void setTypeface(Typeface tf) {
175+
super.setTypeface(tf);
176+
if (mAnimatorTextPaint != null) {
177+
mAnimatorTextPaint.set(getPaint());
178+
}
179+
}
180+
160181
private void setMaxLength(int maxLength) {
161182
if (maxLength >= 0) {
162183
setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
@@ -187,10 +208,11 @@ private void checkItemRadius() {
187208
if (mPinItemRadius > halfOfLineWidth) {
188209
throw new IllegalArgumentException("The itemRadius can not be greater than lineWidth when viewType is line");
189210
}
190-
}
191-
float halfOfItemWidth = ((float) mPinItemWidth) / 2;
192-
if (mPinItemRadius > halfOfItemWidth) {
193-
throw new IllegalArgumentException("The itemRadius can not be greater than itemWidth");
211+
} else if (mViewType == VIEW_TYPE_RECTANGLE) {
212+
float halfOfItemWidth = ((float) mPinItemWidth) / 2;
213+
if (mPinItemRadius > halfOfItemWidth) {
214+
throw new IllegalArgumentException("The itemRadius can not be greater than itemWidth");
215+
}
194216
}
195217
}
196218

@@ -292,17 +314,25 @@ private void updatePaints() {
292314
mPaint.setColor(mCurLineColor);
293315
mPaint.setStyle(Paint.Style.STROKE);
294316
mPaint.setStrokeWidth(mLineWidth);
295-
mTextPaint.setColor(getCurrentTextColor());
317+
getPaint().setColor(getCurrentTextColor());
296318
}
297319

298320
private void drawPinView(Canvas canvas) {
299321
for (int i = 0; i < mPinItemCount; i++) {
300322
updateItemRectF(i);
301323
updateCenterPoint();
302324

325+
canvas.save();
326+
if (mViewType == VIEW_TYPE_RECTANGLE) {
327+
updatePinBoxPath(i);
328+
canvas.clipPath(mPath);
329+
}
330+
drawItemBackground(canvas, false);
331+
canvas.restore();
332+
303333
if (mViewType == VIEW_TYPE_RECTANGLE) {
304334
drawPinBox(canvas, i);
305-
} else {
335+
} else if (mViewType == VIEW_TYPE_LINE) {
306336
drawPinLine(canvas, i);
307337
}
308338

@@ -327,13 +357,21 @@ private void drawPinView(Canvas canvas) {
327357
updateItemRectF(index);
328358
updateCenterPoint();
329359

330-
mPaint.setColor(getLineColorForState(android.R.attr.state_selected));
360+
canvas.save();
361+
if (mViewType == VIEW_TYPE_RECTANGLE) {
362+
updatePinBoxPath(index);
363+
canvas.clipPath(mPath);
364+
}
365+
drawItemBackground(canvas, true);
366+
canvas.restore();
367+
368+
mPaint.setColor(getLineColorForState(HIGHLIGHT_STATES));
331369

332370
drawCursor(canvas);
333371

334372
if (mViewType == VIEW_TYPE_RECTANGLE) {
335373
drawPinBox(canvas, index);
336-
} else {
374+
} else if (mViewType == VIEW_TYPE_LINE) {
337375
drawPinLine(canvas, index);
338376
}
339377
}
@@ -343,7 +381,22 @@ private int getLineColorForState(int... states) {
343381
return mLineColor != null ? mLineColor.getColorForState(states, mCurLineColor) : mCurLineColor;
344382
}
345383

346-
private void drawPinBox(Canvas canvas, int i) {
384+
private void drawItemBackground(Canvas canvas, boolean highlight) {
385+
if (mItemBackground == null) {
386+
return;
387+
}
388+
float delta = (float) mLineWidth / 2;
389+
int left = Math.round(mItemBorderRect.left - delta);
390+
int top = Math.round(mItemBorderRect.top - delta);
391+
int right = Math.round(mItemBorderRect.right + delta);
392+
int bottom = Math.round(mItemBorderRect.bottom + delta);
393+
394+
mItemBackground.setBounds(left, top, right, bottom);
395+
mItemBackground.setState(highlight ? HIGHLIGHT_STATES : getDrawableState());
396+
mItemBackground.draw(canvas);
397+
}
398+
399+
private void updatePinBoxPath(int i) {
347400
boolean drawRightCorner = false;
348401
boolean drawLeftCorner = false;
349402
if (mPinItemSpacing != 0) {
@@ -357,6 +410,10 @@ private void drawPinBox(Canvas canvas, int i) {
357410
}
358411
}
359412
updateRoundRectPath(mItemBorderRect, mPinItemRadius, mPinItemRadius, drawLeftCorner, drawRightCorner);
413+
}
414+
415+
private void drawPinBox(Canvas canvas, int i) {
416+
// updatePinBoxPath(canvas, i);
360417
canvas.drawPath(mPath, mPaint);
361418
}
362419

@@ -378,7 +435,11 @@ private void drawPinLine(Canvas canvas, int i) {
378435
mPaint.setStyle(Paint.Style.FILL);
379436
mPaint.setStrokeWidth(((float) mLineWidth) / 10);
380437
float halfLineWidth = ((float) mLineWidth) / 2;
381-
mItemLineRect.set(mItemBorderRect.left, mItemBorderRect.bottom - halfLineWidth, mItemBorderRect.right, mItemBorderRect.bottom + halfLineWidth);
438+
mItemLineRect.set(
439+
mItemBorderRect.left - halfLineWidth,
440+
mItemBorderRect.bottom - halfLineWidth,
441+
mItemBorderRect.right + halfLineWidth,
442+
mItemBorderRect.bottom + halfLineWidth);
382443

383444
updateRoundRectPath(mItemLineRect, mPinItemRadius, mPinItemRadius, l, r);
384445
canvas.drawPath(mPath, mPaint);
@@ -511,10 +572,10 @@ private void drawCircle(Canvas canvas, int i) {
511572

512573
private Paint getPaintByIndex(int i) {
513574
if (isAnimationEnable && i == getText().length() - 1) {
514-
mAnimatorTextPaint.setColor(mTextPaint.getColor());
575+
mAnimatorTextPaint.setColor(getPaint().getColor());
515576
return mAnimatorTextPaint;
516577
} else {
517-
return mTextPaint;
578+
return getPaint();
518579
}
519580
}
520581

@@ -780,7 +841,7 @@ public void setTextSize(int unit, float size) {
780841
updateCursorHeight();
781842
}
782843

783-
//region Cursor
844+
//region Cursorint
784845

785846
/**
786847
* Sets the width (in pixels) of cursor.
@@ -829,6 +890,48 @@ public int getCursorColor() {
829890
return mCursorColor;
830891
}
831892

893+
/**
894+
* Set the item background to a given resource. The resource should refer to
895+
* a Drawable object or 0 to remove the item background.
896+
*
897+
* @param resId The identifier of the resource.
898+
* @attr ref R.styleable#PinView_itemBackground
899+
*/
900+
public void setItemBackgroundResources(@DrawableRes int resId) {
901+
if (resId != 0 && mItemBackgroundResource != resId) {
902+
return;
903+
}
904+
mItemBackground = ResourcesCompat.getDrawable(getResources(), resId, getContext().getTheme());
905+
setItemBackground(mItemBackground);
906+
mItemBackgroundResource = resId;
907+
}
908+
909+
/**
910+
* Sets the item background color for this view.
911+
*
912+
* @param color the color of the item background
913+
*/
914+
public void setItemBackgroundColor(@ColorInt int color) {
915+
if (mItemBackground instanceof ColorDrawable) {
916+
((ColorDrawable) mItemBackground.mutate()).setColor(color);
917+
mItemBackgroundResource = 0;
918+
} else {
919+
setItemBackground(new ColorDrawable(color));
920+
}
921+
}
922+
923+
/**
924+
* Set the item background to a given Drawable, or remove the background.
925+
*
926+
* @param background The Drawable to use as the item background, or null to remove the
927+
* item background
928+
*/
929+
public void setItemBackground(Drawable background) {
930+
mItemBackgroundResource = 0;
931+
mItemBackground = background;
932+
invalidate();
933+
}
934+
832935
@Override
833936
public void setCursorVisible(boolean visible) {
834937
if (isCursorVisible != visible) {

pinview/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
<attr name="viewType" format="enum">
3232
<enum name="rectangle" value="0" />
3333
<enum name="line" value="1" />
34+
<enum name="none" value="2" />
3435
</attr>
3536
<attr name="android:cursorVisible" />
3637
<attr name="cursorWidth" format="dimension" />
3738
<attr name="cursorColor" format="reference|color" />
39+
<attr name="itemBackground" format="reference|color" />
3840
</declare-styleable>
3941
</resources>

simple/src/main/java/com/chaos/view/example/MainActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.chaos.view.example;
1818

1919
import android.content.Intent;
20+
import android.graphics.Color;
2021
import android.os.Bundle;
2122
import android.support.v4.content.res.ResourcesCompat;
2223
import android.support.v7.app.AppCompatActivity;
@@ -73,6 +74,9 @@ public void afterTextChanged(Editable s) {
7374

7475
}
7576
});
77+
pinView.setItemBackgroundColor(Color.BLACK);
78+
pinView.setItemBackground(getResources().getDrawable(R.drawable.item_background));
79+
pinView.setItemBackgroundResources(R.drawable.item_background);
7680
}
7781

7882
@Override

0 commit comments

Comments
 (0)