Skip to content

Commit

Permalink
[Optimize] Optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaosLeung committed Apr 10, 2017
1 parent 0ca2e35 commit 1f1c4c1
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions pinview/src/main/java/com/chaos/view/PinView.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
* @author Chaos Leong
* 01/04/2017
*
* <p>
* <b>XML attributes</b>
* <p>
* See <a href="https://developer.android.com/reference/android/R.styleable.html#EditText">EditText Attributes</a>,
* <a href="https://developer.android.com/reference/android/R.styleable.html#TextView">TextView Attributes</a>,
* <a href="https://developer.android.com/reference/android/R.styleable.html#View">View Attributes</a>
* <p>
* <b>XML attributes</b>
* <p>
* See <a href="https://developer.android.com/reference/android/R.styleable.html#EditText">EditText Attributes</a>,
* <a href="https://developer.android.com/reference/android/R.styleable.html#TextView">TextView Attributes</a>,
* <a href="https://developer.android.com/reference/android/R.styleable.html#View">View Attributes</a>
* @attr ref R.styleable#PinView_boxCount
* @attr ref R.styleable#PinView_boxHeight
* @attr ref R.styleable#PinView_boxRadius
Expand Down Expand Up @@ -247,38 +247,24 @@ protected void drawableStateChanged() {
protected void onDraw(Canvas canvas) {
canvas.save();

updatePaints();
drawPinView(canvas);

canvas.restore();
}

private void updatePaints() {
mPaint.setColor(mCurBorderColor);
mPaint.setStrokeWidth(mBorderWidth);
mTextPaint.setColor(getCurrentTextColor());

drawPinBox(canvas);

canvas.restore();
}

private void drawPinBox(Canvas canvas) {
private void drawPinView(Canvas canvas) {
for (int i = 0; i < mPinBoxCount; i++) {
updateBoxRectF(i);
updateCenterPoint();

boolean l, r;
l = r = true;
if (mPinBoxMargin == 0) {
if (mPinBoxCount > 1) {
if (i == 0) {
// draw only left round
r = false;
} else if (i == mPinBoxCount - 1) {
// draw only right round
l = false;
} else {
// draw rect
l = r = false;
}
}
}
updateRoundRectPath(mBoxBorderRect, mPinBoxRadius, mPinBoxRadius, l, r);
canvas.drawPath(mPath, mPaint);
drawPinBox(canvas, i);

if (getText().length() > i) {
if (DBG) {
Expand All @@ -294,6 +280,27 @@ private void drawPinBox(Canvas canvas) {
}
}

private void drawPinBox(Canvas canvas, int i) {
boolean l, r;
l = r = true;
if (mPinBoxMargin == 0) {
if (mPinBoxCount > 1) {
if (i == 0) {
// draw only left round
r = false;
} else if (i == mPinBoxCount - 1) {
// draw only right round
l = false;
} else {
// draw rect
l = r = false;
}
}
}
updateRoundRectPath(mBoxBorderRect, mPinBoxRadius, mPinBoxRadius, l, r);
canvas.drawPath(mPath, mPaint);
}

private void updateRoundRectPath(RectF rectF, float rx, float ry, boolean l, boolean r) {
updateRoundRectPath(rectF, rx, ry, l, r, r, l);
}
Expand Down

0 comments on commit 1f1c4c1

Please sign in to comment.