Translate animation from bottom view to top view parent android năm 2024

Android animations allow you to change the object behavior and property at runtime. It adds visual cues that notify your users about what is going on in the app. They are especially useful when the UI changes state, such as when new content loads or new actions become available. Animations also add a polished look to your app, which gives it a higher quality look and feel.

When to use Animations in android app

To better understand when to use animations, let’s take a look at how to use animations. 1. Informative: Motion designs help users to understand the relationships between elements, action availability and action outcomes.

2. Focused: Motions focuses attention on what is important, without unnecessarily creating distractions.

3. Expressive: Motion celebrates moments in user journeys, adds character to common interactions, and can express a brand’s style.

Types of Animations in android

  1. Property Animation: This allows to animate almost anything. One can design an animation to change object’s property over a time, whether it is drawn to the screen or not. A property animation changes a property’s [a field in an object] value over a specified length of time. To animate something, you specify the object property that you want to animate, such as an object’s position on the screen, how long you want to animate it for, and what values you want to animate between.
  2. View Animations: This is used to perform simple animations like changing size, position, transparency and rotation. They are easy to be built and fast to run too. You don’t need a lot of code to write. If view animation accomplishes everything that you need to do, or if your existing code already works the way you want, there is no need to use the property animation system. It also might make sense to use both animation systems for different situations if the use case arises.
  3. Drawable Animations: This lets you load a series of Drawable resources one after another to create an animation. This is a traditional animation in the sense that it is created with a sequence of different images, played in order, like a roll of film.

Let’s go into each of the ways of creating animation one after the other for better understanding in their application.

Using Property Animation in android, we are going to create a sample project that uses property animation to create animation in android.

  1. Translation of View on Y axis

This is can be used in changing the TranslationY property of a view.

private void moveTextviewOnYaxis[]{

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview, "translationY", 0,600];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new AnticipateOvershootInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

2. Translation of View on X axis

private void moveTextviewOnXaxis[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview, "translationX", 0, 600];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new AnticipateOvershootInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

3. Fading of the View

private void fadeTextview[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,   
        "alpha", 0, 1];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

4. Scaling of the View on the Y axis

private void scaleTextview[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,  
        "scaleY", 1, 20];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

5. Scaling of the View on the Xaxis

private void scaleTextview[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,  
        "scaleX", 1, 20];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

6. Rotation of the View

private void rotateTextView[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,   
        "rotation", 0, 90];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

7. Rotation of the View on the Y axis

private void rotateTextView[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,   
        "rotationY", 0, 90];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

8. Rotation of the View on the X axis

private void rotateTextView[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,   
        "rotationX", 0, 90];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

9. Play Together two or more animations

private void combineAnimations[] {

isAnimationStarted = true;  
//Change the x pivot of a view ObjectAnimator animX = ObjectAnimator.ofFloat[firstTextview,
        "x",  50f];  
animX.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];**//Change the y pivot of a view**  
ObjectAnimator animY = ObjectAnimator._ofFloat_[firstTextview,  
        "y", 100f];  
animY.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];**//Change the scale of a view**  
ObjectAnimator scaleAnim= ObjectAnimator._ofFloat_[firstTextview,  
        "scaleY", 1, 20];  
scaleAnim.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];
//play the animations together
set = new AnimatorSet[];
//use playSequentially if you want the animation to happen one after the other set.playTogether[animX,animY,scaleAnim];
set.start[];  
}

10. Achieving Animation Combination with PropertyValueHolder

private void usingPropertyValueHolder[] {

isAnimationStarted = true;  
PropertyValuesHolder animX = PropertyValuesHolder._ofFloat_["x", 50f];  
PropertyValuesHolder animY = PropertyValuesHolder._ofFloat_["y", 100f];  
PropertyValuesHolder scaleY = PropertyValuesHolder._ofFloat_["scaleY", 20];
ObjectAnimator._ofPropertyValuesHolder_[firstTextview,animX, animY, scaleY].start[];
}

Using XML To Create Animation

As above, we created animation using java, now we could create one using xml.

