android 事件分发验证

android事件分发流程很长,从驱动到EventHub到Input模块再到view。这里不做那么复杂的验证,只是验证应用中需要Override的接口,以在不同的时机处理。

具体来说是这几个

ViewGroup、View

Activity:onTouchEvent,dispatchTouchEvent

ViewGroup:dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent(继承了view)

View:onTouchEvent,dispatchTouchEvent


先上个代码:

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "AndroidDemo";
    private Button mClick;
    private LinearLayout mLayout = null;
    private TextView mTv = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d(TAG, "Android Demo onCreate");
        createViewByCode();
    }

    private void createViewByCode() {
        mLayout = new SLinearLayout(this);
        mLayout.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mLayout.setOrientation(LinearLayout.VERTICAL);

        mTv = new STextView(this);
        mTv.setText("Hello code");
        mTv.setTextSize(19);
        LinearLayout.LayoutParams  params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        mLayout.addView(mTv, params);

        setContentView(mLayout);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(TAG, "activity onTouchEvent");
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d(TAG, "activity dispatchTouchEvent");
        return super.dispatchTouchEvent(ev);
    }
}

class SLinearLayout extends LinearLayout {

    SLinearLayout(Context context) {
        super(context);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d("AndroidDemo", "layout dispatchTouchEvent");
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d("AndroidDemo", "layout onInterceptTouchEvent");
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("AndroidDemo", "layout onTouchEvent");
        return super.onTouchEvent(event);
    }
}

class STextView extends TextView {

    STextView(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("AndroidDemo", "tv onTouchEvent");
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.d("AndroidDemo", "tv dispatchTouchEvent");
        return super.dispatchTouchEvent(event);
    }
}

模拟器截图



点击后日志打印

09-12 03:38:34.744 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: activity dispatchTouchEvent
09-12 03:38:34.744 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: layout dispatchTouchEvent
09-12 03:38:34.744 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: layout onInterceptTouchEvent
09-12 03:38:34.744 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: tv dispatchTouchEvent
09-12 03:38:34.744 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: tv onTouchEvent
09-12 03:38:34.744 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: layout onTouchEvent
09-12 03:38:34.744 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: activity onTouchEvent
09-12 03:38:34.791 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: activity dispatchTouchEvent
09-12 03:38:34.791 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: activity onTouchEvent

结果分析待续


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值