Touch.java revision d24b8183b93e781080b2c16c487e60d51c12da31
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.text.method; 18 19import android.text.Layout; 20import android.text.NoCopySpan; 21import android.text.Layout.Alignment; 22import android.text.Spannable; 23import android.view.MotionEvent; 24import android.view.View; 25import android.view.ViewConfiguration; 26import android.widget.TextView; 27 28public class Touch { 29 private Touch() { } 30 31 /** 32 * Scrolls the specified widget to the specified coordinates, except 33 * constrains the X scrolling position to the horizontal regions of 34 * the text that will be visible after scrolling to the specified 35 * Y position. 36 */ 37 public static void scrollTo(TextView widget, Layout layout, int x, int y) { 38 int padding = widget.getTotalPaddingTop() + 39 widget.getTotalPaddingBottom(); 40 int top = layout.getLineForVertical(y); 41 int bottom = layout.getLineForVertical(y + widget.getHeight() - 42 padding); 43 44 int left = Integer.MAX_VALUE; 45 int right = 0; 46 Alignment a = null; 47 48 for (int i = top; i <= bottom; i++) { 49 left = (int) Math.min(left, layout.getLineLeft(i)); 50 right = (int) Math.max(right, layout.getLineRight(i)); 51 52 if (a == null) { 53 a = layout.getParagraphAlignment(i); 54 } 55 } 56 57 padding = widget.getTotalPaddingLeft() + widget.getTotalPaddingRight(); 58 int width = widget.getWidth(); 59 int diff = 0; 60 61 if (right - left < width - padding) { 62 if (a == Alignment.ALIGN_CENTER) { 63 diff = (width - padding - (right - left)) / 2; 64 } else if (a == Alignment.ALIGN_OPPOSITE) { 65 diff = width - padding - (right - left); 66 } 67 } 68 69 x = Math.min(x, right - (width - padding) - diff); 70 x = Math.max(x, left - diff); 71 72 widget.scrollTo(x, y); 73 } 74 75 /** 76 * Handles touch events for dragging. You may want to do other actions 77 * like moving the cursor on touch as well. 78 */ 79 public static boolean onTouchEvent(TextView widget, Spannable buffer, 80 MotionEvent event) { 81 DragState[] ds; 82 83 switch (event.getAction()) { 84 case MotionEvent.ACTION_DOWN: 85 buffer.setSpan(new DragState(event.getX(), event.getY()), 86 0, 0, Spannable.SPAN_MARK_MARK); 87 return true; 88 89 case MotionEvent.ACTION_UP: 90 ds = buffer.getSpans(0, buffer.length(), DragState.class); 91 92 for (int i = 0; i < ds.length; i++) { 93 buffer.removeSpan(ds[i]); 94 } 95 96 if (ds.length > 0 && ds[0].mUsed) { 97 return true; 98 } else { 99 return false; 100 } 101 102 case MotionEvent.ACTION_MOVE: 103 ds = buffer.getSpans(0, buffer.length(), DragState.class); 104 105 if (ds.length > 0) { 106 if (ds[0].mFarEnough == false) { 107 int slop = ViewConfiguration.get(widget.getContext()).getScaledTouchSlop(); 108 109 if (Math.abs(event.getX() - ds[0].mX) >= slop || 110 Math.abs(event.getY() - ds[0].mY) >= slop) { 111 ds[0].mFarEnough = true; 112 } 113 } 114 115 if (ds[0].mFarEnough) { 116 ds[0].mUsed = true; 117 118 float dx = ds[0].mX - event.getX(); 119 float dy = ds[0].mY - event.getY(); 120 121 ds[0].mX = event.getX(); 122 ds[0].mY = event.getY(); 123 124 int nx = widget.getScrollX() + (int) dx; 125 int ny = widget.getScrollY() + (int) dy; 126 127 int padding = widget.getTotalPaddingTop() + 128 widget.getTotalPaddingBottom(); 129 Layout layout = widget.getLayout(); 130 131 ny = Math.min(ny, layout.getHeight() - (widget.getHeight() - 132 padding)); 133 ny = Math.max(ny, 0); 134 135 scrollTo(widget, layout, nx, ny); 136 widget.cancelLongPress(); 137 return true; 138 } 139 } 140 } 141 142 return false; 143 } 144 145 private static class DragState implements NoCopySpan { 146 public float mX; 147 public float mY; 148 public boolean mFarEnough; 149 public boolean mUsed; 150 151 public DragState(float x, float y) { 152 mX = x; 153 mY = y; 154 } 155 } 156} 157