ViewConfiguration.getScaledTouchSlop () 用法

本文深入探讨了Android中ScrollView的工作原理及其构造方法。通过源代码分析了关键参数的作用,包括焦点管理、滑动识别距离、加速度等,并介绍了如何自定义View。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0225/907.html

getScaledTouchSlop是一个距离,表示滑动的时候,手的移动要大于这个距离才开始移动控件。如果小于这个距离就不触发移动控件,如viewpager就是用这个距离来判断用户是否翻页

ViewConfiguration滑动参数设置类:

/**    
  * 包含了方法和标准的常量用来设置UI的超时、大小和距离    
  */ 
 public class ViewConfiguration {     
     // 设定水平滚动条的宽度和垂直滚动条的高度,单位是像素px     
     private static final int SCROLL_BAR_SIZE = 10;     

     //定义滚动条逐渐消失的时间,单位是毫秒     
     private static final int SCROLL_BAR_FADE_DURATION = 250;     

     // 默认的滚动条多少秒之后消失,单位是毫秒     
     private static final int SCROLL_BAR_DEFAULT_DELAY = 300;     

     // 定义边缘地方褪色的长度     
     private static final int FADING_EDGE_LENGTH = 12;     

     //定义子控件按下状态的持续事件     
     private static final int PRESSED_STATE_DURATION = 125;     

     //定义一个按下状态转变成长按状态的转变时间     
     private static final int LONG_PRESS_TIMEOUT = 500;     

     //定义用户在按住适当按钮,弹出全局的对话框的持续时间     
     private static final int GLOBAL_ACTIONS_KEY_TIMEOUT = 500;     

     //定义一个touch事件中是点击事件还是一个滑动事件所需的时间,如果用户在这个时间之内滑动,那么就认为是一个点击事件     
     private static final int TAP_TIMEOUT = 115;     

     /**    
      * Defines the duration in milliseconds we will wait to see if a touch event     
      * is a jump tap. If the user does not complete the jump tap within this interval, it is    
      * considered to be a tap.     
      */ 
     //定义一个touch事件时候是一个点击事件。如果用户在这个时间内没有完成这个点击,那么就认为是一个点击事件     
     private static final int JUMP_TAP_TIMEOUT = 500;     

     //定义双击事件的间隔时间     
     private static final int DOUBLE_TAP_TIMEOUT = 300;     

     //定义一个缩放控制反馈到用户界面的时间     
     private static final int ZOOM_CONTROLS_TIMEOUT = 3000;     

     /**    
      * Inset in pixels to look for touchable content when the user touches the edge of the screen    
      */ 
     private static final int EDGE_SLOP = 12;     

     /**    
      * Distance a touch can wander before we think the user is scrolling in pixels    
      */ 
     private static final int TOUCH_SLOP = 16;     

     /**    
      * Distance a touch can wander before we think the user is attempting a paged scroll    
      * (in dips)    
      */ 
     private static final int PAGING_TOUCH_SLOP = TOUCH_SLOP * 2;     

     /**    
      * Distance between the first touch and second touch to still be considered a double tap    
      */ 
     private static final int DOUBLE_TAP_SLOP = 100;     

     /**    
      * Distance a touch needs to be outside of a window's bounds for it to    
      * count as outside for purposes of dismissing the window.    
      */ 
     private static final int WINDOW_TOUCH_SLOP = 16;     

    //用来初始化fling的最小速度,单位是每秒多少像素     
     private static final int MINIMUM_FLING_VELOCITY = 50;     

     //用来初始化fling的最大速度,单位是每秒多少像素     
     private static final int MAXIMUM_FLING_VELOCITY = 4000;     

     //视图绘图缓存的最大尺寸,以字节表示。在ARGB888格式下,这个尺寸应至少等于屏幕的大小     
     @Deprecated     
     private static final int MAXIMUM_DRAWING_CACHE_SIZE = 320 * 480 * 4; // HVGA screen, ARGB8888     

     //flings和scrolls摩擦力度大小的系数     
     private static float SCROLL_FRICTION = 0.015f;     

     /**    
      * Max distance to over scroll for edge effects    
      */ 
     private static final int OVERSCROLL_DISTANCE = 0;     

     /**    
      * Max distance to over fling for edge effects    
      */ 
     private static final int OVERFLING_DISTANCE = 4;     

 }

原文链接:http://blog.youkuaiyun.com/freedom13905149949/article/details/51661019

这篇博客的目的在于借助于ScrollView这个控件为大家讲解在我们平常的安卓开发过程经常使用的关于View的滑动知识

1、ScrollView的构造方法

public ScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initScrollView();

        final TypedArray a = context.obtainStyledAttributes(
                attrs, com.android.internal.R.styleable.ScrollView, defStyleAttr, defStyleRes);

        setFillViewport(a.getBoolean(R.styleable.ScrollView_fillViewport, false));

        a.recycle();
    }

