当我反向运行动画时,Android 会跳过 onDraw()

标签 android animation android-canvas objectanimator

我有一个 Sliding Fragments DevByte 的实现.除了将 fragment 滑入 View 之外,我还想在它遮挡的内容上画一个阴影。我已将视频中的 FractionalLinearLayout 修改为 measure 本身,使其位于屏幕宽度的两倍处,并在右半部分 layout 其子元素。在动画循环期间,我在左半部分绘制了一个增加 alpha 的黑色矩形,并将 X 坐标设置为负值以使右半部分可见。

我的问题是,当我将内容滑入 View 时,此方法工作正常,但当我将内容滑出 View 时,此方法失败。在进去的路上,我得到了我想要的行为:平移和变暗的阴影。在出路时,我只得到翻译,阴影在整个动画过程中一直保持它的第一个帧颜色。

我在日志记录中看到的是,在进入过程中,setPercentOnScreen()onDraw() 方法被交替调用,正如预期的那样。然而,在出去的路上,我接到了一个 setPercentageOnScreen() 调用,接着是一个 onDraw() 调用,然后只调用了 setPercentOnScreen()。 Android 正在优化绘图,但我不明白为什么。

更新:有趣的是,我只在运行 Android 4.4 的模拟器上看到这种行为。运行 4.0.3 和 4.3 的模拟器按预期在两个方向运行动画。旧的 Nexus 7 出现问题,不同的模拟器 4.4 没有。它在设备上似乎是一致的,但在设备之间有所不同。

再次更新:我提取了一个示例项目并将其放在 GitHub 上:barend/android-slidingfragment . GitHub 上的自述文件包含十几个设备的测试结果。对于模拟器,问题与“启用主机 GPU”功能相关,但仅在 Jelly Bean 和 KitKat 上;不在 ICS 上。

再次更新:进一步测试表明,问题出现在运行 Jelly Bean 及更高版本的物理设备上,以及运行 Jelly Bean 或更高版本且启用了“使用主机 GPU”的 ARM 模拟器上。无论 Android 版本如何,它都不会出现在没有“使用主机 GPU”的 x86 模拟器和 ARM 模拟器上。我的测试的确切表格可以在上面链接的 github 项目中找到。

// Imports left out
public class HorizontalSlidingLayout extends FrameLayout {
    /**
     * The fraction by which the content has slid into view. Legal range: from 0.0 (all content
     * off-screen) to 1.0 (all content visible).
     */
    private float percentOnScreen;
    private int screenWidth, screenHeight, shift;
    private Paint shadowPaint;

    // Constructors left out, all three call super, then init().

    private void init() {
        if (isInEditMode()) {
            // Ensure content is visible in edit mode.
            percentOnScreen = 1.0f;
        } else {
            setWillNotDraw(false);
            percentOnScreen = 0.0f;
            shadowPaint = new Paint();
            shadowPaint.setAlpha(0x00);
            shadowPaint.setColor(0x000000);
            shadowPaint.setStyle(Paint.Style.FILL);
        }
    }

    /** Reports our own size as (2w, h) and measures all children at (w, h). */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        screenWidth = MeasureSpec.getSize(widthMeasureSpec);
        screenHeight = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(2 * screenWidth, screenHeight);
        for (int i = 0, max = getChildCount(); i < max; i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    /** Lays out the children in the right half of the view. */
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        for (int i = 0, max = getChildCount(); i < max; i++) {
            View child = getChildAt(i);
            child.layout(screenWidth, top, right, bottom);
        }
    }

    /**
     * Draws a translucent shadow in the left half of the view, darkening by
     * {@code percentOnScreen}, then lets superclass draw children in the right half.
     */
    @Override
    protected void onDraw(Canvas canvas) {
        // Maintain 30% translucency
        if (percentOnScreen < 0.7f) {
            shadowPaint.setAlpha((int) (percentOnScreen * 0xFF));
        }
        android.util.Log.i("Slider", "onDraw(" + percentOnScreen + ") -> alpha(" + shadowPaint.getAlpha() + ')');
        canvas.drawRect(shift, 0, screenWidth, screenHeight, shadowPaint);
        super.onDraw(canvas);
    }

    @SuppressWarnings("unused")
    public float getPercentOnScreen() {
        return percentOnScreen;
    }

    /** Repeatedly invoked by an Animator. */
    @SuppressWarnings("unused")
    public void setPercentOnScreen(float fraction) {
        this.percentOnScreen = fraction;
        shift = (int)(fraction < 1.0 ? fraction * screenWidth : screenWidth);
        setX(-shift);
        android.util.Log.i("Slider", "setPOS(" + fraction + ") -> invalidate(" + shift + ',' + screenWidth + ')');
        invalidate(shift, 0, screenWidth, screenHeight);
        //invalidate() // Makes no difference
    }
}

奇怪的是这违反了对称性。我在滑出时和滑入时做的是完全相同的反向操作,但行为不同。我可能忽略了一些愚蠢的事情。有什么想法吗?

最佳答案

这是 Android 中硬件加速 View 的失效/重绘逻辑中的错误。默认情况下,我希望在 JB 中看到相同的错误,但只有在您选择硬件加速时才会在 ICS 上看到(从 JB 开始默认启用 hw accel)。

问题在于,当 View 从层次结构中移除然后动画消失时(例如在您的应用程序的 fragment 事务中发生的情况,或者在移除的 View 淡出时在 LayoutTransition 中发生的情况,或者在淡出 a 的 AlphaAnimation 中发生的情况removed view),它们不参与与普通/父级 subview 相同的失效/重绘逻辑。它们重新显示正确(因此我们看到 fragment 滑出),但它们没有重新绘制,因此如果它们的内容在此期间实际发生变化,它们将不会随这些变化一起重新显示。您的应用程序中错误的影响是 fragment 正确滑出,但未绘制阴影,因为这需要重新绘制 View 以获取这些更改。

您使 View 无效的方式是正确的,但错误意味着无效无效。

这个错误以前没有出现过,因为我相信,消失的 View 在动画消失时改变它们的外观并不常见(它们通常只是滑动或淡出,效果很好)。

该错误应该会在未来的版本中修复(我已经修复了它)。同时,针对您的特定情况的解决方法是也添加父容器的失效;这将强制重绘 View 并正确显示阴影:

if (getParent() instanceof ViewGroup) {
    ((ViewGroup) getParent()).invalidate();
}

关于当我反向运行动画时,Android 会跳过 onDraw(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19742350/

相关文章:

CSS变形词/文本

android - 创建多屏幕 Android 应用程序

java - 如何制作一个可以覆盖任何屏幕的按钮?

android - Glide 4.7.1 占位符未加载

android - 这段代码的结构是否适合它的用途?我如何在 Android 中平滑动画?

css - 动画多个 svg 路径

android - 使用 Android 的 RecyclerView 时在滑动行下添加带有文本/图标的彩色背景

android - 增加 clipPath 大小会导致滞后

Android - 计算弧角

android - 为什么添加 list CAPTURE_VIDEO_OUTPUT 或 CAPTURE_SECURE_VIDEO_OUTPUT 时会出现错误?