在ViewPager和setFillAfter使用动画 [英] Using animation on a ViewPager and setFillAfter
问题描述
我有一个ViewPager,我需要移动作为一个整体上的按钮preSS。我使用动画这一点。
I have a ViewPager which I need to move as a whole on button press. I use an animation for this.
当我preSS,我翻译的X它。我用setFillAfter(真),以确保新的位置。
但是,当我改变ViewPager的页面,它就跳回到原来的x位置!
When I press it, I translate the 'x' for it. I use setFillAfter(true) to keep the new position. But when I change the page of the ViewPager, it jumps back to the original x-position!
我只看到了这个问题在Android 4.1与Android 4.0是没有问题的!所以它看起来像Android中的某种回归。
I only saw this issue on Android 4.1, with Android 4.0 there is no problem! So it looks like some kind of regression in Android.
我连着一个testproject在那里我可以重现该问题的周围没有我的所有其他的东西。我认为这是最好的,如果你要帮我算出这个导入项目在Eclipse,看到它自己。
I attached a testproject where I could reproduce the issue without all my other stuff around it. I think it is best if you want to help me figure this out to import the project in your Eclipse and see it for yourself.
我也加入到视频的,一个是关于我的HTC One X的,我看到了问题,另一个在平板电脑与Android 4.0,这里的问题是不存在的。
I also added to video's, one on my HTC One X where I see the issue, and the other on a tablet with Android 4.0, where the issue is not there.
我一直在拼命地寻找解决这个丑陋的副作用,但没有运气至今...
I have been desperately looking to fix this ugly side effect, but no luck till now...
(对不起,我大电影文件...)
(Sorry for the big movie files...)
编辑:
我添加以下内容:
@Override
public void onAnimationEnd(Animation animation) {
RelativeLayout.LayoutParams lp = (android.widget.RelativeLayout.LayoutParams) myViewPager.getLayoutParams();
if (!i)
lp.setMargins(300,0,0,0);
else
lp.setMargins(0,0,0,0);
myViewPager.setLayoutParams(lp);
}
这之后,它停留在正确的位置,但它闪烁很快,像动画结尾仍然显示,当我更改页边距,它仍然显示了偏移动画之后了。然后跳转到正确的位置。
After that it stays at the correct position, but it 'flickers' quickly, like the animation is still showing at the end and when I change the margin, it still shows the offset it had after animation. Then it jumps to the correct position.
推荐答案
主要的问题似乎是动画类型不正确的选择。你看,观看动画作为一种工具的无意以像ViewPager复杂的交互对象使用。它仅提供图纸地方意见的低成本动画。动画ViewPager的响应于用户的操作的视觉behaivior是未定义和不应该依赖。
丑陋的笔触,当你替换为GOST与真正的对象是自然的。
The main problem seems to be incorrect choice of animation type. You see, View Animation as a tool is not intended to be used with complex interactive objects like ViewPager. It offers only low-cost animation of the drawing place of views. The visual behaivior of the animated ViewPager in response to user-actions is undefined and should not be relied on. Ugly flicks, when you substitute a "gost" with the real object are only natural.
的机制,即意在您的情况使用,因为API 11是一个建立在视图中为优化性能的专业物业动画师: ViewPropertyAnimator 或不专业,但更多才多艺ObjectAnimator和AnimatorSet。
The mechanism, that is intended to use in your case since API 11 is specialized property animator built in Views for optimized performance: ViewPropertyAnimator, or not specialized, but more versatile ObjectAnimator and AnimatorSet.
属性动画使得观真正改变它的位置和功能正常情况。
Property animation makes the View to really change its place and function normally there.
要做好工程,使用,比方说,ViewPropertyAnimator,改变你的监听器设置这样的:
To make project, to use, say, ViewPropertyAnimator, change your listener setting to this:
btn.setOnClickListener(new OnClickListener() {
boolean b = false;
@Override
public void onClick(View v) {
if(b) {
myViewPager.animate().translationX(0f).setDuration(700);
}
else {
myViewPager.animate().translationX(300f).setDuration(700);
}
b=!b;
}
});
如果您希望只使用xml配置,坚持| ObjectAnimator和AnimatorSet。通过阅读上面的链接了解更多信息。
If you want to use xml configuration only, stick to |ObjectAnimator and AnimatorSet. Read through the above link for further information.
在情况下,你都急于支持pre-蜂窝设备,您可以用杰克沃顿的 NineOldAndroids 项目。希望有所帮助。
In case, you are anxious to support pre-Honeycomb devices, you can use Jake Warton's NineOldAndroids project. Hope that helps.
这篇关于在ViewPager和setFillAfter使用动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!