The animator set is loaded from xml using AnimatorInflater class. After loading the animator set, you need to set the target to the animator set before starting.

private void moveTextviewOnXaxis[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview, "translationX", 0, 600];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new AnticipateOvershootInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

0

Animating Through State Changes

Using StateListAnimator, you can determine which animation to run when your view changes in state. For instance, when your view is focused or pressed or disabled, which animation you would like to run. Check below for example of this where we animate through when the state is pressed. The below xml name is scale_animate.xml

Setting the above state_animator xml to a view target by passing the scale_animate.xml to the android:stateListAnimator property.

Animate drawable graphics

One way to animate

private void fadeTextview[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,   
        "alpha", 0, 1];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

9 is to load a series of Drawable resources one after another to create an animation. This is a traditional animation in the sense that it is created with a sequence of different images, played in order, like a roll of film. The

private void scaleTextview[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,  
        "scaleY", 1, 20];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

0 class is the basis for Drawable animations. Let’s see how to achieve this with an xml.

NB: you cannot start the animation in onCreate[] of an activity because AnimationDrawable cannot be called in OnCreate[] so if you wish the animation to kick off immediately then start the animation in onStart[] since by then the views might be visible.

Use AnimatedVectorDrawable

Let’s create animated drawable using a vector drawables. First, you need to get vector drawable you want to animate. NB: Both paths must be compatible for morphing: they must have the same number of commands and the same number of parameters for each command. You can use this site to generate your animated vector drawable.

Using xml:

This is a bit complex, let’s take it one after the other for explanation. The root tag element should be and it requires drawable, which is the vector drawable.

private void moveTextviewOnXaxis[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview, "translationX", 0, 600];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new AnticipateOvershootInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

1

  1. The drawable points to the main vector drawable which is kept in the drawable resource folder. vector_drawable.xml

private void moveTextviewOnXaxis[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview, "translationX", 0, 600];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new AnticipateOvershootInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

2

  1. The animation points to the animation to be used for the path morphing. You can also create the objectAnimator animation file in the animator resource folder.

private void moveTextviewOnXaxis[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview, "translationX", 0, 600];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new AnticipateOvershootInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

3

  1. The target element in the animated-vector describes which animation run on a specified name.

After creating this animated vector drawable resource, you set it as a resource file to an imageview.

private void moveTextviewOnXaxis[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview, "translationX", 0, 600];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new AnticipateOvershootInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

4

You can now reference the imageView in the java class.

private void moveTextviewOnXaxis[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview, "translationX", 0, 600];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new AnticipateOvershootInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

5

Animate layout changes to ViewGroup objects

Animations allows you to animate all kind of motions in your UI. Let’s first of all look at one easy way to do this before going into details. There is an auto animation that can handle layout changes in your ViewGroup. All you have to do is to enable animateLayoutChanges in your ViewGroup.

NB: Use the animateLayoutChanges in your immediate ViewGroup you want to animate the children views.

  1. Auto animate Layout Updates.

This is using default layout transition so you can also use setLayoutTransition[] to create a transition and if you want xml too, you can enable animateLayoutChanges as below.

private void moveTextviewOnXaxis[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview, "translationX", 0, 600];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new AnticipateOvershootInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

6

  1. Using TransitionManager

When trying to alter the children view, for example, changing the position of the children views, changing the visibility of the children views and so on, you can easily call this beginDelayedTransition[] to create the transitions.

private void moveTextviewOnXaxis[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview, "translationX", 0, 600];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new AnticipateOvershootInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

7

  1. Using Transition to Change Layout in Same Screen.

We can use transition framework to change layouts through creating scene. Let’s create an example to get more understanding.

  1. Create main layout that holds the scenes we want to create.

Create the first layout to be used as a scene. Lets create first_scene.xml layout.

Create a second layout, this should be the layout that should be shown when doing the transition. The second layout should have same ids as the children views in the first layout for proper transition; if the ids become different from the first layout, the transition framework will use Fade transition to show the second scene. Create second layout, second_scene.xml.

