Touch.java revision 58b971d733a2c700cabd3db02b6ea4d5faca6939
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.ViewConfiguration; 25import android.widget.TextView; 26 27public class Touch { 28 private Touch() { } 29 30 /** 31 * Scrolls the specified widget to the specified coordinates, except 32 * constrains the X scrolling position to the horizontal regions of 33 * the text that will be visible after scrolling to the specified 34 * Y position. 35 */ 36 public static void scrollTo(TextView widget, Layout layout, int x, int y) { 37 int padding = widget.getTotalPaddingTop() + 38 widget.getTotalPaddingBottom(); 39 int top = layout.getLineForVertical(y); 40 int bottom = layout.getLineForVertical(y + widget.getHeight() - 41 padding); 42 43 int left = Integer.MAX_VALUE; 44 int right = 0; 45 Alignment a = null; 46 47 for (int i = top; i <= bottom; i++) { 48 left = (int) Math.min(left, layout.getLineLeft(i)); 49 right = (int) Math.max(right, layout.getLineRight(i)); 50 51 if (a == null) { 52 a = layout.getParagraphAlignment(i); 53 } 54 } 55 56 padding = widget.getTotalPaddingLeft() + widget.getTotalPaddingRight(); 57 int width = widget.getWidth(); 58 int diff = 0; 59 60 if (right - left < width - padding) { 61 if (a == Alignment.ALIGN_CENTER) { 62 diff = (width - padding - (right - left)) / 2; 63 } else if (a == Alignment.ALIGN_OPPOSITE) { 64 diff = width - padding - (right - left); 65 } 66 } 67 68 x = Math.min(x, right - (width - padding) - diff); 69 x = Math.max(x, left - diff); 70 71 widget.scrollTo(x, y); 72 } 73 74 /** 75 * Handles touch events for dragging. You may want to do other actions 76 * like moving the cursor on touch as well. 77 */ 78 public static boolean onTouchEvent(TextView widget, Spannable buffer, 79 MotionEvent event) { 80 DragState[] ds; 81 82 switch (event.getAction()) { 83 case MotionEvent.ACTION_DOWN: 84 ds = buffer.getSpans(0, buffer.length(), DragState.class); 85 86 for (int i = 0; i < ds.length; i++) { 87 buffer.removeSpan(ds[i]); 88 } 89 90 buffer.setSpan(new DragState(event.getX(), event.getY(), 91 widget.getScrollX(), widget.getScrollY()), 92 0, 0, Spannable.SPAN_MARK_MARK); 93 return true; 94 95 case MotionEvent.ACTION_UP: 96 ds = buffer.getSpans(0, buffer.length(), DragState.class); 97 98 for (int i = 0; i < ds.length; i++) { 99 buffer.removeSpan(ds[i]); 100 } 101 102 if (ds.length > 0 && ds[0].mUsed) { 103 return true; 104 } else { 105 return false; 106 } 107 108 case MotionEvent.ACTION_MOVE: 109 ds = buffer.getSpans(0, buffer.length(), DragState.class); 110 111 if (ds.length > 0) { 112 if (ds[0].mFarEnough == false) { 113 int slop = ViewConfiguration.get(widget.getContext()).getScaledTouchSlop(); 114 115 if (Math.abs(event.getX() - ds[0].mX) >= slop || 116 Math.abs(event.getY() - ds[0].mY) >= slop) { 117 ds[0].mFarEnough = true; 118 } 119 } 120 121 if (ds[0].mFarEnough) { 122 ds[0].mUsed = true; 123 124 float dx = ds[0].mX - event.getX(); 125 float dy = ds[0].mY - event.getY(); 126 127 ds[0].mX = event.getX(); 128 ds[0].mY = event.getY(); 129 130 int nx = widget.getScrollX() + (int) dx; 131 int ny = widget.getScrollY() + (int) dy; 132 133 int padding = widget.getTotalPaddingTop() + 134 widget.getTotalPaddingBottom(); 135 Layout layout = widget.getLayout(); 136 137 ny = Math.min(ny, layout.getHeight() - (widget.getHeight() - 138 padding)); 139 ny = Math.max(ny, 0); 140 141 scrollTo(widget, layout, nx, ny); 142 widget.cancelLongPress(); 143 return true; 144 } 145 } 146 } 147 148 return false; 149 } 150 151 public static int getInitialScrollX(TextView widget, Spannable buffer) { 152 DragState[] ds = buffer.getSpans(0, buffer.length(), DragState.class); 153 return ds.length > 0 ? ds[0].mScrollX : -1; 154 } 155 156 public static int getInitialScrollY(TextView widget, Spannable buffer) { 157 DragState[] ds = buffer.getSpans(0, buffer.length(), DragState.class); 158 return ds.length > 0 ? ds[0].mScrollY : -1; 159 } 160 161 private static class DragState implements NoCopySpan { 162 public float mX; 163 public float mY; 164 public int mScrollX; 165 public int mScrollY; 166 public boolean mFarEnough; 167 public boolean mUsed; 168 169 public DragState(float x, float y, int scrollX, int scrollY) { 170 mX = x; 171 mY = y; 172 mScrollX = scrollX; 173 mScrollY = scrollY; 174 } 175 } 176} 177