gpt4 book ai didi

android - myView.getHitRect() 返回相对于父对象的坐标。如何让他们与祖 parent 联系起来?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:49:51 34 4
gpt4 key购买 nike

我正在使用以下代码创建 View 的触摸委托(delegate)。问题是我想增加可点击区域的 View 位于非常狭窄的 LinearLayout 内部。所以下面的代码可以增加我的 View 的可点击区域,但仅限于我狭窄的 LinearLayout 范围内。我想传递给这个函数的不是父类而是 myView 的祖父类 (myView.getParent().getParent())。它是一个 RelativeLayout,它有更大的可点击区域空间。但随后 touchRect 将指向错误的位置,并且我的 TouchDelegate 的位置将不正确..

因为:delegate.getHitRect(touchRect);返回相对于 parent 的位置,而不是祖 parent (或 parent 的 parent )。

public static Runnable getTouchDelegateAction(final View delegate, final View parent, final int topPadding, final int bottomPadding, final int leftPadding,
final int rightPadding) {
return new Runnable() {
@Override
public void run() {

// Construct a new Rectangle and let the Delegate set its values
Rect touchRect = new Rect();
delegate.getHitRect(touchRect);

// Modify the dimensions of the Rectangle
// Padding values below zero are replaced by zeros
touchRect.top -= Math.max(0, topPadding);
touchRect.bottom += Math.max(0, bottomPadding);
touchRect.left -= Math.max(0, leftPadding);
touchRect.right += Math.max(0, rightPadding);


// Now we are going to construct the TouchDelegate
TouchDelegate touchDelegate = new TouchDelegate(touchRect, delegate);

// And set it on the parent
parent.setTouchDelegate(touchDelegate);
}
};
}

有什么建议吗?

最佳答案

如果有人有类似的问题,那么这是我找到的解决方案。不同之处在于可以设置标志 bindToGrandparent,以便将委托(delegate)委托(delegate)给可点击 View 的祖 parent 。

public static Runnable getTouchDelegateAction(final View delegate, final boolean bindToGrandparent, final int topPadding,
final int bottomPadding, final int leftPadding, final int rightPadding) {
return new Runnable() {
@Override
public void run() {
View parent = (View) delegate.getParent();
View grandparent = (View) parent.getParent();

// Construct a new Rectangle and let the Delegate set its values
Rect touchRect = new Rect();
delegate.getHitRect(touchRect);

if (bindToGrandparent) {
touchRect.top += parent.getTop();
touchRect.left += parent.getLeft();
touchRect.bottom += parent.getTop();
touchRect.right += parent.getLeft();
}

// Modify the dimensions of the Rectangle
// Padding values below zero are replaced by zeros
touchRect.top -= Math.max(0, topPadding);
touchRect.bottom += Math.max(0, bottomPadding);
touchRect.left -= Math.max(0, leftPadding);
touchRect.right += Math.max(0, rightPadding);

// Now we are going to construct the TouchDelegate
TouchDelegate touchDelegate = new TouchDelegate(touchRect, delegate);

// And set it on the parent
if (bindToGrandparent) {
grandparent.setTouchDelegate(touchDelegate);
} else {
parent.setTouchDelegate(touchDelegate);
}
}
};
}

应按如下方式使用:

grandparentView.post(FuncHelper.getTouchDelegateAction(ivFavorite, true, 30, 30, 30, 30));

将过程发布到 View 的祖 parent 是很重要的。

或:

parentView.post(FuncHelper.getTouchDelegateAction(ivFavorite, false, 30, 30, 30, 30));

关于android - myView.getHitRect() 返回相对于父对象的坐标。如何让他们与祖 parent 联系起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18101850/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com