最短距离

android中的ViewConfiguration.get(getContext())
.getScaledTouchSlop(),有什么用了?
其实他就是一个触发移动事件的最短距离(用户最好的体检距离),如果小于这个距离就不触发移动控件,如viewpager就是用这个距离来判断用户是否翻页。

**

源码如下:

**

In some cases, you will need to express dimensions in dp and then convert them to pixels. Imagine an application in which a scroll gesture is recognized after the user's finger has moved by at least 16 pixels. On a baseline screen, the user will have to move his finger by 16 pixels / 160 dpi = 1/10th of an inch (or 2.5 mm) before the gesture is recognized. On a device with a high (240) density display, the user will move his finger by only 16 pixels / 240 dpi = 1/15th of an inch (or 1.7 mm.) The distance is much shorter and the application thus appears more sensitive to the user. To fix this issue, the gesture threshold must be expressed in the code in dp and then converted to actual pixels.

// The gesture threshold expressed in dp
private static final float GESTURE_THRESHOLD_DP = 16.0f;
// Convert the dps to pixels
final float scale = getContext().getResources().getDisplayMetrics().density;
mGestureThreshold = (int) (GESTURE_THRESHOLD_DP * scale + 0.5f);
// Use mGestureThreshold as a distance in pixels

The android.util.DisplayMetrics.density field specifies the the scale factor you must use to convert dps to pixels according to the current screen density. You can access the current screen's metrics through a Context or Activity. On a medium (160) density screen, DisplayMetrics.density equals "1.0", whereas on a high (240) density screen it equals "1.5". You can refer to the documentation of the DisplayMetrics class for details.

The ViewConfiguration class can be used to access the most common distances, speeds, and times used in the Android framework. For instance, the distance in pixels used by the framework as the scroll threshold can be obtained as follows:
ViewConfiguration.get(aContext).getScaledTouchSlop()
Methods starting with the getScaled prefix are guaranteed to return a value in pixels that will display properly regardless of the current screen density.

从上面的源码可以看出,这个常量是更具不同的手机做了不同的体验

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值