Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为TouchDelegate放大矩形时的奇怪行为_Android - Fatal编程技术网

Android 为TouchDelegate放大矩形时的奇怪行为

Android 为TouchDelegate放大矩形时的奇怪行为,android,Android,我倾向于增加命中矩形的顶部区域 我引用了和中的代码 touch\u delegate\u linear\u布局是我的父视图 currency\u exchange\u linear\u布局是我的目标 下面是我的布局 上述布局的XML代码如下所示 <LinearLayout android:id="@+id/touch_delegate_linear_layout" ...> <LinearLayout android:id="@+id

我倾向于增加命中矩形的顶部区域

我引用了和中的代码

touch\u delegate\u linear\u布局是我的父视图

currency\u exchange\u linear\u布局
是我的目标

下面是我的布局

上述布局的XML代码如下所示

<LinearLayout
    android:id="@+id/touch_delegate_linear_layout"
    ...>

    <LinearLayout
        android:id="@+id/footer_linear_layout"
        ...
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/footer_label_text_view"
            ... />

        <View
            android:layout_width="1px"
            ... />

        <TextView
            android:id="@+id/footer_value_text_view"
            ... />
    </LinearLayout>

    <LinearLayout
        ...
        android:orientation="horizontal" >

        <TextSwitcher
            android:id="@+id/status_bar"
            ... />

        <LinearLayout
            android:id="@+id/currency_exchange_linear_layout"
            ...>

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

如果我在
currency\u exchange\u linear\u布局的区域内触摸,它工作得非常好

final View parentView = v.findViewById(R.id.touch_delegate_linear_layout);
final View directChildView = v.findViewById(R.id.footer_value_text_view);

ViewTreeObserver vto = parentView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    @Override
    public void onGlobalLayout() {
        final Rect r = new Rect();
        final Rect directChild = new Rect();

        currencyExchangeLinearLayout.getHitRect(r);
        directChildView.getHitRect(directChild);

        int bestHeight = Utils.dpToPixel(48);

        directChild.top = directChild.bottom - Math.max((bestHeight - r.height()), 0);

        parentView.setTouchDelegate(new TouchDelegate(directChild, currencyExchangeLinearLayout));

        ViewTreeObserver obs = parentView.getViewTreeObserver();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            obs.removeOnGlobalLayoutListener(this);
        } else {
            obs.removeGlobalOnLayoutListener(this);
        }
    }
});

我希望触摸也能工作,即使我触摸略高于<代码>货币兑换>线性布局
。这是因为我有code
r.top-=10
扩展我的触摸区域但是,它没有按预期工作。

奇怪的是,如果我在远离
货币交换线性布局的地方触摸,触摸事件将被错误触发。这不是我所期望的

你知道为什么会发生这种奇怪的行为吗?我的
TouchDelegate
代码有什么问题吗

演示代码
下面是示例代码,用于演示所提到的问题:

这种行为的原因是因为
货币兑换\u线性\u布局
不是
触摸\u代表\u线性布局的直接子对象

final View parentView = v.findViewById(R.id.touch_delegate_linear_layout);
final View directChildView = v.findViewById(R.id.footer_value_text_view);

ViewTreeObserver vto = parentView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    @Override
    public void onGlobalLayout() {
        final Rect r = new Rect();
        final Rect directChild = new Rect();

        currencyExchangeLinearLayout.getHitRect(r);
        directChildView.getHitRect(directChild);

        int bestHeight = Utils.dpToPixel(48);

        directChild.top = directChild.bottom - Math.max((bestHeight - r.height()), 0);

        parentView.setTouchDelegate(new TouchDelegate(directChild, currencyExchangeLinearLayout));

        ViewTreeObserver obs = parentView.getViewTreeObserver();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            obs.removeOnGlobalLayoutListener(this);
        } else {
            obs.removeGlobalOnLayoutListener(this);
        }
    }
});
因此,当我们表演的时候,

currencyExchangeLinearLayout.getHitRect(r);
获得的矩形坐标与
touch\u delegate\u linear\u布局无关

final View parentView = v.findViewById(R.id.touch_delegate_linear_layout);
final View directChildView = v.findViewById(R.id.footer_value_text_view);

ViewTreeObserver vto = parentView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    @Override
    public void onGlobalLayout() {
        final Rect r = new Rect();
        final Rect directChild = new Rect();

        currencyExchangeLinearLayout.getHitRect(r);
        directChildView.getHitRect(directChild);

        int bestHeight = Utils.dpToPixel(48);

        directChild.top = directChild.bottom - Math.max((bestHeight - r.height()), 0);

        parentView.setTouchDelegate(new TouchDelegate(directChild, currencyExchangeLinearLayout));

        ViewTreeObserver obs = parentView.getViewTreeObserver();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            obs.removeOnGlobalLayoutListener(this);
        } else {
            obs.removeGlobalOnLayoutListener(this);
        }
    }
});
在这种情况下,我们可以使用
footer\u value\u text\u view
的hit矩形,其矩形的Y坐标相对于
touch\u delegate\u linear\u布局

final View parentView = v.findViewById(R.id.touch_delegate_linear_layout);
final View directChildView = v.findViewById(R.id.footer_value_text_view);

ViewTreeObserver vto = parentView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    @Override
    public void onGlobalLayout() {
        final Rect r = new Rect();
        final Rect directChild = new Rect();

        currencyExchangeLinearLayout.getHitRect(r);
        directChildView.getHitRect(directChild);

        int bestHeight = Utils.dpToPixel(48);

        directChild.top = directChild.bottom - Math.max((bestHeight - r.height()), 0);

        parentView.setTouchDelegate(new TouchDelegate(directChild, currencyExchangeLinearLayout));

        ViewTreeObserver obs = parentView.getViewTreeObserver();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            obs.removeOnGlobalLayoutListener(this);
        } else {
            obs.removeGlobalOnLayoutListener(this);
        }
    }
});