Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View animation right to left android

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.

like image 217
sd33din90 Avatar asked Jan 03 '13 14:01

sd33din90


People also ask

How do I animate a view in Android?

You can use the view animation system to perform tweened animation on Views. Tween animation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation.


3 Answers

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,

like image 182
sd33din90 Avatar answered Oct 19 '22 08:10

sd33din90


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

like image 29
Oleg Vaskevich Avatar answered Oct 19 '22 10:10

Oleg Vaskevich


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

like image 39
Mr Z Avatar answered Oct 19 '22 08:10

Mr Z