2. Create starting scene from first layout and ending scene from second layout in java.

private void moveTextviewOnXaxis[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview, "translationX", 0, 600];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new AnticipateOvershootInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

8

Create the transition you prefer for the change.

private void moveTextviewOnXaxis[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview, "translationX", 0, 600];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new AnticipateOvershootInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

9

Create a click listener to change between the first and the second scene.

private void fadeTextview[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,   
        "alpha", 0, 1];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

0

Now let’s put the java codes together.

Transition in Activity

We can implement transition in activity to activity interactions. Let’s look at how transitions in activity can be implemented.

  1. Enabling the transition in the style.

private void fadeTextview[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,   
        "alpha", 0, 1];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

1

  1. Setting the Enter Transition: This is the transition that is used when the activity is entering into a scene and this can be done in the style resource too by

private void fadeTextview[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,   
        "alpha", 0, 1];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

2

  1. Setting the Exit Transition: This is used when the activity is leaving the current scene and it can also be set in the style resources by

private void fadeTextview[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,   
        "alpha", 0, 1];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

3

Shared Element Transition in Activity

This defines how views are shared between two activities. For example, when image exists in the two activities but with different position and size, we could use ChangeImageTransform transition to change the position and scale the image between the two activities.

  1. Setting Shared Element Enter Transition: This can be set in the style resource.

private void fadeTextview[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,   
        "alpha", 0, 1];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

4

  1. Setting Shared Element Exit Transition: This can be set in the style resource.

private void fadeTextview[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,   
        "alpha", 0, 1];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

5

Let us create an example showing how elements are shared in activity.

  1. We need a start scene, so we create it now and you can name it as first_layout.xml containing only imageview and textview.
  1. Reference the views in the java class. We link the second activity through a view click.
  1. Create the destination activity which you can call SecondActivity.java with the same items views as were in the first activity but this time, going to be different size and position in order to see the transition happening.

In an activity to activity interaction, we can create a shared Element transition by creating activityOption object with a pair of the view from the first activity and the transitionName of the corresponding view in the second activity.

private void fadeTextview[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,   
        "alpha", 0, 1];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

6

We can create Shared Element transition with fragments too. If you use normal FragmentManager to manage your fragment, then you can do below.

private void fadeTextview[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,   
        "alpha", 0, 1];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

7

If you are using navigation components library too, you can do this through the FragmentNavigator.Extras class as done below.

private void fadeTextview[] {

isAnimationStarted = true;  
objectAnimator = ObjectAnimator._ofFloat_[firstTextview,   
        "alpha", 0, 1];  
objectAnimator.setDuration[2000]  
        .setInterpolator[new LinearInterpolator[]];  
objectAnimator.setRepeatMode[ValueAnimator._REVERSE_];  
objectAnimator.setRepeatCount[1];  
objectAnimator.start[];
}

8

In conclusion, using animation and transition in android application enhances the look and feel of the application and I think, it should not be too much as too much of everything becomes bad. 😉😉 You can find these examples in my github repository. Thanks for your time.

How to permanently move view with animation effect in Android?

One way that Android lets you reposition your view objects on screen is by using ObjectAnimator . You provide the end position you want the object to settle in as well as the duration of the animation. You can also use time interpolators to control the acceleration or deceleration of the animation.

How do you animate visibility of view on Android?

Reveal or hide a view using animation.

Create a crossfade animation. Create the views. Set up the crossfade animation. Crossfade the views..

Create a card flip animation. Create the animator objects. Create the views. Create the fragments. Animate the card flip..

Create a circular reveal animation..

Additional resources..

What is translate animation in Android?

Translate Animation is Android is a used to apply the effect of Bounce, Move left, Move right, Move Top, Move Bottom and Left to Right effect with the help of this animation.

How to set animation to view in Android?

The animation XML file belongs in the res/anim/ directory of your Android project. The file must have a single root element: this will be either a single , , , , interpolator element, or element that holds groups of these elements [which may include another ].

Chủ Đề