Android實現(xiàn)擴大View點擊區(qū)域的三種方式
在 Android 應用開發(fā)中,有時候需要擴大 View 的點擊區(qū)域以提高用戶交互的便利性,尤其是當視圖元素較小或用戶界面密集時。擴大點擊區(qū)域可以讓用戶更容易點擊目標,改善用戶體驗。以下提供幾種擴大點擊區(qū)域的思路。
方式一:增加padding
通過設置padding來增大點擊區(qū)域,如:
<TextView
android:id="@+id/tv_view_delegate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/gray_holo_light"
android:padding="20dp"
android:text="TouchDelegate2" />
上面的代碼通過在XML中設置padding來擴大點擊區(qū)域,當然也可以通過代碼設置setPadding來實現(xiàn)。雖然設置padding可以起到效果,但是如果使用不當可能會影響視圖的布局和外觀,比如對ImageView設置padding的話可能會擠壓其形狀,所以使用Padding擴大點擊區(qū)域時需要確保不影響視圖的布局和外觀。
方式二:TouchDelegate
TouchDelegate 類是 Android 中的一個輔助類,用于擴展 View 的觸摸區(qū)域,使其大于實際的 View 邊界。這對于增加某些 UI 元素的觸控便捷性非常有用,比如小按鈕。
TouchDelegate 使用示例:
/**
* 擴展方法,擴大點擊區(qū)域
* NOTE: 需要保證目標targetView有父View,否則無法擴大點擊區(qū)域
*
* @param expandSize 擴大的大小,單位px
*/
fun View.expandTouchView(expandSize: Int = 10.dp2px()) {
val parentView = (parent as? View)
parentView?.post {
val rect = Rect()
getHitRect(rect) //getHitRect(rect)將視圖在父容器中所占據(jù)的區(qū)域存儲到rect中。
log("rect = $rect")
rect.left -= expandSize
rect.top -= expandSize
rect.right += expandSize
rect.bottom += expandSize
log("expandRect = $rect")
parentView.touchDelegate = TouchDelegate(rect, this)
}
}
在Activity中使用:
private val tvExpandTouch: TextView by id(R.id.tv_view_delegate)
tvExpandTouch.run {
expandTouchView(50.dp2px()) //擴大點擊區(qū)域
setOnClickListener { showToast("通過TouchDelegate擴大點擊區(qū)域") }
}
上面就實現(xiàn)了View擴大點擊區(qū)域,繼續(xù)來看下TouchDelegate 的源碼:
public class TouchDelegate {
private View mDelegateView; //需要接收觸摸事件的 View,即代理 View。
private Rect mBounds;//本地坐標中的代理 View 的邊界,用于初始命中測試。
private Rect mSlopBounds;//增加一定范圍的 mBounds,用于追蹤觸摸事件是否應被視為在代理 View 內。
@UnsupportedAppUsage
private boolean mDelegateTargeted;
private TouchDelegateInfo mTouchDelegateInfo;
public TouchDelegate(Rect bounds, View delegateView) {
mBounds = bounds;
mSlop = ViewConfiguration.get(delegateView.getContext()).getScaledTouchSlop();
mSlopBounds = new Rect(bounds);
mSlopBounds.inset(-mSlop, -mSlop);
mDelegateView = delegateView;
}
//接收并處理觸摸事件。若事件在 mBounds 內,則會將其轉發(fā)到 mDelegateView。
public boolean onTouchEvent(@NonNull MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
boolean sendToDelegate = false;
boolean hit = true;
boolean handled = false;
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
mDelegateTargeted = mBounds.contains(x, y);
sendToDelegate = mDelegateTargeted;
break;
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_MOVE:
sendToDelegate = mDelegateTargeted;
if (sendToDelegate) {
Rect slopBounds = mSlopBounds;
if (!slopBounds.contains(x, y)) {
hit = false;
}
}
break;
case MotionEvent.ACTION_CANCEL:
sendToDelegate = mDelegateTargeted;
mDelegateTargeted = false;
break;
}
if (sendToDelegate) {
if (hit) {
// Offset event coordinates to be inside the target view
event.setLocation(mDelegateView.getWidth() / 2, mDelegateView.getHeight() / 2);
} else {
// Offset event coordinates to be outside the target view (in case it does
// something like tracking pressed state)
int slop = mSlop;
event.setLocation(-(slop * 2), -(slop * 2));
}
//NOTE:重點看這里,最終是調用的代理View去處理事件了。
handled = mDelegateView.dispatchTouchEvent(event);
}
return handled;
}
}
View.java 源碼中使用 TouchDelegate:
private TouchDelegate mTouchDelegate = null;
public void setTouchDelegate(TouchDelegate delegate) {
mTouchDelegate = delegate;
}
public TouchDelegate getTouchDelegate() {
return mTouchDelegate;
}
public boolean onTouchEvent(MotionEvent event) {
//......
if (mTouchDelegate != null) {
if (mTouchDelegate.onTouchEvent(event)) {
return true;
}
}
switch (action) {
case MotionEvent.ACTION_DOWN:
//...省略...
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
}
}
可以看到在onTouchEvent中,優(yōu)先去判斷是否有TouchDelegate,如果有的話會先去找對應的代理View去處理事件。使用TouchDelegate的注意事項:
- 目標View必須有父View;
- 給多個目標View擴大點擊區(qū)域時,不能是同一個父View,從View類的源碼中可以看到,設置setTouchDelegate時,會把之前的覆蓋掉。
方式三:RectF & getLocationOnScreen
RectF 是一個用于表示浮點坐標的矩形區(qū)域的類,而 getLocationOnScreen 則用于獲取視圖在整個屏幕中的絕對坐標。結合兩者,可以檢查觸摸事件是否在子視圖的“擴展區(qū)域”內,然后執(zhí)行相應的操作。代碼示例:
class ParentInnerTouchView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
private val tvChildView: TextView
init {
inflate(context, R.layout.expand_touch_view, this)
tvChildView = findViewById(R.id.tv_expand_view)
tvChildView.setOnClickListener { showToast("擴大了點擊事件") }
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent?): Boolean {
event?.let { ev ->
if (ev.action == MotionEvent.ACTION_DOWN) {
val isChildHit = isHitExpandChildView(tvChildView, Pair(ev.rawX, ev.rawY))
if (isChildHit) {
//將事件傳遞給子控件
tvChildView.performClick()
}
}
}
return super.onTouchEvent(event)
}
/**
* 判斷是否點擊到了子 View 的擴大區(qū)域
* @param childView 子 View
* @param touchPair 點擊的位置 (x, y)
* @param expandSize 擴大區(qū)域的大小
* @return 是否命中
*/
private fun isHitExpandChildView(
childView: View,
touchPair: Pair<Float, Float>,
expandSize: Int = 50.dp2px()
): Boolean {
// 獲取子 View 在屏幕上的位置
val location = IntArray(2)
childView.getLocationOnScreen(location)
val childX = location[0].toFloat()
val childY = location[1].toFloat()
val touchX = touchPair.first
val touchY = touchPair.second
// 擴大點擊區(qū)域
val rect = RectF()
rect.set(
childX - expandSize,
childY - expandSize,
childX + childView.width + expandSize,
childY + childView.height + expandSize
)
// 判斷點擊是否在擴大的子 View 區(qū)域內
return rect.contains(touchX, touchY)
}
}
- getLocationOnScreen: 用于獲取子視圖在屏幕上的絕對坐標,返回一個包含 x 和 y 坐標的數(shù)組。利用這些坐標計算出子視圖在屏幕上的位置。
- RectF: 創(chuàng)建一個矩形區(qū)域,通過調用 set 方法擴展矩形的上下左右邊界,從而擴大點擊區(qū)域。
- onTouchEvent: 監(jiān)聽觸摸事件,如果點擊位置在擴大的區(qū)域內,則調用 performClick 觸發(fā)子視圖的點擊事件。
以上就是Android實現(xiàn)擴大View點擊區(qū)域的三種方式的詳細內容,更多關于Android View點擊區(qū)域的資料請關注腳本之家其它相關文章!
相關文章
Android Studio 3.0中mipmap-anydpi-v26是什么東東
在Android Studio 3.0中一旦我們創(chuàng)建了一個項目,一個名為mipmap-anydpi-v26自動創(chuàng)建的文件夾在res文件夾下。它究竟能干什么?為什么我們需要這個?我們在開發(fā)時該如何利用它,下面通過本文給大家介紹下2017-12-12
Android中ImageView實現(xiàn)選擇本地圖片并顯示功能
本文主要介紹了android中ImageView實現(xiàn)選擇本地圖片并顯示功能的示例代碼。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04
Android中google Zxing實現(xiàn)二維碼與條形碼掃描
這篇文章主要介紹了Android中google Zxing實現(xiàn)二維碼與條形碼掃描的相關資料,需要的朋友可以參考下2017-05-05
Android開發(fā)中使用Intent打開第三方應用及驗證可用性的方法詳解
這篇文章主要介紹了Android開發(fā)中使用Intent打開第三方應用及驗證可用性的方法,結合實例形式分析了Android使用Intent打開第三方應用的三種常用方式及使用注意事項,需要的朋友可以參考下2017-11-11
Android GridView實現(xiàn)滾動到指定位置的方法
這篇文章主要介紹了Android GridView實現(xiàn)滾動到指定位置的方法,本文介紹了4個相關的方法,分別對它們做了講解,需要的朋友可以參考下2015-06-06
解決EditText編輯時hint 在6.0 手機上顯示不出來的問題
下面小編就為大家?guī)硪黄鉀QEditText編輯時hint 在6.0 手機上顯示不出來的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05

