gpt4 book ai didi

android - 动画 Android 窗口和 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:43:14 25 4
gpt4 key购买 nike

我正在做一个自定义的 android 构建,我的服务在每个应用程序的顶部添加了一个 View 。使用以下代码:

WindowManager mWM = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
mParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
mParams.height = 117;
mParams.width = 366;
View myView = inflater.inflate(R.layout.myView,null);
mWM.addView(myView, mParams);

我能够成功添加 View 。我正在使用

为 View 设置动画
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", 300);
ObjectAnimator.ofPropertyValuesHolder(myView, pvhX).start();

我看到动画 View ,但不是窗口。 View 曾经所在的透明窗口没有动画。这与 Android Property Animation 中描述的行为相同

Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.

如何使窗口与 View 一起动画化?

谢谢

最佳答案

您有两种选择来实现这一点(使用户的触摸反馈与 View 的位置保持一致):

  1. 创建一个足够大的透明容器(如 FrameLayout)以包含您的 View 及其动画偏移量(在您的情况下高度为 366+300 像素)。通过这种方式,您会将容器添加到 Window,而 View 保留在容器内并且可以进行动画处理/翻译。

  2. 移动 View 在屏幕上的绝对位置,创建一种自定义动画;你只需要使用 ValueAnimator 动态设置 mParams.x 值,或者,如果你想要更多的弹性,通过 Rebound 使用 Spring 效果图书馆。我建议你最后一个,真的很花哨!

这是一个使用 Rebound 的(未经测试的)示例:

SpringSystem springSystem = SpringSystem.create();
Spring spring = springSystem.createSpring();
final int originalPos = mParams.x;
final int offset = 300;
spring.addListener(new SimpleSpringListener() {

@Override
public void onSpringUpdate(Spring spring) {
float value = (float) spring.getCurrentValue();
mParams.x = originalPos + ((int) value*offset)
mWM.updateViewLayout(myView, mParams);
}
});
spring.setEndValue(1);

请记住,您需要先在依赖项中添加 Rebound 的编译语句:

// Gradle dependency on Rebound
dependencies {
compile 'com.facebook.rebound:rebound:0.3.8'
}

关于android - 动画 Android 窗口和 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33618126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com