溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android使用TouchDelegate增加View的觸摸范圍

發(fā)布時(shí)間:2020-09-20 13:02:06 來(lái)源:腳本之家 閱讀:164 作者:sgwhp 欄目:移動(dòng)開(kāi)發(fā)

本文為大家分享了Android使用TouchDelegate增加View觸摸范圍的方法,供大家參考,具體內(nèi)容如下

還不知道TouchDelegate這個(gè)東西的可以先看一下API,這里大致說(shuō)一下它的作用:假設(shè)有兩個(gè)View,分別是v1,v2,我們可以通過(guò)v1的setTouchDelegate(bounds, v2)來(lái)委派觸摸事件,其中bounds是一個(gè)Rect。v1中,落在這個(gè)范圍的TouchEvent都會(huì)傳給v2。

既然是這樣,那我們可以通過(guò)設(shè)置某個(gè)view的parent的touchDelegate來(lái)達(dá)到擴(kuò)大這個(gè)view觸摸范圍的目的。關(guān)鍵是什么時(shí)候去執(zhí)行parent.setTouchDelegate()方法呢?要設(shè)置這個(gè)委派,必須得知道當(dāng)前view大小以及它在parent的位置。而這些數(shù)據(jù)都是在onLayout才能確定(注:如果不是自定義View,只是在A(yíng)ctivity中設(shè)置,請(qǐng)將這些操作置于onWindowFocusChanged()方法中)。至此,實(shí)現(xiàn)的思路已經(jīng)很清晰了,我們通過(guò)自定義一個(gè)Button來(lái)檢驗(yàn)一下,下面開(kāi)始上代碼:

為了方便在xml中使用我們自定義的View,并且可以自定義擴(kuò)大的觸摸范圍,我們?cè)僮远x一個(gè)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實(shí)現(xiàn):

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)); 
    } 
  } 
 
} 

然后在具體要使用到這個(gè)Button的xml中加上以下代碼:

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

其中"lta"這個(gè)名字可以隨便取,最后的是你的app包名。
最后在這個(gè)Button中定義希望增大的尺寸:

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

大功告成。

但這個(gè)自定義的View并不是完美的,還存在以下問(wèn)題:

1、必須保證parent足夠大,如果自定義的范圍超出parent的大小,則超出的那部分無(wú)效。

2、一個(gè)parent只能設(shè)置一個(gè)觸摸委派,設(shè)置多個(gè)時(shí),只有最后設(shè)置的child有效。如果希望一個(gè)view能設(shè)置多個(gè)委派,需要再自定義parent,具體方法可參考:鏈接地址

總而言之,要觸發(fā)委派,必須保證parent接收到了觸摸事件,并且落在了你定義的范圍內(nèi)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI