I'm making my own Mongolian vertical script TextView
(and EditText
) from scratch by extending View
. I'm trying to understand how the blinking cursor is implemented in the Android TextView
source code. It seems to be handled by an mEditor
instance of the Editor
class. In that class I found a drawCursor
method:
private void drawCursor(Canvas canvas, int cursorOffsetVertical) {
final boolean translate = cursorOffsetVertical != 0;
if (translate) canvas.translate(0, cursorOffsetVertical);
for (int i = 0; i < mCursorCount; i++) {
mCursorDrawable[i].draw(canvas);
}
if (translate) canvas.translate(0, -cursorOffsetVertical);
}
Apparently, the cursor itself is a Drawable
of some type.
So I am finding pieces, but I can't see the forest through the trees. Could someone who understands this better than me tell me in plain English how the blinking cursor works?
Here are some specific points I don't really understand:
TextView
(or EditText
), Layout
, Editor
, and Drawable
.Note:
The accepted answer answers my question as I asked it. However, I would be glad to accept a different answer if anyone can add a more canonical one that explains things in more detail.
EditText
is just editable TextView
, Layout
is used for measurements, calculation, where and how the text will laid out. Drawable
is there just to hold the look of the cursor, later being drawn on the Canvas
. Path
is invalidated.Blinking basically is recursive Runnable
that a Handler
executes every 500ms (guessing), in on-off fashion, and it is rescheduled while EditText
has focus. This also can be achieved by looping ValueAnimator
of int, where the cursor drawable alpha will be toggled between 0 and 255.
You can find this method in the TextView
class:
public void setCursorVisible(boolean visible) {
if (visible && mEditor == null) return; // visible is the default value with no edit data
createEditorIfNeeded();
if (mEditor.mCursorVisible != visible) {
mEditor.mCursorVisible = visible;
invalidate();
mEditor.makeBlink();
// InsertionPointCursorController depends on mCursorVisible
mEditor.prepareCursorControllers();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With