ViewConfiguration.java revision 909cbaf8f92d69f507bbdba9e5aa960d1e6c7a1f
1/* 2 * Copyright (C) 2006 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.view; 18 19import android.content.Context; 20import android.util.DisplayMetrics; 21import android.util.SparseArray; 22 23/** 24 * Contains methods to standard constants used in the UI for timeouts, sizes, and distances. 25 */ 26public class ViewConfiguration { 27 /** 28 * Expected bit depth of the display panel. 29 * 30 * @hide 31 */ 32 public static final float PANEL_BIT_DEPTH = 24; 33 34 /** 35 * Minimum alpha required for a view to draw. 36 * 37 * @hide 38 */ 39 public static final float ALPHA_THRESHOLD = 0.5f / PANEL_BIT_DEPTH; 40 /** 41 * @hide 42 */ 43 public static final float ALPHA_THRESHOLD_INT = 0x7f / PANEL_BIT_DEPTH; 44 45 /** 46 * Defines the width of the horizontal scrollbar and the height of the vertical scrollbar in 47 * pixels 48 */ 49 private static final int SCROLL_BAR_SIZE = 10; 50 51 /** 52 * Duration of the fade when scrollbars fade away in milliseconds 53 */ 54 private static final int SCROLL_BAR_FADE_DURATION = 250; 55 56 /** 57 * Default delay before the scrollbars fade in milliseconds 58 */ 59 private static final int SCROLL_BAR_DEFAULT_DELAY = 300; 60 61 /** 62 * Defines the length of the fading edges in pixels 63 */ 64 private static final int FADING_EDGE_LENGTH = 12; 65 66 /** 67 * Defines the duration in milliseconds of the pressed state in child 68 * components. 69 */ 70 private static final int PRESSED_STATE_DURATION = 125; 71 72 /** 73 * Defines the duration in milliseconds before a press turns into 74 * a long press 75 */ 76 private static final int LONG_PRESS_TIMEOUT = 500; 77 78 /** 79 * Defines the duration in milliseconds a user needs to hold down the 80 * appropriate button to bring up the global actions dialog (power off, 81 * lock screen, etc). 82 */ 83 private static final int GLOBAL_ACTIONS_KEY_TIMEOUT = 500; 84 85 /** 86 * Defines the duration in milliseconds we will wait to see if a touch event 87 * is a tap or a scroll. If the user does not move within this interval, it is 88 * considered to be a tap. 89 */ 90 private static final int TAP_TIMEOUT = 115; 91 92 /** 93 * Defines the duration in milliseconds we will wait to see if a touch event 94 * is a jump tap. If the user does not complete the jump tap within this interval, it is 95 * considered to be a tap. 96 */ 97 private static final int JUMP_TAP_TIMEOUT = 500; 98 99 /** 100 * Defines the duration in milliseconds between the first tap's up event and 101 * the second tap's down event for an interaction to be considered a 102 * double-tap. 103 */ 104 private static final int DOUBLE_TAP_TIMEOUT = 300; 105 106 /** 107 * Defines the duration in milliseconds we want to display zoom controls in response 108 * to a user panning within an application. 109 */ 110 private static final int ZOOM_CONTROLS_TIMEOUT = 3000; 111 112 /** 113 * Inset in pixels to look for touchable content when the user touches the edge of the screen 114 */ 115 private static final int EDGE_SLOP = 12; 116 117 /** 118 * Distance a touch can wander before we think the user is scrolling in pixels 119 */ 120 private static final int TOUCH_SLOP = 16; 121 122 /** 123 * Distance a touch can wander before we think the user is attempting a paged scroll 124 * (in dips) 125 */ 126 private static final int PAGING_TOUCH_SLOP = TOUCH_SLOP * 2; 127 128 /** 129 * Distance between the first touch and second touch to still be considered a double tap 130 */ 131 private static final int DOUBLE_TAP_SLOP = 100; 132 133 /** 134 * Distance a touch needs to be outside of a window's bounds for it to 135 * count as outside for purposes of dismissing the window. 136 */ 137 private static final int WINDOW_TOUCH_SLOP = 16; 138 139 /** 140 * Minimum velocity to initiate a fling, as measured in pixels per second 141 */ 142 private static final int MINIMUM_FLING_VELOCITY = 50; 143 144 /** 145 * Maximum velocity to initiate a fling, as measured in pixels per second 146 */ 147 private static final int MAXIMUM_FLING_VELOCITY = 4000; 148 149 /** 150 * The maximum size of View's drawing cache, expressed in bytes. This size 151 * should be at least equal to the size of the screen in ARGB888 format. 152 */ 153 @Deprecated 154 private static final int MAXIMUM_DRAWING_CACHE_SIZE = 320 * 480 * 4; // HVGA screen, ARGB8888 155 156 /** 157 * The coefficient of friction applied to flings/scrolls. 158 */ 159 private static float SCROLL_FRICTION = 0.015f; 160 161 private final int mEdgeSlop; 162 private final int mFadingEdgeLength; 163 private final int mMinimumFlingVelocity; 164 private final int mMaximumFlingVelocity; 165 private final int mScrollbarSize; 166 private final int mTouchSlop; 167 private final int mPagingTouchSlop; 168 private final int mDoubleTapSlop; 169 private final int mWindowTouchSlop; 170 private final int mMaximumDrawingCacheSize; 171 172 private static final SparseArray<ViewConfiguration> sConfigurations = 173 new SparseArray<ViewConfiguration>(2); 174 175 /** 176 * @deprecated Use {@link android.view.ViewConfiguration#get(android.content.Context)} instead. 177 */ 178 @Deprecated 179 public ViewConfiguration() { 180 mEdgeSlop = EDGE_SLOP; 181 mFadingEdgeLength = FADING_EDGE_LENGTH; 182 mMinimumFlingVelocity = MINIMUM_FLING_VELOCITY; 183 mMaximumFlingVelocity = MAXIMUM_FLING_VELOCITY; 184 mScrollbarSize = SCROLL_BAR_SIZE; 185 mTouchSlop = TOUCH_SLOP; 186 mPagingTouchSlop = PAGING_TOUCH_SLOP; 187 mDoubleTapSlop = DOUBLE_TAP_SLOP; 188 mWindowTouchSlop = WINDOW_TOUCH_SLOP; 189 //noinspection deprecation 190 mMaximumDrawingCacheSize = MAXIMUM_DRAWING_CACHE_SIZE; 191 } 192 193 /** 194 * Creates a new configuration for the specified context. The configuration depends on 195 * various parameters of the context, like the dimension of the display or the density 196 * of the display. 197 * 198 * @param context The application context used to initialize this view configuration. 199 * 200 * @see #get(android.content.Context) 201 * @see android.util.DisplayMetrics 202 */ 203 private ViewConfiguration(Context context) { 204 final DisplayMetrics metrics = context.getResources().getDisplayMetrics(); 205 final float density = metrics.density; 206 207 mEdgeSlop = (int) (density * EDGE_SLOP + 0.5f); 208 mFadingEdgeLength = (int) (density * FADING_EDGE_LENGTH + 0.5f); 209 mMinimumFlingVelocity = (int) (density * MINIMUM_FLING_VELOCITY + 0.5f); 210 mMaximumFlingVelocity = (int) (density * MAXIMUM_FLING_VELOCITY + 0.5f); 211 mScrollbarSize = (int) (density * SCROLL_BAR_SIZE + 0.5f); 212 mTouchSlop = (int) (density * TOUCH_SLOP + 0.5f); 213 mPagingTouchSlop = (int) (density * PAGING_TOUCH_SLOP + 0.5f); 214 mDoubleTapSlop = (int) (density * DOUBLE_TAP_SLOP + 0.5f); 215 mWindowTouchSlop = (int) (density * WINDOW_TOUCH_SLOP + 0.5f); 216 217 // Size of the screen in bytes, in ARGB_8888 format 218 mMaximumDrawingCacheSize = 4 * metrics.widthPixels * metrics.heightPixels; 219 } 220 221 /** 222 * Returns a configuration for the specified context. The configuration depends on 223 * various parameters of the context, like the dimension of the display or the 224 * density of the display. 225 * 226 * @param context The application context used to initialize the view configuration. 227 */ 228 public static ViewConfiguration get(Context context) { 229 final DisplayMetrics metrics = context.getResources().getDisplayMetrics(); 230 final int density = (int) (100.0f * metrics.density); 231 232 ViewConfiguration configuration = sConfigurations.get(density); 233 if (configuration == null) { 234 configuration = new ViewConfiguration(context); 235 sConfigurations.put(density, configuration); 236 } 237 238 return configuration; 239 } 240 241 /** 242 * @return The width of the horizontal scrollbar and the height of the vertical 243 * scrollbar in pixels 244 * 245 * @deprecated Use {@link #getScaledScrollBarSize()} instead. 246 */ 247 @Deprecated 248 public static int getScrollBarSize() { 249 return SCROLL_BAR_SIZE; 250 } 251 252 /** 253 * @return The width of the horizontal scrollbar and the height of the vertical 254 * scrollbar in pixels 255 */ 256 public int getScaledScrollBarSize() { 257 return mScrollbarSize; 258 } 259 260 /** 261 * @return Duration of the fade when scrollbars fade away in milliseconds 262 */ 263 public static int getScrollBarFadeDuration() { 264 return SCROLL_BAR_FADE_DURATION; 265 } 266 267 /** 268 * @return Default delay before the scrollbars fade in milliseconds 269 */ 270 public static int getScrollDefaultDelay() { 271 return SCROLL_BAR_DEFAULT_DELAY; 272 } 273 274 /** 275 * @return the length of the fading edges in pixels 276 * 277 * @deprecated Use {@link #getScaledFadingEdgeLength()} instead. 278 */ 279 @Deprecated 280 public static int getFadingEdgeLength() { 281 return FADING_EDGE_LENGTH; 282 } 283 284 /** 285 * @return the length of the fading edges in pixels 286 */ 287 public int getScaledFadingEdgeLength() { 288 return mFadingEdgeLength; 289 } 290 291 /** 292 * @return the duration in milliseconds of the pressed state in child 293 * components. 294 */ 295 public static int getPressedStateDuration() { 296 return PRESSED_STATE_DURATION; 297 } 298 299 /** 300 * @return the duration in milliseconds before a press turns into 301 * a long press 302 */ 303 public static int getLongPressTimeout() { 304 return LONG_PRESS_TIMEOUT; 305 } 306 307 /** 308 * @return the duration in milliseconds we will wait to see if a touch event 309 * is a tap or a scroll. If the user does not move within this interval, it is 310 * considered to be a tap. 311 */ 312 public static int getTapTimeout() { 313 return TAP_TIMEOUT; 314 } 315 316 /** 317 * @return the duration in milliseconds we will wait to see if a touch event 318 * is a jump tap. If the user does not move within this interval, it is 319 * considered to be a tap. 320 */ 321 public static int getJumpTapTimeout() { 322 return JUMP_TAP_TIMEOUT; 323 } 324 325 /** 326 * @return the duration in milliseconds between the first tap's up event and 327 * the second tap's down event for an interaction to be considered a 328 * double-tap. 329 */ 330 public static int getDoubleTapTimeout() { 331 return DOUBLE_TAP_TIMEOUT; 332 } 333 334 /** 335 * @return Inset in pixels to look for touchable content when the user touches the edge of the 336 * screen 337 * 338 * @deprecated Use {@link #getScaledEdgeSlop()} instead. 339 */ 340 @Deprecated 341 public static int getEdgeSlop() { 342 return EDGE_SLOP; 343 } 344 345 /** 346 * @return Inset in pixels to look for touchable content when the user touches the edge of the 347 * screen 348 */ 349 public int getScaledEdgeSlop() { 350 return mEdgeSlop; 351 } 352 353 /** 354 * @return Distance a touch can wander before we think the user is scrolling in pixels 355 * 356 * @deprecated Use {@link #getScaledTouchSlop()} instead. 357 */ 358 @Deprecated 359 public static int getTouchSlop() { 360 return TOUCH_SLOP; 361 } 362 363 /** 364 * @return Distance a touch can wander before we think the user is scrolling in pixels 365 */ 366 public int getScaledTouchSlop() { 367 return mTouchSlop; 368 } 369 370 /** 371 * @return Distance a touch can wander before we think the user is scrolling a full page 372 * in dips 373 */ 374 public int getScaledPagingTouchSlop() { 375 return mPagingTouchSlop; 376 } 377 378 /** 379 * @return Distance between the first touch and second touch to still be 380 * considered a double tap 381 * @deprecated Use {@link #getScaledDoubleTapSlop()} instead. 382 * @hide The only client of this should be GestureDetector, which needs this 383 * for clients that still use its deprecated constructor. 384 */ 385 @Deprecated 386 public static int getDoubleTapSlop() { 387 return DOUBLE_TAP_SLOP; 388 } 389 390 /** 391 * @return Distance between the first touch and second touch to still be 392 * considered a double tap 393 */ 394 public int getScaledDoubleTapSlop() { 395 return mDoubleTapSlop; 396 } 397 398 /** 399 * @return Distance a touch must be outside the bounds of a window for it 400 * to be counted as outside the window for purposes of dismissing that 401 * window. 402 * 403 * @deprecated Use {@link #getScaledWindowTouchSlop()} instead. 404 */ 405 @Deprecated 406 public static int getWindowTouchSlop() { 407 return WINDOW_TOUCH_SLOP; 408 } 409 410 /** 411 * @return Distance a touch must be outside the bounds of a window for it 412 * to be counted as outside the window for purposes of dismissing that 413 * window. 414 */ 415 public int getScaledWindowTouchSlop() { 416 return mWindowTouchSlop; 417 } 418 419 /** 420 * @return Minimum velocity to initiate a fling, as measured in pixels per second. 421 * 422 * @deprecated Use {@link #getScaledMinimumFlingVelocity()} instead. 423 */ 424 @Deprecated 425 public static int getMinimumFlingVelocity() { 426 return MINIMUM_FLING_VELOCITY; 427 } 428 429 /** 430 * @return Minimum velocity to initiate a fling, as measured in pixels per second. 431 */ 432 public int getScaledMinimumFlingVelocity() { 433 return mMinimumFlingVelocity; 434 } 435 436 /** 437 * @return Maximum velocity to initiate a fling, as measured in pixels per second. 438 * 439 * @deprecated Use {@link #getScaledMaximumFlingVelocity()} instead. 440 */ 441 @Deprecated 442 public static int getMaximumFlingVelocity() { 443 return MAXIMUM_FLING_VELOCITY; 444 } 445 446 /** 447 * @return Maximum velocity to initiate a fling, as measured in pixels per second. 448 */ 449 public int getScaledMaximumFlingVelocity() { 450 return mMaximumFlingVelocity; 451 } 452 453 /** 454 * The maximum drawing cache size expressed in bytes. 455 * 456 * @return the maximum size of View's drawing cache expressed in bytes 457 * 458 * @deprecated Use {@link #getScaledMaximumDrawingCacheSize()} instead. 459 */ 460 @Deprecated 461 public static int getMaximumDrawingCacheSize() { 462 //noinspection deprecation 463 return MAXIMUM_DRAWING_CACHE_SIZE; 464 } 465 466 /** 467 * The maximum drawing cache size expressed in bytes. 468 * 469 * @return the maximum size of View's drawing cache expressed in bytes 470 */ 471 public int getScaledMaximumDrawingCacheSize() { 472 return mMaximumDrawingCacheSize; 473 } 474 475 /** 476 * The amount of time that the zoom controls should be 477 * displayed on the screen expressed in milliseconds. 478 * 479 * @return the time the zoom controls should be visible expressed 480 * in milliseconds. 481 */ 482 public static long getZoomControlsTimeout() { 483 return ZOOM_CONTROLS_TIMEOUT; 484 } 485 486 /** 487 * The amount of time a user needs to press the relevant key to bring up 488 * the global actions dialog. 489 * 490 * @return how long a user needs to press the relevant key to bring up 491 * the global actions dialog. 492 */ 493 public static long getGlobalActionKeyTimeout() { 494 return GLOBAL_ACTIONS_KEY_TIMEOUT; 495 } 496 497 /** 498 * The amount of friction applied to scrolls and flings. 499 * 500 * @return A scalar dimensionless value representing the coefficient of 501 * friction. 502 */ 503 public static float getScrollFriction() { 504 return SCROLL_FRICTION; 505 } 506} 507