View animation right to left android

2019-01-21 11:48发布

问题:

I am not able to put view animation for inflated layouts. I used the following code snippet

pageView.startAnimation(AnimationUtils.loadAnimation(this,R.anim.right_to_left_anim.xml));

and 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>

Is any thing i missing?

Thanks.

回答1:

Here is the code for the sliding animation for view.

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;
    }

and now start Animation on view

pageView.startAnimation(inFromRightAnimation());

Thanks,



回答2:

If you're trying to animate the view when it's first created, then you either need to set the layoutAnimation XML property or call setLayoutAnimation().

If you just want to make your view look like it's moving, you need a TranslateAnimation; see this answer: https://stackoverflow.com/a/4214490/832776 Also if you'd like to repeat the animation, then call setAnimationListener() and in onAnimationEnd() just start the animation again.

If you're trying to move the view permanently, see this: http://www.clingmarks.com/how-to-permanently-move-view-with-animation-effect-in-android/400



回答3:

I know that you have already accepted the answer. But I think this reply will be helpful for somebody reading this. You can try removing .xml from, pageView.startAnimation(AnimationUtils.loadAnimation(this,R.anim.right_to_left_anim.xml));