Avoid layout change animations in custom view, tha

2019-03-31 05:38发布

问题:

I have a custom RelativeLayout and I even have set setLayoutTransition(null);. I add this custom view to the WindowManager with ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).updateViewLayout(this, layoutParams);

I change views in the custom view AND I change the LayoutParams for the WindowManager and afterwards call updateViewLayout...

I think, the chang of the LayoutParams for the WindowManager is animated, but I'm not sure...

How can I disable ALL animations?

回答1:

This one's a little annoying. Android animates all window changes, and they've made the flag to disable it private. You can disable window animations using reflection

    WindowManager.LayoutParams wp = new WindowManager.LayoutParams();
    String className = "android.view.WindowManager$LayoutParams";
    try {
        Class layoutParamsClass = Class.forName(className);

        Field privateFlags = layoutParamsClass.getField("privateFlags");
        Field noAnim = layoutParamsClass.getField("PRIVATE_FLAG_NO_MOVE_ANIMATION");

        int privateFlagsValue = privateFlags.getInt(wp);
        int noAnimFlag = noAnim.getInt(wp);
        privateFlagsValue |= noAnimFlag;

        privateFlags.setInt(wp, privateFlagsValue);

        // Dynamically do stuff with this class
        // List constructors, fields, methods, etc.

    } catch (ClassNotFoundException e) {
        Logger.l.e(e.toString());
        // Class not found!
    } catch (Exception e) {
        Logger.l.e(e.toString());
        // Unknown exception
    }

Now wp will not animate layout changes. Note you'll probably see flicker when you change the window size. I haven't found a way to work around that yet.