如何通过windowManager.UpdateViewLayout移动视图而不进行任何动画?

时间:2016-09-24 00:35:59

标签: android android-animation android-windowmanager

当我这样做时:

    LayoutParams lp = getLayoutParams(view);
    lp.x = absoluteX;
    lp.y = absoluteY;
    this.mWindowManager.updateViewLayout(view, lp);

然后我有一个线性动画,从视图所在的位置到新位置absoluteX / absoluteY。如何在没有任何动画的情况下移动视图?

我尝试设置 lp.windowAnimations = 0 ,但它什么都没改变:( 任何想法我怎么做?

如果无法避免动画,是否可以通过任何方式了解视图的实际位置?也许我可以让它看不见仍然真正的位置不是绝对的X /绝对值

注意:

查看android的源代码,我在文件WindowManager.java中看到了这个条目:

     /**
     * Never animate position changes of the window.
     *
     * {@hide} */
    public static final int PRIVATE_FLAG_NO_MOVE_ANIMATION = 0x00000040;

    /**
     * Control flags that are private to the platform.
     * @hide
     */
    public int privateFlags;

它看起来像我需要的,但我不知道如何访问和设置这样的标志:(任何想法?

2 个答案:

答案 0 :(得分:0)

尝试禁用布局动画:

android:animateLayoutChanges="false"

答案 1 :(得分:0)

val lp = WindowManager.LayoutParams(
    width, 
    height, 
    this.windowManagerType(),
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
    or WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
    or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
    or WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
    or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
    PixelFormat.TRANSPARENT
)

val className = "android.view.WindowManager\$LayoutParams"
try {
    val layoutParamsClass = Class.forName(className)
    val privateFlags = layoutParamsClass.getField("privateFlags")
    val noAnim = layoutParamsClass.getField("PRIVATE_FLAG_NO_MOVE_ANIMATION")

    var privateFlagsValue = privateFlags.getInt(lp)
    val noAnimFlag = noAnim.getInt(lp)
    privateFlagsValue = privateFlagsValue or noAnimFlag
    privateFlags.setInt(lp, privateFlagsValue)
} catch (e: Exception) {
    Log.e("EXCEPT", "EXCEPTION: ${e.localizedMessage}")
}