Android setVisibility(View.GONE)無效的問題及原因分析

weixin_30788239發表於2020-04-05

出現這種情況很可能是因為設定了animation,並且呼叫了setFillAfter(true),這就會導致setVisibility無效,只需要呼叫一下clearAnimation()方法或者去掉setFillAfter(true)語句即可。

實驗證明只要在setVisibility之前設定View的mCurrentAnimation為null就可以,因此呼叫setAnimation(null)也是可以的。從原始碼來看setFillAfter為false時也會間接導致呼叫clearAnimation,而clearAnimation在這裡使setVisibility有效的原因也是置mCurrentAnimation為null了。

setFillAfter為false時間接導致clearAnimation:

 

而mCurrentAnimation使GONE無效的初步分析很可能是下面的程式碼造成:

在dispatchDraw子View的時候,儘管不為VISIBLE,由於是||的關係,也會導致drawChild被呼叫。而drawChild的程式碼如下:

  /**
     * Draw one child of this View Group. This method is responsible for getting
     * the canvas in the right state. This includes clipping, translating so
     * that the child's scrolled origin is at 0, 0, and applying any animation
     * transformations.
     *
     * @param canvas The canvas on which to draw the child
     * @param child Who to draw
     * @param drawingTime The time at which draw is occurring
     * @return True if an invalidate() was issued
     */
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        return child.draw(canvas, this, drawingTime);
    }

 

轉載於:https://www.cnblogs.com/zhujiabin/p/9013748.html

相關文章