Android视图动画从右到左

18

我无法在充气布局中放置视图动画。 我使用了以下代码片段

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>

我有什么遗漏的吗?

谢谢。


当您尝试运行动画时,会得到什么结果? - WarrenFaith
当对一个视图进行动画处理时,它只是从左向右滑动,然后从右向左滑动。 - codewithdk
你想要发生什么事情呢? - WarrenFaith
我只想要从右到左的动画。 - codewithdk
3个回答

81

这是视图滑动动画的代码。

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
但是如何为视图启动动画。 - Sazzad Hissain Khan
yourview.startAnimation(inFromRightAnimation()); - Bishoy Fakhry
如果有帮助的话,为了避免动画元素回到原始位置,可以添加例如outtoLeft.setFillAfter(true); - Cruclax

1

我知道你已经接受了答案。但我认为这个回复对于阅读此内容的某些人会有帮助。您可以尝试从pageView.startAnimation(AnimationUtils.loadAnimation(this,R.anim.right_to_left_anim.xml));删除 .xml


1

如果您想在视图创建时进行动画处理,那么您需要设置layoutAnimation XML属性或调用setLayoutAnimation()

如果您只是想让您的视图看起来像在移动,您需要使用TranslateAnimation;请参见此答案:https://dev59.com/RW855IYBdhLWcg3wq2TL#4214490 另外,如果您想重复播放动画,则调用setAnimationListener()并在onAnimationEnd()中再次启动动画。

如果您想永久移动视图,请参阅此内容:http://www.clingmarks.com/how-to-permanently-move-view-with-animation-effect-in-android/400


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接