Android 开发 Tip 9 -- TouchDelegate

来源:互联网 发布:seo全网优化指南 编辑:程序博客网 时间:2024/06/08 02:15

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/71115773


View的 onTouchEvent() 函数中有这样一段!

public boolean onTouchEvent(MotionEvent event) {        // ...        if (mTouchDelegate != null) {            if (mTouchDelegate.onTouchEvent(event)) {                return true;            }        }        // ...        return false;    }

源码对 TouchDelegate 有一段比较长的注释:

/** * Helper class to handle situations where you want a view to have a larger touch area than its * actual view bounds. The view whose touch area is changed is called the delegate view. This * class should be used by an ancestor of the delegate. To use a TouchDelegate, first create an * instance that specifies the bounds that should be mapped to the delegate and the delegate * view itself. * <p> * The ancestor should then forward all of its touch events received in its * {@link android.view.View#onTouchEvent(MotionEvent)} to {@link #onTouchEvent(MotionEvent)}. * </p> */public class TouchDelegate {

大概意思就是说,可以拓展view的触摸区域

public TouchDelegate(Rect bounds, View delegateView) {        mBounds = bounds;        mSlop = ViewConfiguration.get(delegateView.getContext()).getScaledTouchSlop();        mSlopBounds = new Rect(bounds);        mSlopBounds.inset(-mSlop, -mSlop);        mDelegateView = delegateView;    }

从构造函数可以看出,需要传入一个区域和一个被代理的view


官网有简单的应用demo和步骤说明!

https://developer.android.com/training/gestures/viewgroup.html#delegate


布局文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/root_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.jacksen.demo.view.dispatch.TestTouchDelegateActivity">    <!-- 用来指示扩展的区域 -->    <View        android:id="@+id/area_indicator"        android:layout_width="48dp"        android:layout_height="48dp"        android:layout_centerInParent="true"        android:background="#1000" />    <Button        android:id="@+id/touch_delegate_btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="TouchDelegate" /></RelativeLayout>
@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_test_touch_delegate);        area_indicator = findViewById(R.id.area_indicator);        root_layout = (RelativeLayout) findViewById(R.id.root_layout);        touch_delegate_btn = (Button) findViewById(R.id.touch_delegate_btn);        offset = (int) (getResources().getDisplayMetrics().density * 50 + 0.5f);        root_layout.post(new Runnable() {            @Override            public void run() {                Rect bound = new Rect();                touch_delegate_btn.getHitRect(bound);                bound.left -= offset;                bound.top -= offset;                bound.right += offset;                bound.bottom += offset;                TouchDelegate touchDelegate = new TouchDelegate(bound, touch_delegate_btn);                if (View.class.isInstance(touch_delegate_btn.getParent())) {                    ((View) touch_delegate_btn.getParent()).setTouchDelegate(touchDelegate);                }                // 设置展示扩展区域的大小                ViewGroup.LayoutParams params = area_indicator.getLayoutParams();                params.width = bound.width();                params.height = bound.height();                area_indicator.setLayoutParams(params);            }        });    }

点击button周围的区域也可以触发button的点击效果

效果图:

这里写图片描述

1 0
原创粉丝点击