Android学习笔记——View事件分发(上)

来源:互联网 发布:ios Linux 编辑:程序博客网 时间:2024/04/28 16:53
<pre name="code" class="java"><pre name="code" class="java"><pre name="code" class="java"> if (li != null && li.mOnTouchListener != null                    && (mViewFlags & ENABLED_MASK) == ENABLED                    && li.mOnTouchListener.onTouch(this, event)) {                result = true;            }
/** * view 的事件分发 *  1.返回true,说明可以响应down事件和up事件 *  2.返回false,只会响应down事件,不会响应up事件 *  在downs事件如果能处理(消费)当前事件,那么在up的时候,也会把事件传递给当前的view *  在downs事件如果不能处理(消费)当前事件,那么在up的时候,也不会把事件传递给当前的view *  a.判断mOnTouchListener是否为空 *  b.  判断当前控件是否可用 *  c.  判断view的onTouch *  d.  如果一个返回为false, 那么就会调用onTouchEvent * @author Administrator * */
</pre><pre name="code" class="java">一段源码 <span style="font-family: Arial, Helvetica, sans-serif;">onTouchEvent</span>
    public boolean onTouchEvent(MotionEvent event) {        final float x = event.getX();        final float y = event.getY();        final int viewFlags = mViewFlags;        if ((viewFlags & ENABLED_MASK) == DISABLED) {            if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {                setPressed(false);            }            // A disabled view that is clickable still consumes the touch            // events, it just doesn't respond to them.            return (((viewFlags & CLICKABLE) == CLICKABLE ||                    (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));        }        if (mTouchDelegate != null) {            if (mTouchDelegate.onTouchEvent(event)) {                return true;            }        }        if (((viewFlags & CLICKABLE) == CLICKABLE ||                (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {            switch (event.getAction()) {                case MotionEvent.ACTION_UP:                    boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;                    if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {                        // take focus if we don't have it already and we should in                        // touch mode.                        boolean focusTaken = false;                        if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {                            focusTaken = requestFocus();                        }                        if (prepressed) {                            // The button is being released before we actually                            // showed it as pressed.  Make it show the pressed                            // state now (before scheduling the click) to ensure                            // the user sees it.                            setPressed(true, x, y);                       }                        if (!mHasPerformedLongPress) {                            // This is a tap, so remove the longpress check                            removeLongPressCallback();                            // Only perform take click actions if we were in the pressed state                            if (!focusTaken) {                                // Use a Runnable and post this rather than calling                                // performClick directly. This lets other visual state                                // of the view update before click actions start.                                if (mPerformClick == null) {                                    mPerformClick = new PerformClick();                                }                                if (!post(mPerformClick)) {                                    performClick();                                }                            }                        }                        if (mUnsetPressedState == null) {                            mUnsetPressedState = new UnsetPressedState();                        }                        if (prepressed) {                            postDelayed(mUnsetPressedState,                                    ViewConfiguration.getPressedStateDuration());                        } else if (!post(mUnsetPressedState)) {                            // If the post failed, unpress right now                            mUnsetPressedState.run();                        }                        removeTapCallback();                    }                    break;                case MotionEvent.ACTION_DOWN:                    mHasPerformedLongPress = false;                    if (performButtonActionOnTouchDown(event)) {                        break;                    }                    // Walk up the hierarchy to determine if we're inside a scrolling container.                    boolean isInScrollingContainer = isInScrollingContainer();                    // For views inside a scrolling container, delay the pressed feedback for                    // a short period in case this is a scroll.                    if (isInScrollingContainer) {                        mPrivateFlags |= PFLAG_PREPRESSED;                        if (mPendingCheckForTap == null) {                            mPendingCheckForTap = new CheckForTap();                        }                        mPendingCheckForTap.x = event.getX();                        mPendingCheckForTap.y = event.getY();                        postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());                    } else {                        // Not inside a scrolling container, so show the feedback right away                        setPressed(true, x, y);                        checkForLongClick(0);                    }                    break;                case MotionEvent.ACTION_CANCEL:                    setPressed(false);                    removeTapCallback();                    removeLongPressCallback();                    break;                case MotionEvent.ACTION_MOVE:                    drawableHotspotChanged(x, y);                    // Be lenient about moving outside of buttons                    if (!pointInView(x, y, mTouchSlop)) {                        // Outside button                        removeTapCallback();                        if ((mPrivateFlags & PFLAG_PRESSED) != 0) {                            // Remove any future long press/tap checks                            removeLongPressCallback();                            setPressed(false);                        }                    }                    break;            }            return true;        }        return false;    }
==================
从ImageView和Button分别解析各自的事件分发。down?up?
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); iv_img = (ImageView) findViewById(R.id.img); button = (Button) findViewById(R.id.button); //点击ImageView 按钮,只有一次 /** * return true; 说明能销毁这个事件 * return false; down处理不了,up 事件就不会传入 */ iv_img.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {System.out.println("-iv_img--onTouch-" +event.getAction());// return false; 只能处理down事件,只打印一次,imageView不具备点击事件return true;}}); //button有很多次 button.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {System.out.println("-button--onTouch-" +event.getAction());return true;}}); //点击事件 iv_img.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {System.out.println("-iv_img--onClick-");}}); System.out.println("--iv_img-----" + iv_img.isEnabled()); /** * ListenerInfo li = mListenerInfo; if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED //p判断组件是否可用 && li.mOnTouchListener.onTouch(this, event)) { result = true; } */ //点击事件 button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {System.out.println("-button--onClick-");}}); System.out.println("--button-----" + button.isEnabled()); }



0 0
原创粉丝点击