【发布时间】:2014-09-07 18:01:54
【问题描述】:
我正在使用Facebook's Rebound library 来复制在他们的聊天头实现中看到的有弹性的动画。问题是,大多数时候动画都会卡顿。几张图片可以更好地解释这一点。这是流畅的聊天头动画:
这是我的尝试(注意白色 View 的动画是如何跳过几乎所有帧的):
偶尔会顺利运行:
下面是我目前正在使用的代码(如果你想快速设置,整个项目是up on Github)。我猜这与我的View 上没有正确启用硬件加速有关。我的SpringSystem 中有2 个Springs,一个用于“气泡”(Android 图标),另一个用于内容(点击气泡时显示的白色View)。任何有关如何解决此问题的帮助将不胜感激。谢谢。
AndroidManifest.xml:
<application android:hardwareAccelerated="true" ...>
...
</application>
AppService.java:
// the following code is in AppService#onCreate()
// AppService extends android.app.Service
// full code at https://github.com/vickychijwani/BubbleNote
mContent.setLayerType(View.LAYER_TYPE_HARDWARE, null);
final Spring bubbleSpring = system.createSpring();
bubbleSpring.setCurrentValue(1.0);
bubbleSpring.addListener(new SpringListener() {
@Override
public void onSpringUpdate(Spring spring) {
float value = (float) spring.getCurrentValue();
params.x = (int) (mPos[0] * value);
params.y = (int) (mPos[1] * value);
mWindowManager.updateViewLayout(mBubble, params);
// fire the second animation when this one is about to end
if (spring.isOvershooting() && contentSpring.isAtRest()) {
contentSpring.setEndValue(1.0);
}
}
// ...
});
final Spring contentSpring = system.createSpring();
contentSpring.setCurrentValue(0.0);
contentSpring.addListener(new SpringListener() {
@Override
public void onSpringUpdate(Spring spring) {
// always prints false?!
Log.d(TAG, "hardware acc = " + mContent.isHardwareAccelerated());
float value = (float) spring.getCurrentValue();
// clamping is required to prevent flicker
float clampedValue = Math.min(Math.max(value, 0.0f), 1.0f);
mContent.setScaleX(value);
mContent.setScaleY(value);
mContent.setAlpha(clampedValue);
}
// ...
});
【问题讨论】:
-
首先从 onSpringUpdate 中删除 Log.d,因为它会减慢动画速度
-
@pskink 我知道这一点。我只是在之后登录该日志以检查是否正确启用了 View 的硬件加速。
-
您可以添加 Log.d 以查看第二个动画是否真的触发了一次
-
@pskink 是的,动画每次都正确触发。我用日志检查了它和调试器。
标签: android animation android-animation hardware-acceleration facebook-messenger