Android使用TouchDelegate增加View的触摸范围

所属分类: 软件编程 / Android 阅读数: 50
收藏 0 赞 0 分享

本文为大家分享了Android使用TouchDelegate增加View触摸范围的方法,供大家参考,具体内容如下

还不知道TouchDelegate这个东西的可以先看一下API,这里大致说一下它的作用:假设有两个View,分别是v1,v2,我们可以通过v1的setTouchDelegate(bounds, v2)来委派触摸事件,其中bounds是一个Rect。v1中,落在这个范围的TouchEvent都会传给v2。

既然是这样,那我们可以通过设置某个view的parent的touchDelegate来达到扩大这个view触摸范围的目的。关键是什么时候去执行parent.setTouchDelegate()方法呢?要设置这个委派,必须得知道当前view大小以及它在parent的位置。而这些数据都是在onLayout才能确定(注:如果不是自定义View,只是在Activity中设置,请将这些操作置于onWindowFocusChanged()方法中)。至此,实现的思路已经很清晰了,我们通过自定义一个Button来检验一下,下面开始上代码:

为了方便在xml中使用我们自定义的View,并且可以自定义扩大的触摸范围,我们再自定义一个attrs,res/values/attrs.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
  <declare-styleable name="LargeTouchableAreaView"> 
    <attr name="addition" format="dimension" /> 
    <attr name="additionBottom" format="dimension" /> 
    <attr name="additionLeft" format="dimension" /> 
    <attr name="additionRight" format="dimension" /> 
    <attr name="additionTop" format="dimension" /> 
  </declare-styleable> 
</resources> 

Button实现:

public class LargeTouchableAreasButton extends Button { 
  private final int TOUCH_ADDITION = 0; 
  private int mTouchAdditionBottom = 0; 
  private int mTouchAdditionLeft = 0; 
  private int mTouchAdditionRight = 0; 
  private int mTouchAdditionTop = 0; 
  private int mPreviousLeft = -1; 
  private int mPreviousRight = -1; 
  private int mPreviousBottom = -1; 
  private int mPreviousTop = -1; 
 
  public LargeTouchableAreasButton(Context context) { 
    super(context); 
  } 
 
  public LargeTouchableAreasButton(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context, attrs); 
  } 
 
  public LargeTouchableAreasButton(Context context, AttributeSet attrs, 
      int defStyle) { 
    super(context, attrs, defStyle); 
    init(context, attrs); 
  } 
 
  private void init(Context context, AttributeSet attrs) { 
    TypedArray a = context.obtainStyledAttributes(attrs, 
        R.styleable.LargeTouchableAreaView); 
    int addition = (int) a.getDimension( 
        R.styleable.LargeTouchableAreaView_addition, TOUCH_ADDITION); 
    mTouchAdditionBottom = addition; 
    mTouchAdditionLeft = addition; 
    mTouchAdditionRight = addition; 
    mTouchAdditionTop = addition; 
    mTouchAdditionBottom = (int) a.getDimension( 
        R.styleable.LargeTouchableAreaView_additionBottom, 
        mTouchAdditionBottom); 
    mTouchAdditionLeft = (int) a.getDimension( 
        R.styleable.LargeTouchableAreaView_additionLeft, 
        mTouchAdditionLeft); 
    mTouchAdditionRight = (int) a.getDimension( 
        R.styleable.LargeTouchableAreaView_additionRight, 
        mTouchAdditionRight); 
    mTouchAdditionTop = (int) a.getDimension( 
        R.styleable.LargeTouchableAreaView_additionTop, 
        mTouchAdditionTop); 
    a.recycle(); 
  } 
 
  @Override 
  protected void onLayout(boolean changed, int left, int top, int right, 
      int bottom) { 
    super.onLayout(changed, left, top, right, bottom); 
    if (left != mPreviousLeft || top != mPreviousTop 
        || right != mPreviousRight || bottom != mPreviousBottom) { 
      mPreviousLeft = left; 
      mPreviousTop = top; 
      mPreviousRight = right; 
      mPreviousBottom = bottom; 
      final View parent = (View) this.getParent(); 
      parent.setTouchDelegate(new TouchDelegate(new Rect(left 
          - mTouchAdditionLeft, top - mTouchAdditionTop, right 
          + mTouchAdditionRight, bottom + mTouchAdditionBottom), this)); 
    } 
  } 
 
} 

然后在具体要使用到这个Button的xml中加上以下代码:

xmlns:lta="http://schemas.android.com/apk/res/com.xxx.xxx" 

其中"lta"这个名字可以随便取,最后的是你的app包名。
最后在这个Button中定义希望增大的尺寸:

<com.xxx.LargeTouchableAreasButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    lta:addition="30dp" /> 

大功告成。

但这个自定义的View并不是完美的,还存在以下问题:

1、必须保证parent足够大,如果自定义的范围超出parent的大小,则超出的那部分无效。

2、一个parent只能设置一个触摸委派,设置多个时,只有最后设置的child有效。如果希望一个view能设置多个委派,需要再自定义parent,具体方法可参考:链接地址

总而言之,要触发委派,必须保证parent接收到了触摸事件,并且落在了你定义的范围内。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android框架学习之Volley和Glide详解

这篇文章主要给大家介绍了关于Android框架学习之Volley和Glide的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android中Fragment的基本用法示例总结

Fragment是activity的界面中的一部分或一种行为,下面这篇文章主要给大家介绍了关于Android中Fragment的基本用法的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
收藏 0 赞 0 分享

Android.mk引入第三方jar包和so库文件的方法

这篇文章主要介绍了Android.mk引入第三方jar包和so库文件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿微信录制小视频

这篇文章主要为大家详细介绍了Android仿微信录制小视频,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

android实现一键锁屏和一键卸载的方法实例

这篇文章主要给大家介绍了关于android如何实现一键锁屏和一键卸载的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

Android手势密码--设置和校验功能的实现代码

这篇文章主要介绍了Android手势密码--设置和校验功能的实现代码,非常不错,具有一定的参考校验价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Kotlin学习笔记之const val与val

这篇文章主要给大家介绍了关于Kotlin学习笔记之const val与val的相关资料,并给大家介绍了const val和val区别以及Kotlin中var和val的区别,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android实现调用系统分享功能示例的总结

这篇文章主要介绍了通过Android调用系统分享文本信息、单张图片、多个文件和指定分享到微信、QQ,同时分享图片和文字的功能示例,小编觉得挺不错,一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android自定义view实现输入控件

这篇文章主要为大家详细介绍了Android自定义view实现输入控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码

这篇文章主要介绍了Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码的实例代码,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多