Android: child view outside parent does not respond to click events

In order to have an effect, I scale out the child view, which causes the child view to go beyond its parent view. I have a button in the child view, it works before scaling, but after scaling does not work. what is going wrong see image below:

the button outside does not work

to scale child I use this code:

            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); 

parent element 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"
        />
+4
source share
1 answer

This should do the trick:

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();

android:clipChildren="false" XML .

+1

Source: https://habr.com/ru/post/1651921/


All Articles