Skip to content

Commit 0e55efa

Browse files
committed
绘制光标
1 parent a4df930 commit 0e55efa

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

app/build.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ android {
3030
dependencies {
3131
compile fileTree(include: ['*.jar'], dir: 'libs')
3232
implementation "com.android.support:appcompat-v7:$SLVersion"
33-
// compile 'com.github.lwjfork:CodeEditText:1.0.3'
34-
implementation 'com.github.lwjfork:CodeEditText:1.0.4'
35-
// implementation project(':CodeEditText')
33+
// implementation 'com.github.lwjfork:CodeEditText:1.0.4'
34+
implementation project(':CodeEditText')
3635
}

app/src/main/res/layout/activity_simple.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
app:codeInputType="text"
3535
app:codeTextColor="@android:color/black"
3636
app:codeTextSize="30sp"
37+
app:cursorHeight="30dp"
3738
app:cursorWidth="4dp"
3839
app:maxCodeLength="6"
3940
app:showCursor="true"

library/src/main/java/com/lwjfork/code/cusor/CursorDrawer.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class CursorDrawer extends BaseDrawer {
2121
private int cursorColor;// 光标颜色 没有设置时默认黑色
2222
private Paint cursorPaint;
2323

24-
public CursorDrawer(boolean showCursor, int cursorDuration, int cursorWidth, int cursorHeight,int cursorColor) {
24+
public CursorDrawer(boolean showCursor, int cursorDuration, int cursorWidth, int cursorHeight, int cursorColor) {
2525
this.showCursor = showCursor;
2626
this.cursorDuration = cursorDuration;
2727
this.cursorWidth = cursorWidth;
@@ -45,30 +45,40 @@ public void setFocused(boolean isFocused) {
4545

4646
@Override
4747
public void drawCanvas() {
48+
drawCursor();
49+
}
50+
4851

52+
private void drawCursorLine() {
53+
Rect rect = blockRects.get(currentBlockIndex);
54+
int startX = rect.centerX() - cursorWidth / 2;
55+
int endX = startX;
56+
int padding = Math.max((rect.height() - cursorHeight) / 2, 0);
57+
58+
int startY = padding;
59+
int endY = startY + cursorHeight;
60+
canvas.drawLine(startX, startY, endX, endY, cursorPaint);
4961
}
5062

63+
private void clearCursor() {
64+
clearCanvas(canvas);
65+
}
5166

5267
public void drawCursor() {
5368
if (!showCursor) { // 不显示光标
54-
clearCanvas(canvas);
69+
clearCursor();
5570
return;
5671
}
5772
if (!isFocused()) { // 失去焦点
58-
clearCanvas(canvas);
73+
clearCursor();
5974
return;
6075
}
6176
if (currentBlockIndex >= blockRects.size()) { // 已经输入完成
62-
clearCanvas(canvas);
77+
clearCursor();
6378
return;
6479
}
6580
clearCanvas(canvas);
66-
Rect rect = blockRects.get(currentBlockIndex);
67-
int startX = rect.centerX() - cursorWidth / 2;
68-
int endX = startX;
69-
int startY = rect.centerY() - (rect.bottom - rect.top) / 6;
70-
int endY = rect.centerY() + (rect.bottom - rect.top) / 6;
71-
canvas.drawLine(startX, startY, endX, endY, cursorPaint);
81+
drawCursorLine();
7282
}
7383

7484
public void cancelCursor() {

0 commit comments

Comments
 (0)