用 TouchDelegate 扩大子 View 的点击区域

原文出处:http://developer.android.com/training/gestures/viewgroup.html#delegate

点击区域的大小会影响到用户体验,除了扩大可点击 View 的 padding 之外,今天偶遇另一种方法,在父 View 级别增大子 View 的点击区域。

gist 被墙,csdn code 402,暂且当做博客记录,以备不时之需。

安卓提供了 TouchDelegate 类来让父 View 扩大子 View 的可点击区域。使用场景是,子 View 必须很小,但是又要有较大的点击区域。当然,如果你愿意,也可以用该类来缩小子 View 的点击区域。

下面的例子中,ImageButton 是“代理 View”(即,自己不争气、需要靠爹 View 来帮助扩大点击区域的子 View)。layout 如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/parent_layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity" >

     <ImageButton android:id="@+id/button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@null"
          android:src="@drawable/icon" />
</RelativeLayout>

下面的代码片段作用如下:

  • 获取父 View ,然后在 UI 线程 post 一个 Runnable,保证父 View 在摆放各个子 View 之前调用 getHitRect() 方法。该方法得到子 View 的点击区域;
  • 找到可点击的 ImageButton,调用 getHitRect() 方法获取其可点击区域的边界;
  • 扩大 ImageButton 的可点击区域;
  • 实例化 TouchDelegate,在其构造方法中传入扩大后的可点击区域和 ImageButton。在父 View 中设置 TouchDelegate,以便父 View 代理的区域内的点击事件能传给 ImageButton;

在被代理的区域内,父 View 会获取所有的 touch 事件,然后传递给子 View 处理。

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Get the parent view
        View parentView = findViewById(R.id.parent_layout);

        parentView.post(new Runnable() {
            // Post in the parent's message queue to make sure the parent
            // lays out its children before you call getHitRect()
            @Override
            public void run() {
                // The bounds for the delegate view (an ImageButton
                // in this example)
                Rect delegateArea = new Rect();
                ImageButton myButton = (ImageButton) findViewById(R.id.button);
                myButton.setEnabled(true);
                myButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, 
                                "Touch occurred within ImageButton touch region.", 
                                Toast.LENGTH_SHORT).show();
                    }
                });

                // The hit rectangle for the ImageButton
                myButton.getHitRect(delegateArea);

                // Extend the touch area of the ImageButton beyond its bounds
                // on the right and bottom.
                final int _15DP = ViewUtils.dp2px(15);
                delegateArea.left -= _15DP;
                delegateArea.top -= _15DP;
                delegateArea.right += _15DP;
                delegateArea.bottom += _15DP;

                // Instantiate a TouchDelegate.
                // "delegateArea" is the bounds in local coordinates of 
                // the containing view to be mapped to the delegate view.
                // "myButton" is the child view that should receive motion
                // events.
                TouchDelegate touchDelegate = new TouchDelegate(delegateArea, 
                        myButton);

                // Sets the TouchDelegate on the parent view, such that touches 
                // within the touch delegate bounds are routed to the child.
                if (View.class.isInstance(myButton.getParent())) {
                    ((View) myButton.getParent()).setTouchDelegate(touchDelegate);
                }
            }
        });
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QGraphicsView是Qt中用于显示和编辑图形项的控件。它提供了许多事件处理函数,包括鼠标点击事件。下面是使用QGraphicsView的鼠标点击事件的步骤: 1.创建一个QGraphicsView对象,并将其设置为主窗口的中心部件。 2.创建一个QGraphicsScene对象,并将其设置为QGraphicsView的场景。 3.在QGraphicsScene中添加图形项,例如QGraphicsRectItem或QGraphicsEllipseItem。 4.重写QGraphicsView的mousePressEvent函数,并在其中处理鼠标点击事件。例如,您可以使用itemAt函数获取鼠标点击位置下的图形项,并对其进行操作。 下面是一个简单的示例代码,演示如何在QGraphicsView中处理鼠标点击事件: ``` class MyView : public QGraphicsView { public: MyView(QWidget *parent = nullptr) : QGraphicsView(parent) { // 创建场景 QGraphicsScene *scene = new QGraphicsScene(this); setScene(scene); // 添加图形项 QGraphicsRectItem *rectItem = new QGraphicsRectItem(0,0, 100, 100); scene->addItem(rectItem); } protected: void mousePressEvent(QMouseEvent *event) override { // 处理鼠标点击事件 QPointF pos = mapToScene(event->pos()); QGraphicsItem *item = scene()->itemAt(pos, QTransform()); if (item) { qDebug() << "Clicked on item"; } else { qDebug() << "Clicked on background"; } } }; ``` 在这个示例中,我们创建了一个名为MyView的自定义QGraphicsView类,并在其中重写了mousePressEvent函数。在这个函数中,我们使用mapToScene函数将鼠标点击位置转换为场景坐标系中的位置,并使用itemAt函数获取该位置下的图形项。如果找到了图形项,则输出“Clicked on item”,否则输出“Clicked on background”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值