gpt4 book ai didi

Android:其父 View 之外的 subview 不响应点击事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:59:40 25 4
gpt4 key购买 nike

为了产生效果,我放大了 subview ,这导致 subview 超出了其父 View 的一侧。我在 subview 中有一个按钮,它在缩放之前有效,但在缩放之后不起作用。出了什么问题?见下图:

button outside doesn't work

对于缩放 child 我使用这个代码:

            childView.bringToFront();
Animation a = new Animation() {
@Override
protected void applyTransformation(float t, Transformation trans) {
float scale = 1f * ( 1 - t ) + SCALE_UP_FACTOR * t;
childView.setScaleX(scale);
childView.setScaleY(scale);
}

@Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration(ANIM_DURATION);
a.setInterpolator(new Interpolator() {
@Override
public float getInterpolation(float t) {
t -= 1f;
return (t * t * t * t * t) + 1f; // (t-1)^5 + 1
}
});
childView.startAnimation(a);

父级是一个ViewPager:

     <ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/invoice_list_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f5f5f5"
android:layout_gravity="center"
android:clipChildren="false"
android:clipToPadding="false"
/>

最佳答案

这应该可以解决问题:

final View grandParent = (View) childView.getParent().getParent();
grandParent.post(new Runnable() {
public void run() {
Rect offsetViewBounds = new Rect();
childView.getHitRect(offsetViewBounds);

// After scaling you probably want to append your view to the new size.
// in your particular case it probably could be only offsetViewBounds.right:
// (animDistance - int value, which you could calculate from your scale logic)
offsetViewBounds.right = offsetViewBounds.right + animDistance;

// calculates the relative coordinates to the parent
((ViewGroup)parent).offsetDescendantRectToMyCoords(childView, offsetViewBounds);
grandParent.setTouchDelegate(new TouchDelegate(offsetViewBounds, childView));
}
});

虽然我不确定它是否适用于Animation,但是对于缩放你可以使用类似的东西:

float scale = ...; // your scale logic

ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(childView,
PropertyValuesHolder.ofFloat("scaleX", scale),
PropertyValuesHolder.ofFloat("scaleY", scale));
animator.setDuration(ANIM_DURATION);
animator.start();

注意 XML 文件中父 View 的 android:clipChildren="false" 行。

关于Android:其父 View 之外的 subview 不响应点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39061433/

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