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
- Android 开发 Tip 9 -- TouchDelegate
- Android TouchDelegate
- Android TouchDelegate
- Android开发tip
- Android 开发 Tip 13 -- generateViewId
- Android 开发 Tip 14 -- Singleton
- TouchDelegate
- android之view的TouchDelegate
- 关于Android TouchDelegate源码解析
- 关于Android TouchDelegate源码解析
- Android TouchDelegate 扩大点击区域
- Android开发layout中的小tip
- Android 开发 Tip 7 -- volatile & synchronized
- Android 开发 Tip 8 -- clipToPadding & clipChildren
- Android 开发 Tip 12 -- 各种Drawable
- Android中TouchDelegate的用法浅析
- Android 扩大触摸的触发区域 TouchDelegate
- Android 开发 Tip 1 -- android:state_checked & android:state_activated
- LintCode 68 二叉树的中序遍历
- spring cloud学习笔记
- Java知识点(四)——多线程
- 551. Student Attendance Record I
- 编译opencv+opencv_contrib源码实现KCF跟踪算法
- Android 开发 Tip 9 -- TouchDelegate
- 4870: [Shoi2017]组合数问题
- java之hashset与hashmap总结
- Python 中的闭包(2)—— 一个栗子
- 二分贪心 D题
- 边缘独立(marginal independent)的理解及举例
- poco-1.7.8p2-all安装
- java如何求最大子数组的和 蛮力算法 动态规划算法
- struts2