Android Left to Right Slide Animation

Android Left to Right slide animation

Use this xml in res/anim/

This is for left to right animation:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700"/>
</set>

This is for right to left animation:

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

In your coding use intent like for left to right:

this.overridePendingTransition(R.anim.animation_enter,
R.anim.animation_leave);

In your coding use intent like for right to left

this.overridePendingTransition(R.anim.animation_leave,
R.anim.animation_enter);

How to make a right to left animation in a layout

This code may help you.

Animation Left slide:

Create a file under res/anim/anim_left

    <translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="100%"
android:toXDelta="0%" >
</translate>

Animation Right slide:

Create a file under res/anim/anim_right

    <translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="-100%"
android:toXDelta="0%" >
</translate>

Use it in Activity as:

Animation rightSwipe = AnimationUtils.loadAnimation(this, R.anim.anim_right);
<view>.startAnimation(rightSwipe);

How to slide an ImageView from left to right smoothly in Android?

Creating this kind of tween animation is simple. Just follow the steps,

Step 1

Create a directory anim inside the res directory and put this as slide.xml

<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">

<translate
android:fromXDelta="0%p"
android:toXDelta="75%p"
android:duration="800" />
</set>

You can obviously customize the animation by changing the two attributes fromXDelta and toXDelta. The %p refers to with respect to the parent which simply means that it will move the image 75% with respect to the parent.

Step 2

// Refer the ImageView like this
imageView = (ImageView) findViewById(R.id.img);

// Load the animation like this
animSlide = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide);

// Start the animation like this
imageView.startAnimation(animSlide);

You can also setInterpolator() and setListeners() if you want to. I haven't shown them here to keep it simple. If you need, just let me know.

NOTE

As you have mentioned repeatedly, that you are experiencing a laggy animation. I have tested this animation on 3 real devices and 2 emulators and the animation was buttery smooth on all of them. Tested on low end devices like Moto E to high end devices like Nexus 5 and Galaxy S6.

If you still have a lag running this code, then the test device must be the reason. The code is perfect.

UPDATE

I just checked on my Moto G running on Lollipop too, the animation is running smoothly. This is a very small and light-weight animation, and should never be laggy. If you are still getting a lag, then it must be the device you are testing, or some other piece of code on that Activity making the UI slow or unresponsive.

Try to check which one is applicable to you,

  • I have tested on a total of 6 devices now with no lag at all. So, you can be rest assured that your production app will not have any lag, it can be your device which is slow
  • If you are doing some heavy operations like accessing the file system, database, or any other heavy operation, it must be slowing down the UI thread and you are loosing frames. Try to use AsyncTask for these heavy operations

How to slide a view from right to left?

Visibility changes should be animated via Transition API which is available in support (androix) package :

private void toggle() {
View imageView = findViewById(R.id.imageView);
ViewGroup parent = findViewById(R.id.parent);

Transition transition = new Slide(Gravity.LEFT);
transition.setDuration(600);
transition.addTarget(R.id.imageView);

TransitionManager.beginDelayedTransition(parent, transition);
imageView.setVisibility(show ? View.VISIBLE : View.GONE);
}

Here is result:

Sample Image

Here is my answer with more info.

android animation slide out left, reverse on back

Instead of

transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right, R.anim.slide_in_left, R.anim.slide_out_right);

use

transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right, R.anim.slide_in_right, R.anim.slide_out_left);

Where R.anim.slide_in_right

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-100%"
android:toXDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="400"/>
</set>

and R.anim.slide_out_left

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="100%"
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="300"/>
</set>

The arguments in this method are

setCustomAnimations(int enter, int exit, int popEnter, int popExit)

So basically

-slide new fragment in from right to left on entering

-slide out to the left when leaving current fragment

-slide old fragment from the left to the right when clicking back

-slide current fragment to the right when clicking back

As per method description

FragmentTransaction setCustomAnimations (int enter, 
int exit,
int popEnter,
int popExit)

Set specific animation resources to run for the fragments that are entering and exiting in this transaction. The popEnter and popExit animations will be played for enter/exit operations specifically when popping the back stack.

Android animation view right to left

This is a complete solution :

In jour xml file set two views at the same position and set the second view's visibility to invisible :

    <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/currentView"
android:text="currentView"/>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="invisible"
android:id="@+id/nextView"
android:text="nextView"/>

</RelativeLayout>

Then in your activity :

//First in you onCreate translate the second Layout to the right
comingView.setTranslationX(testbutton.getWidth());

// Here is the method that will do the job
// We slide both views (current one and waiting one) at the same time
// and in the same direction

private void slideExit(View currentView,final View comingView) {
currentView.animate().
translationXBy(-currentView.getWidth()).
setDuration(1000).
setInterpolator(new LinearInterpolator()).
setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
slideFromRightToLeft(comingView);
comingView.setVisibility(View.VISIBLE);
}
});
}

private void slideFromRightToLeft(View v) {
v.animate().
translationX(0).
setDuration(1000).
setInterpolator(new LinearInterpolator());
}

//call the method whenever you want
slideExit(currentView, comingView);

Android Left to Right Sliding animation across Activities

Resolved it in this fashion (which is a little different from how right-to-left was done)

anim3.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700"/>
</set>

anim4.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>

Code

overridePendingTransition(R.anim.anim3, R.anim.anim4);

How can i slide these views from right to left on button clicked?

I thought to use the navigation drawer layout but as of a little bit of performance enhancement, I was also thinking it will put some load if we looked at it from a device memory usage point of view.
I have achieved the same effect using a translationX property in XML and animate() function. A combination of these gave the same result which I was looking for.

In XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/_8sdp"
android:translationX="@dimen/_138sdp"
tools:translationX="0dp">

<ImageView
android:id="@+id/btn_action_menu"
android:layout_width="@dimen/_28sdp"
android:layout_height="@dimen/_28sdp"
android:layout_gravity="center_vertical"
android:background="@drawable/shape_action_menu_bg"
android:contentDescription="@null"
android:src="@drawable/arrow_left" />
........
</LinearLayout>

On Jave/Kotlin side

var rotation = 180f // For Arrow rotation effect from < to >
val translationX = binding.root.translationX

and for drawer-like effect, I've set click listener on the left arrow button and executed code like this.

binding.btnActionMenu.setOnClickListener {
// This animation is for rotating arrow from < to > upon clicked
it.animate().rotation(rotation).start()

// This will drag or you can say open a menu from right to left with
// 500ms delay effect.
binding.actionMenuLayout.animate().also {
.translationX(if(rotation != 0f) 0f else translationX)
.duration = 500L
}
.animate.start()

rotation = if(rotation == 0f) 180f else 0f
}

That's it.
Saves me from using the navigation drawer layout and a little bit of memory saving as well.

Menu Hides initially by setting translationX in xml:- https://i.stack.imgur.com/RbuMX.png

Menu Shown upon arrow click from dragging it right to left with 500ms delay:- https://i.stack.imgur.com/pBGoa.png



Related Topics



Leave a reply



Submit