我无法为膨胀的布局添加视图动画。 我使用了以下代码片段
pageView.startAnimation(AnimationUtils.loadAnimation(this,R.anim.right_to_left_anim.xml));
和xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="0%" android:toXDelta="100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700"/>
</set>
我遗失了什么?
感谢。
答案 0 :(得分:69)
以下是视图滑动动画的代码。
1)inFromRightAnimation
private Animation inFromRightAnimation() {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(500);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
2)outToLeftAnimation
private Animation outToLeftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(500);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
3)inFromLeftAnimation
private Animation inFromLeftAnimation() {
Animation inFromLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromLeft.setDuration(500);
inFromLeft.setInterpolator(new AccelerateInterpolator());
return inFromLeft;
}
4)outToRightAnimation
private Animation outToRightAnimation() {
Animation outtoRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoRight.setDuration(500);
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
}
现在在视图上启动动画
pageView.startAnimation(inFromRightAnimation());
谢谢,
答案 1 :(得分:1)
如果您在第一次创建视图时尝试为视图设置动画,则需要设置layoutAnimation
XML属性或调用setLayoutAnimation()
。
如果你只想让你的视图看起来像是在移动,你需要一个TranslateAnimation
;看到这个答案:https://stackoverflow.com/a/4214490/832776
此外,如果您想重复动画,请拨打setAnimationListener()
并在onAnimationEnd()
中再次启动动画。
如果您尝试永久移动视图,请参阅:http://www.clingmarks.com/how-to-permanently-move-view-with-animation-effect-in-android/400
答案 2 :(得分:1)
我知道你已经接受了答案。但我认为这个回复对于阅读此内容的人会有所帮助。您可以尝试从<{1}}
中删除.xml