Android使用TouchDelegate增加View的觸摸范圍
本文為大家分享了Android使用TouchDelegate增加View觸摸范圍的方法,供大家參考,具體內(nèi)容如下
還不知道TouchDelegate這個東西的可以先看一下API,這里大致說一下它的作用:假設(shè)有兩個View,分別是v1,v2,我們可以通過v1的setTouchDelegate(bounds, v2)來委派觸摸事件,其中bounds是一個Rect。v1中,落在這個范圍的TouchEvent都會傳給v2。
既然是這樣,那我們可以通過設(shè)置某個view的parent的touchDelegate來達到擴大這個view觸摸范圍的目的。關(guān)鍵是什么時候去執(zhí)行parent.setTouchDelegate()方法呢?要設(shè)置這個委派,必須得知道當(dāng)前view大小以及它在parent的位置。而這些數(shù)據(jù)都是在onLayout才能確定(注:如果不是自定義View,只是在Activity中設(shè)置,請將這些操作置于onWindowFocusChanged()方法中)。至此,實現(xiàn)的思路已經(jīng)很清晰了,我們通過自定義一個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實現(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));
}
}
}
然后在具體要使用到這個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只能設(shè)置一個觸摸委派,設(shè)置多個時,只有最后設(shè)置的child有效。如果希望一個view能設(shè)置多個委派,需要再自定義parent,具體方法可參考:鏈接地址
總而言之,要觸發(fā)委派,必須保證parent接收到了觸摸事件,并且落在了你定義的范圍內(nèi)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android判斷touch事件點是否在view范圍內(nèi)的方法
- Android從源碼的角度徹底理解事件分發(fā)機制的解析(下)
- Android從源碼的角度徹底理解事件分發(fā)機制的解析(上)
- 解析Android點擊事件分發(fā)機制
- Android View的事件分發(fā)詳解
- android事件分發(fā)機制的實現(xiàn)原理
- Android事件分發(fā)機制的詳解
- 談?wù)剬ndroid View事件分發(fā)機制的理解
- Android事件分發(fā)機制(上) ViewGroup的事件分發(fā)
- 如何自己實現(xiàn)Android View Touch事件分發(fā)流程
相關(guān)文章
Android實現(xiàn)多點觸控,自由縮放圖片的實例代碼
本篇文章主要介紹了Android實現(xiàn)多點觸控,自由縮放圖片的實例代碼,可以自由地對圖片進行縮放和移動,非常具有實用價值,需要的朋友可以參考下。2016-12-12
Android中將一個圖片切割成多個圖片的實現(xiàn)方法
有種場景,我們想將一個圖片切割成多個圖片。比如我們在開發(fā)一個拼圖的游戲,就首先要對圖片進行切割2013-05-05
Android簡單實現(xiàn)動態(tài)權(quán)限獲取相機權(quán)限及存儲空間等多權(quán)限
這篇文章主要介紹了Android簡單實現(xiàn)動態(tài)權(quán)限獲取相機權(quán)限及存儲空間等多權(quán)限,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07
APP添加CNZZ統(tǒng)計插件教程 Android版添加phonegap
這篇文章主要介紹了APP添加CNZZ統(tǒng)計插件教程,Android版添加phonegap,感興趣的小伙伴們可以參考一下2015-12-12