大家发现ScrollView这个构造方法里面一共有四个参数,其实所有的View都是按照View的规则绘制出来的,这四个参数照样适用于其他View的自定义

2、initScrollView()这个方法的源代码:

private void initScrollView() {
        mScroller = new OverScroller(getContext());
        setFocusable(true);
        setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
        setWillNotDraw(false);
        final ViewConfiguration configuration = ViewConfiguration.get(mContext);
        mTouchSlop = configuration.getScaledTouchSlop();
        mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
        mOverscrollDistance = configuration.getScaledOverscrollDistance();
        mOverflingDistance = configuration.getScaledOverflingDistance();
    }

详细解释这个类里面用到的API

//这个方法的参数表示当前是否获得焦点 
setFocusable(boolean flag);

setDescendantFocusability(int flag);

//如果要重写这个View的onDraw()方法,设置这个标志位false会走重写过的方法 
setWillNotDraw(boolean flag);

ViewConfiguration

//能够识别的最小滑动举例 
ViewConfiguration.getScaledTouchSlop();

//最小加速度 
ViewConfigurationgetScaledMinimumFlingVelocity();

//最大加速度 
ViewConfiguration.getScaledMaximumFlingVelocity();

//滚动距离 
ViewConfiguration.getScaledOverscrollDistance();

//Filing距离 
ViewConfiguration.getScaledOverflingDistance();
2025-07-06 16:25:10.644 936-936 ViewConfiguration com.android.launcher3 E Tried to access UI constants from a non-visual Context:com.android.quickstep.inputconsumers.OtherActivityInputConsumer@d0ca171UI constants, such as display metrics or window metrics, must be accessed from Activity or other visual Context. Use an Activity or a Context created with Context#createWindowContext(int, Bundle), which are adjusted to the configuration and visual bounds of an area on screen java.lang.IllegalArgumentException: Tried to access UI constants from a non-visual Context:com.android.quickstep.inputconsumers.OtherActivityInputConsumer@d0ca171 at android.view.ViewConfiguration.get(ViewConfiguration.java:510) at com.android.quickstep.inputconsumers.OtherActivityInputConsumer.<init>(OtherActivityInputConsumer.java:163) at com.android.quickstep.TouchInteractionService.createOtherActivityInputConsumer(TouchInteractionService.java:709) at com.android.quickstep.TouchInteractionService.newBaseConsumer(TouchInteractionService.java:692) at com.android.quickstep.TouchInteractionService.newConsumer(TouchInteractionService.java:587) at com.android.quickstep.TouchInteractionService.onInputEvent(TouchInteractionService.java:493) at com.android.quickstep.TouchInteractionService.lambda$5N8OpQhfWV7_tkhenf8Ys3ECXWs(Unknown Source:0) at com.android.quickstep.-$$Lambda$TouchInteractionService$5N8OpQhfWV7_tkhenf8Ys3ECXWs.onInputEvent(Unknown Source:2) at com.android.systemui.shared.system.InputChannelCompat$InputEventReceiver$1.onInputEvent(InputChannelCompat.java:81) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:220) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:335) at android.os.Looper.loop(Looper.java:183) at android.app.ActivityThread.main(ActivityThread.java:7660) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
07-07
protected TouchHandler createTouchHandler() { return new TouchHandler() { private long mLastTouchDownTime = -1L; @Override public boolean onInterceptTouchEvent(MotionEvent event) { if (SPEW_LOGCAT) { Log.v(TAG, "NPVC onInterceptTouchEvent (" + event.getId() + "): (" + event.getX() + "," + event.getY() + ")"); } Log.e("lyt---->","---notificatio----11"); if (mBlockTouches || mQs.disallowPanelTouches()) { return false; } initDownStates(event); Log.e("lyt---->","----notifica---22->"); final ViewConfiguration configuration = ViewConfiguration.get(mView.getContext()); int mTouchSlop = configuration.getScaledTouchSlop(); if (mStatusBarStateController.getState() == StatusBarState.KEYGUARD) { Log.e("lyt---->","---触摸拦截->"+event.getActionMasked()); switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: mInitialTouchX = event.getX(); mInitialTouchY = event.getY(); break; case MotionEvent.ACTION_MOVE: Log.e("lyt---->","触摸滑动---"); float dx = event.getX() - mInitialTouchX; float dy = event.getY() - mInitialTouchY; // 判断是向上滑动 & 垂直方向为主 if (dy < -mTouchSlop && Math.abs(dy) > Math.abs(dx)) { Log.d("lyt---->", "拦截上滑解锁,交给 LockIconView"); return true; // 拦截该事件序列(会走到 onTouch) } Log.e("lyt---->","插拦截---》"+dx+"---->"+dy+"---->"+(mTouchSlop)); return false; } return false; } // Do not let touches go to shade or QS if the bouncer is visible, // but still let user swipe down to expand the panel, dismissing the bouncer. if (mCentralSurfaces.isBouncerShowing()) { return true; } if (mCommandQueue.panelsEnabled() && !mNotificationStackScrollLayoutController.isLongPressInProgress() && mHeadsUpTouchHelper.onInterceptTouchEvent(event)) { mMetricsLogger.count(COUNTER_PANEL_OPEN, 1); mMetricsLogger.count(COUNTER_PANEL_OPEN_PEEK, 1); return true; } if (!shouldQuickSettingsIntercept(mDownX, mDownY, 0) && mPulseExpansionHandler.onInterceptTouchEvent(event)) { return true; } if (!isFullyCollapsed() && onQsIntercept(event)) { if (DEBUG_LOGCAT) Log.d(TAG, "onQsIntercept true"); return true; } return super.onInterceptTouchEvent(event); } @Override public boolean onTouch(View v, MotionEvent event) { Log.e("lyt---->","--noto---touh--111-->"); if (event.getAction() == MotionEvent.ACTION_DOWN) { if (event.getDownTime() == mLastTouchDownTime) { // An issue can occur when swiping down after unlock, where multiple down // events are received in this handler with identical downTimes. Until the // source of the issue can be located, detect this case and ignore. // see b/193350347 Log.w(TAG, "Duplicate down event detected... ignoring"); return true; } mLastTouchDownTime = event.getDownTime(); } if (mStatusBarStateController.getState() == StatusBarState.KEYGUARD) { Log.e("lyt---->","---触摸拦截->"+event.getActionMasked()); if(mLockIconView != null){ mLockIconView.dispatchTouchEvent(event); } return true; } if (mBlockTouches || (mQsFullyExpanded && mQs != null && mQs.disallowPanelTouches())) { return false; } // Do not allow panel expansion if bouncer is scrimmed or showing over a dream, // otherwise user would be able to pull down QS or expand the shade. if (mCentralSurfaces.isBouncerShowingScrimmed() || mCentralSurfaces.isBouncerShowingOverDream()) { return false; } // Make sure the next touch won't the blocked after the current ends. if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) { mBlockingExpansionForCurrentTouch = false; } // When touch focus transfer happens, ACTION_DOWN->ACTION_UP may happen immediately // without any ACTION_MOVE event. // In such case, simply expand the panel instead of being stuck at the bottom bar. if (mLastEventSynthesizedDown && event.getAction() == MotionEvent.ACTION_UP) { expand(true /* animate */); } initDownStates(event); // If pulse is expanding already, let's give it the touch. There are situations // where the panel starts expanding even though we're also pulsing boolean pulseShouldGetTouch = (!mIsExpanding && !shouldQuickSettingsIntercept(mDownX, mDownY, 0)) || mPulseExpansionHandler.isExpanding(); if (pulseShouldGetTouch && mPulseExpansionHandler.onTouchEvent(event)) { // We're expanding all the other ones shouldn't get this anymore return true; } if (mListenForHeadsUp && !mHeadsUpTouchHelper.isTrackingHeadsUp() && !mNotificationStackScrollLayoutController.isLongPressInProgress() && mHeadsUpTouchHelper.onInterceptTouchEvent(event)) { mMetricsLogger.count(COUNTER_PANEL_OPEN_PEEK, 1); } boolean handled = false; if ((!mIsExpanding || mHintAnimationRunning) && !mQsExpanded && mBarState != StatusBarState.SHADE && !mDozing) { Log.e("lyt---->","----111----"); handled |= mAffordanceHelper.onTouchEvent(event); } if (mOnlyAffordanceInThisMotion) { return true; } handled |= mHeadsUpTouchHelper.onTouchEvent(event); if (!mHeadsUpTouchHelper.isTrackingHeadsUp() && handleQsTouch(event)) { if (DEBUG_LOGCAT) Log.d(TAG, "handleQsTouch true"); return true; } if (event.getActionMasked() == MotionEvent.ACTION_DOWN && isFullyCollapsed()) { mMetricsLogger.count(COUNTER_PANEL_OPEN, 1); handled = true; } if (event.getActionMasked() == MotionEvent.ACTION_DOWN && isFullyExpanded() && mStatusBarKeyguardViewManager.isShowing()) { mStatusBarKeyguardViewManager.updateKeyguardPosition(event.getX()); } handled |= super.onTouch(v, event); Log.e("lyt----->","---->"+(!mDozing || mPulsing || handled)); return !mDozing || mPulsing || handled; } }; }
05-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值