XML 定义的动画中指定的持续时间真的以毫秒为单位吗?
我正在使用最新版本的支持库,以允许在运行 Android < 版本的设备上使用 Fragments 3.0。任何时候我想切换当前片段,我都会使用以下代码:
public void displayFragmentInMainView(Fragment f) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left,
android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.replace(R.id.fragment_holder, f);
transaction.commit();
}
这是定义过渡动画之一的 XML:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-50%p" android:toXDelta="0"
android:duration="500"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />
</set>
正如您在上面的 XML 中看到的,动画的持续时间设置为 500。开发人员文档中,此处指定的单位为毫秒(时间度量)。
我通过测试注意到动画持续时间在设备之间是不同的。如果我为持续时间指定 ms 值,为什么会出现这种情况?
以下是我测试过的设备以及结果(主观):
HTC Legend:(CPU 较慢的低端手机,Android 2.3.3)动画流畅且持续时间完全相同正如我所希望的那样。
Samsung Galaxy S:(中档 CPU、Android 2.1)动画有些不稳定,并且比 Legend 上的速度更快
模拟器:(在四核 iMac、Android 上运行4.0) 动画非常快,有时快到你甚至看不到它
基于此,似乎持续时间并不是真正以毫秒为单位测量的,而是取决于 CPU 速度。还有其他人注意到类似的结果吗?有没有办法进行设置,以便动画速度在阅读文档后能够在不同设备上保持一致?
I'm using the latest version of the support library to allow the use of Fragments on devices running versions of Android < 3.0. Any time I want to switch the current fragment I do so using this code:
public void displayFragmentInMainView(Fragment f) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left,
android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.replace(R.id.fragment_holder, f);
transaction.commit();
}
Here is the XML which defines one of the transition animations:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-50%p" android:toXDelta="0"
android:duration="500"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />
</set>
As you can see in the XML above, the duration for the animation is set to 500. According to the developer docs, the units specified here are in ms (a measure of time).
I've noticed through testing that the animation duration is different between devices. Why is this the case, if I'm specifying a ms value for the duration?
Here are the devices I've tested on, and the results (subjective):
HTC Legend: (low-end phone with a slow CPU, Android 2.3.3) animation is smooth and lasts exactly as long as I would like it to.
Samsung Galaxy S: (mid-range CPU, Android 2.1) animation is somewhat choppy and goes faster than on the Legend
Emulator: (running on a quad-core iMac, Android 4.0) animation is VERY fast, sometimes so fast that you don't even see it
Based on this it seems that the duration is not truly measured in terms of ms but is dependent on CPU speed. Has anyone else noticed similar results? Is there a way to set it up so that the animation speed will be uniform across devices as would be expected after reading the docs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Android 定义了 3 个整数资源
,您可以在 XML 或 Java 代码中使用它们。
Android defines 3 integer resources
that you can use in XML or Java code.