Touchpublic class Touch extends Object
Constructors Summary |
---|
private Touch()
|
Methods Summary |
---|
public static int | getInitialScrollX(android.widget.TextView widget, android.text.Spannable buffer)
DragState[] ds = buffer.getSpans(0, buffer.length(), DragState.class);
return ds.length > 0 ? ds[0].mScrollX : -1;
| public static int | getInitialScrollY(android.widget.TextView widget, android.text.Spannable buffer)
DragState[] ds = buffer.getSpans(0, buffer.length(), DragState.class);
return ds.length > 0 ? ds[0].mScrollY : -1;
| static boolean | isActivelySelecting(android.text.Spannable buffer)Checks if selection is still active.
This is useful for extending Selection span on buffer.
DragState[] ds;
ds = buffer.getSpans(0, buffer.length(), DragState.class);
return ds.length > 0 && ds[0].mIsActivelySelecting;
| static boolean | isSelectionStarted(android.text.Spannable buffer)Checks if selection has begun (are we out of slop?).
Note: DragState.mIsSelectionStarted goes back to false with the very next event.
This is useful for starting Selection span on buffer.
DragState[] ds;
ds = buffer.getSpans(0, buffer.length(), DragState.class);
return ds.length > 0 && ds[0].mIsSelectionStarted;
| public static boolean | onTouchEvent(android.widget.TextView widget, android.text.Spannable buffer, android.view.MotionEvent event)Handles touch events for dragging. You may want to do other actions
like moving the cursor on touch as well.
DragState[] ds;
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
ds = buffer.getSpans(0, buffer.length(), DragState.class);
for (int i = 0; i < ds.length; i++) {
buffer.removeSpan(ds[i]);
}
buffer.setSpan(new DragState(event.getX(), event.getY(),
widget.getScrollX(), widget.getScrollY()),
0, 0, Spannable.SPAN_MARK_MARK);
return true;
case MotionEvent.ACTION_UP:
ds = buffer.getSpans(0, buffer.length(), DragState.class);
for (int i = 0; i < ds.length; i++) {
buffer.removeSpan(ds[i]);
}
if (ds.length > 0 && ds[0].mUsed) {
return true;
} else {
return false;
}
case MotionEvent.ACTION_MOVE:
ds = buffer.getSpans(0, buffer.length(), DragState.class);
if (ds.length > 0) {
ds[0].mIsSelectionStarted = false;
if (ds[0].mFarEnough == false) {
int slop = ViewConfiguration.get(widget.getContext()).getScaledTouchSlop();
if (Math.abs(event.getX() - ds[0].mX) >= slop ||
Math.abs(event.getY() - ds[0].mY) >= slop) {
ds[0].mFarEnough = true;
if (event.isButtonPressed(MotionEvent.BUTTON_PRIMARY)) {
ds[0].mIsActivelySelecting = true;
ds[0].mIsSelectionStarted = true;
}
}
}
if (ds[0].mFarEnough) {
ds[0].mUsed = true;
boolean cap = (event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0
|| MetaKeyKeyListener.getMetaState(buffer,
MetaKeyKeyListener.META_SHIFT_ON) == 1
|| MetaKeyKeyListener.getMetaState(buffer,
MetaKeyKeyListener.META_SELECTING) != 0;
if (!event.isButtonPressed(MotionEvent.BUTTON_PRIMARY)) {
ds[0].mIsActivelySelecting = false;
}
float dx;
float dy;
if (cap && event.isButtonPressed(MotionEvent.BUTTON_PRIMARY)) {
// if we're selecting, we want the scroll to go in
// the direction of the drag
dx = event.getX() - ds[0].mX;
dy = event.getY() - ds[0].mY;
} else {
dx = ds[0].mX - event.getX();
dy = ds[0].mY - event.getY();
}
ds[0].mX = event.getX();
ds[0].mY = event.getY();
int nx = widget.getScrollX() + (int) dx;
int ny = widget.getScrollY() + (int) dy;
int padding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom();
Layout layout = widget.getLayout();
ny = Math.min(ny, layout.getHeight() - (widget.getHeight() - padding));
ny = Math.max(ny, 0);
int oldX = widget.getScrollX();
int oldY = widget.getScrollY();
if (!event.isButtonPressed(MotionEvent.BUTTON_PRIMARY)) {
scrollTo(widget, layout, nx, ny);
}
// If we actually scrolled, then cancel the up action.
if (oldX != widget.getScrollX() || oldY != widget.getScrollY()) {
widget.cancelLongPress();
}
return true;
}
}
}
return false;
| public static void | scrollTo(android.widget.TextView widget, android.text.Layout layout, int x, int y)Scrolls the specified widget to the specified coordinates, except
constrains the X scrolling position to the horizontal regions of
the text that will be visible after scrolling to the specified
Y position.
final int horizontalPadding = widget.getTotalPaddingLeft() + widget.getTotalPaddingRight();
final int availableWidth = widget.getWidth() - horizontalPadding;
final int top = layout.getLineForVertical(y);
Alignment a = layout.getParagraphAlignment(top);
boolean ltr = layout.getParagraphDirection(top) > 0;
int left, right;
if (widget.getHorizontallyScrolling()) {
final int verticalPadding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom();
final int bottom = layout.getLineForVertical(y + widget.getHeight() - verticalPadding);
left = Integer.MAX_VALUE;
right = 0;
for (int i = top; i <= bottom; i++) {
left = (int) Math.min(left, layout.getLineLeft(i));
right = (int) Math.max(right, layout.getLineRight(i));
}
} else {
left = 0;
right = availableWidth;
}
final int actualWidth = right - left;
if (actualWidth < availableWidth) {
if (a == Alignment.ALIGN_CENTER) {
x = left - ((availableWidth - actualWidth) / 2);
} else if ((ltr && (a == Alignment.ALIGN_OPPOSITE)) ||
(!ltr && (a == Alignment.ALIGN_NORMAL)) ||
(a == Alignment.ALIGN_RIGHT)) {
// align_opposite does NOT mean align_right, we need the paragraph
// direction to resolve it to left or right
x = left - (availableWidth - actualWidth);
} else {
x = left;
}
} else {
x = Math.min(x, right - availableWidth);
x = Math.max(x, left);
}
widget.scrollTo(x, y);
|
|