Skip to content

Commit 1bdd5a9

Browse files
committed
fix
1 parent 3238040 commit 1bdd5a9

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

library/src/main/java/com/lwjfork/code/CodeEditText.java

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private void parseAttrs(Context context, AttributeSet attrs, int defStyleAttr, i
126126
codeInputType = typedArray.getInteger(R.styleable.CodeEditText_codeInputType, CodeInputType.NONE);
127127
textDrawer = createTextDrawer(codeInputType, codeTextColor, codeTextSize, dotRadius);
128128

129-
blockNormalColor = typedArray.getColor(R.styleable.CodeEditText_blockNormalColor,codeTextColor);
129+
blockNormalColor = typedArray.getColor(R.styleable.CodeEditText_blockNormalColor, codeTextColor);
130130
blockFocusColor = typedArray.getColor(R.styleable.CodeEditText_blockFocusColor, blockNormalColor);
131131
blockErrorColor = typedArray.getColor(R.styleable.CodeEditText_blockErrorColor, blockNormalColor);
132132
blockLineWidth = typedArray.getDimensionPixelSize(R.styleable.CodeEditText_blockLineWidth, dp2px(1));
@@ -143,10 +143,30 @@ private void parseAttrs(Context context, AttributeSet attrs, int defStyleAttr, i
143143
typedArray.recycle();
144144
}
145145

146+
/**
147+
* 光标绘制者
148+
*
149+
* @param showCursor
150+
* @param cursorDuration
151+
* @param cursorWidth
152+
* @param cursorColor
153+
* @return
154+
*/
146155
private CursorDrawer createCursorDrawer(boolean showCursor, int cursorDuration, int cursorWidth, int cursorColor) {
147156
return new CursorDrawer(showCursor, cursorDuration, cursorWidth, cursorColor);
148157
}
149158

159+
/**
160+
* 方框绘制者
161+
*
162+
* @param blockNormalColor
163+
* @param blockFocusColor
164+
* @param blockErrorColor
165+
* @param blockShape
166+
* @param blockLineWidth
167+
* @param blockCorner
168+
* @return
169+
*/
150170
private BaseBlockDrawer createBlockDrawer(int blockNormalColor, int blockFocusColor, int blockErrorColor, int blockShape, int blockLineWidth, int blockCorner) {
151171
switch (blockShape) {
152172
case BlockShape.SOLID:
@@ -160,6 +180,15 @@ private BaseBlockDrawer createBlockDrawer(int blockNormalColor, int blockFocusCo
160180
}
161181
}
162182

183+
/**
184+
* 文本绘制者
185+
*
186+
* @param codeInputType
187+
* @param codeTextColor
188+
* @param codeTextSize
189+
* @param dotRadius
190+
* @return
191+
*/
163192
private BaseTextDrawer createTextDrawer(int codeInputType, int codeTextColor, int codeTextSize, int dotRadius) {
164193
switch (codeInputType) {
165194
case CodeInputType.TEXT:
@@ -188,6 +217,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
188217
initBitmapAndCanvas(measureWidth, measureHeight);
189218
}
190219

220+
/**
221+
* 计算每个输入框的位置,并初始化绘制者
222+
*
223+
* @param measureWidth
224+
* @param measureHeight
225+
*/
191226
private void initRect(int measureWidth, int measureHeight) {
192227
if (blockRects == null) {
193228
blockRects = new ArrayList<>();
@@ -212,6 +247,12 @@ private void initRect(int measureWidth, int measureHeight) {
212247
cursorDrawer.setBlockRects(blockRects);
213248
}
214249

250+
/**
251+
* 创建Bitmap 和 Canvas
252+
*
253+
* @param measureWidth
254+
* @param measureHeight
255+
*/
215256
private void initBitmapAndCanvas(int measureWidth, int measureHeight) {
216257
blockBitmap = blockDrawer.createBitmapAndCanvas(measureWidth, measureHeight);
217258
textBitmap = textDrawer.createBitmapAndCanvas(measureWidth, measureHeight);
@@ -323,7 +364,14 @@ public interface OnTextChangedListener {
323364
void onInputCompleted(CharSequence text);
324365
}
325366

326-
367+
/**
368+
* 监听文本变化,并更改UI
369+
*
370+
* @param text
371+
* @param start
372+
* @param lengthBefore
373+
* @param lengthAfter
374+
*/
327375
@Override
328376
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
329377
if (isInEditMode()) {
@@ -357,8 +405,21 @@ protected void onFocusChanged(boolean focused, int direction, Rect previouslyFoc
357405
@Override
358406
protected void onDetachedFromWindow() {
359407
super.onDetachedFromWindow();
408+
recycleBitmap(blockBitmap);
409+
recycleBitmap(textBitmap);
410+
recycleBitmap(cursorBitmap);
360411
}
361412

413+
/**
414+
* 销毁Bitmap
415+
*
416+
* @param bitmap
417+
*/
418+
private void recycleBitmap(Bitmap bitmap) {
419+
if (bitmap != null && !bitmap.isRecycled()) {
420+
bitmap.recycle();
421+
}
422+
}
362423

363424
/**
364425
* 清空所有数据

library/src/main/java/com/lwjfork/code/block/BaseBlockDrawer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ public final void drawCanvas() {
6161
}
6262
}
6363

64-
64+
// 焦点获取绘制
6565
protected abstract void drawFocusedBlock(RectF rectF);
6666

67+
// 常态绘制
6768
protected abstract void drawNormalBlock(RectF rectF);
6869

70+
// 错误态绘制
6971
protected abstract void drawErrorBlock(RectF rectF);
7072

7173
public int getBlockNormalColor() {

0 commit comments

Comments
 (0)