Mackovich
Mackovich

Reputation: 3563

Android how to slide view from left to right

I have an ImageView I would like to make it slide with an animation from its original position to the new one.

Currently my view is on the left close to the screen border. I need to move it to the opposite side and have it stayed there permanently.

Is there a way to accomplish this ?

For now I've tried Transition Animation like this one :

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate android:fromXDelta="0%" android:toXDelta="500%"
        android:fromYDelta="0%" android:toYDelta="0%" android:fillAfter="true"/>
</set>

But the problem is that the androud:toXDelta value is arbitrary and I don't know how to calculate it. With the current value, the view seems to come from outside the screen and goes a little bit further than its original position but at the end of the animation immediately comes back to its original position. That is not what I am looking for.

Thanks for the help !

Upvotes: 0

Views: 2643

Answers (1)

NoChinDeluxe
NoChinDeluxe

Reputation: 3444

To get the ImageView to stay at its final location at the end of the animation, put the following attributes inside your <set> tag:

android:fillAfter="true"
android:fillEnabled="true"

You can also do this type of thing pretty easily programmatically using a TranslateAnimation. You would do something similar to this:

ImageView viewToSlide = (ImageView) findViewById(R.id.your_image_view);
TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,
                                                      Animation.RELATIVE_TO_PARENT, 0.80f,
                                                      Animation.RELATIVE_TO_PARENT, 0,
                                                      Animation.RELATIVE_TO_PARENT, 0);
animation.setDuration(5000);
animation.setFillEnabled(true);
animation.setFillAfter(true);
viewToSlide.startAnimation(animation);

EDIT IN RESPONSE TO COMMENTS

To answer your first question below:

So you had originally mentioned that xDelta was an "arbitrary" value that was difficult to calculate, and you are right! So the way we can position our view relative to the width of its parent is to use the Animation.RELATIVE_TO_PARENT identifier, which identifies our next dimension value as a value that should be multiplied by the dimension of the parent itself. So in the constructor of our TranslateAnimation, we have -- Animation.RELATIVE_TO_PARENT, 0.80f -- which just means "take the width of the parent, and multiply it by 0.80." That gives us 80% of the width of the parent. The other values with zeroes in them just give us values of zero, because it will be "the width/height of the parent multiplied by zero."

To answer your second question:

The reason the animation accelerates is because I believe the default interpolator (the animation modifier that defines the speeds at which the values of the animation play out) is one of the accelerator interpolators. You can change this using the setInterpolator() method, and pass in any of the values found here: http://developer.android.com/reference/android/R.interpolator.html

Upvotes: 2

Related Questions