Android ScaleAnimation doesn't scale clickable

2019-02-05 09:00发布

问题:

I have a layout structured as follows:

LinearLayout1
    LinearLayout2
        EditText

I am applying a ScaleAnimation to LinearLayout1 using a LayoutAnimationController so that all of the views in the hierarchy are scaled by the same amount.

After applying the ScaleAnimation, the views are all scaled correctly, but the EditText no longer responds to clicks that fall outside the space it originally occupied. In other words, it appears that the clickable area for the EditText does not scale along with the visual representation.

Is there a solution to this problem or I am missing before or after calling the ScaleAnimation?

回答1:

In case anyone else is looking for the answer to this problem, here is the solution I came up with. I capture click events in LinearLayout1 and do a manual recursive hit test down the view hierarchy using getHitRect and applying the scaling factor to the dimensions received.

Here is my implementation of the hit test for a view.

public boolean scaledHitTest(float zoomScale, int x, int y) {
        Rect rect = new Rect();
        getHitRect(rect);
        rect.top = (int) (rect.top * zoomScale);
        rect.bottom = (int) (rect.bottom * zoomScale);
        rect.left = (int) (rect.left * zoomScale);
        rect.right = (int) (rect.right * zoomScale);

        return rect.contains(x, y);
}