欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)擴大View點擊區(qū)域的三種方式

 更新時間:2024年08月15日 08:27:58   作者:_小馬快跑_  
在 Android 應用開發(fā)中,有時候需要擴大 View 的點擊區(qū)域以提高用戶交互的便利性,尤其是當視圖元素較小或用戶界面密集時,以下提供幾種擴大點擊區(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開發(fā)之圖片切割工具類定義與用法示例

    Android開發(fā)之圖片切割工具類定義與用法示例

    這篇文章主要介紹了Android開發(fā)之圖片切割工具類定義與用法,結合實例形式分析了Android圖片切割工具類的定義與簡單使用方法,需要的朋友可以參考下
    2017-11-11
  • Android Studio 3.0中mipmap-anydpi-v26是什么東東

    Android Studio 3.0中mipmap-anydpi-v26是什么東東

    在Android Studio 3.0中一旦我們創(chuàng)建了一個項目,一個名為mipmap-anydpi-v26自動創(chuàng)建的文件夾在res文件夾下。它究竟能干什么?為什么我們需要這個?我們在開發(fā)時該如何利用它,下面通過本文給大家介紹下
    2017-12-12
  • Android基礎之獲取LinearLayout的寬高

    Android基礎之獲取LinearLayout的寬高

    LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式排列,按照相對位置來排列所有的widgets或者其他的containers,超過邊界時,某些控件將缺失或消失。有的時候,我們需要想獲取LinearLayout寬高,下面通過這篇文章來跟著小編一起學習學習吧。
    2016-11-11
  • Android中ImageView實現(xiàn)選擇本地圖片并顯示功能

    Android中ImageView實現(xiàn)選擇本地圖片并顯示功能

    本文主要介紹了android中ImageView實現(xiàn)選擇本地圖片并顯示功能的示例代碼。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • Android中FileProvider的各種場景應用詳解

    Android中FileProvider的各種場景應用詳解

    這篇文章主要為大家介紹了Android中FileProvider的各種場景應用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Android 暫停和恢復Activity

    Android 暫停和恢復Activity

    在正常的應用程序使用,前臺activity有時會被其他可視化組件遮擋,從而 造成activity的暫停。例如,當一個半透明的activity打開時(如在一個風格對話框),以前的activity就暫停了。只要 activity仍然是部分可見,但目前沒有獲得焦點,它就依然處于暫停狀態(tài)
    2016-03-03
  • Android中google Zxing實現(xiàn)二維碼與條形碼掃描

    Android中google Zxing實現(xiàn)二維碼與條形碼掃描

    這篇文章主要介紹了Android中google Zxing實現(xiàn)二維碼與條形碼掃描的相關資料,需要的朋友可以參考下
    2017-05-05
  • Android開發(fā)中使用Intent打開第三方應用及驗證可用性的方法詳解

    Android開發(fā)中使用Intent打開第三方應用及驗證可用性的方法詳解

    這篇文章主要介紹了Android開發(fā)中使用Intent打開第三方應用及驗證可用性的方法,結合實例形式分析了Android使用Intent打開第三方應用的三種常用方式及使用注意事項,需要的朋友可以參考下
    2017-11-11
  • Android GridView實現(xiàn)滾動到指定位置的方法

    Android GridView實現(xiàn)滾動到指定位置的方法

    這篇文章主要介紹了Android GridView實現(xiàn)滾動到指定位置的方法,本文介紹了4個相關的方法,分別對它們做了講解,需要的朋友可以參考下
    2015-06-06
  • 解決EditText編輯時hint 在6.0 手機上顯示不出來的問題

    解決EditText編輯時hint 在6.0 手機上顯示不出來的問題

    下面小編就為大家?guī)硪黄鉀QEditText編輯時hint 在6.0 手機上顯示不出來的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05

最新評論