Android 从右到左查看动画

Android 从右到左查看动画,android,animation,layout-animation,Android,Animation,Layout Animation,我无法为膨胀的布局放置视图动画。 我使用了以下代码片段 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"> <tr

我无法为膨胀的布局放置视图动画。 我使用了以下代码片段

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>

我遗漏了什么吗


谢谢。

如果在首次创建视图时尝试设置视图动画,则需要设置
layoutaimation
XML属性或调用
setlayoutaimation()

如果只想让视图看起来像在移动,则需要一个
TranslateAnimation
;看看这个答案: 另外,如果要重复动画,请调用
setAnimationListener()
,然后在
onAnimationEnd()中重新启动动画


如果要永久移动视图,请参见:

以下是视图滑动动画的代码

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());

谢谢,

我知道你已经接受了答案。但我认为这个回答会对阅读本文的人有所帮助。您可以尝试从,
pageView.startAnimation(AnimationUtils.loadAnimation(this,R.anim.right_至_left_anim.xml))中删除.xml

尝试运行动画时会得到什么?在为一个视图设置动画时,它只是从左到右然后从右到左滑动。您想知道会发生什么吗?我只想要从右到左的动画。但是如何为视图启动动画。yourview.startAnimation(INFORMONRIGHTANIMATION());如果有帮助,为避免动画元素捕捉到其原始位置,请添加例如outtoLeft.setFillAfter(true);