Avoid layout change animations in custom view, that's updated in the WindowManager

馋奶兔 提交于 2020-01-01 06:43:39

问题


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.



来源:https://stackoverflow.com/questions/31338359/avoid-layout-change-animations-in-custom-view-thats-updated-in-the-windowmanag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!