Saturday, October 5, 2019

Android Slide Up / Down Animations with Examples

In android, Slide Up and Slide Down animations are used to change the appearance and behaviour of the objects over a particular interval of time. The Slide Up and Slide Down animations will provide a better look and feel for our applications.

Generally, the animations are useful when we want to notify users about the change’s happening in our app, such as new content loaded or new actions available, etc.

To create an animation effect to the objects in our android application, we need to follow below steps.

Create XML File to Define Animation

We need to create an xml file that defines the type of animation to perform in a new folder anim under res directory (res à anim à slide_up.xml) with required properties. In case, anim folder not exists in res directory, create a new one.

To use Slide Up or Slide Down animations in our android applications, we need to define a new xml file with <scale> tag like as shown below.

For Slide Up animation, we need to set android:fromYScale="1.0" and android:toYScale="0.0" like as shown below.

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
    <
scale
        
android:duration="500"
        
android:fromXScale="1.0"
        
android:fromYScale="1.0"
        
android:toXScale="1.0"
        
android:toYScale="0.0" />
</
set>
For Slide Down animation, we need to set android:fromYScale="0.0" and android:toYScale="1.0" like as shown below.

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
    <
scale
        
android:duration="500"
        
android:fromXScale="1.0"
        
android:fromYScale="0.0"
        
android:toXScale="1.0"
        
android:toYScale="1.0" />
</
set>
Once we are done with creation of required animation XML files, we need to load those animation files using different properties.

Android Load and Start the Animation

In android, we can perform animations by using AnimationUtils component methods such as loadAnimation(). Following is the code snippet of loading and starting an animation using loadAnimation() and startAnimation() methods.

ImageView img = (ImageView)findViewById(R.id.imgvw);
Animation aniSlide = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_up);img.startAnimation(aniSlide);
If you observe above code snippet, we are adding an animation to the image using loadAnimation() method. The second parameter in loadAnimation() method is the name of our animation xml file.

Here we used another method startAnimation() to apply the defined animation to imageview object.

Now we will see how to implement slide up and slide down animations for imageview on button click in android applications with examples.

Android Slide Up & Slide Down Animations Example

Following is the example of implementing a slide up and slide down animations to slide an image up or down on button click in android applications.

Create a new android application using android studio and give names as SlideUpDownExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.

Once we create an application, open activity_main.xml file from \res\layout folder path and write the code like as shown below.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
android:layout_width="match_parent"
    
android:layout_height="match_parent"
    
android:paddingLeft="10dp"
    
android:paddingRight="10dp">
    <
ImageView android:id="@+id/imgvw"
        
android:layout_width="wrap_content"
        
android:layout_height="250dp"
        
android:src="@drawable/bangkok"/>
    <
Button
        
android:id="@+id/btnSlideDown"
        
android:layout_below="@+id/imgvw"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:text="Slide Down" android:layout_marginLeft="100dp" />
    <
Button
        
android:id="@+id/btnSlideUp"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:layout_alignBottom="@+id/btnSlideDown"
        
android:layout_toRightOf="@+id/btnSlideDown"
        
android:text="Slide Up" />
</
RelativeLayout>
As discussed, we need to create an xml files to define slide up and slide down animations in new folder anim under res directory (res à anim à slide_up.xmlslide_down.xml) with required properties. In case anim folder not exists in res directory, create a new one.

Following is the example of creating an XML files (slide_up.xmlslide_down.xml) under anim folder to define slide up / down animation properties.

Android Slide Up / Down Create Anim Folder in Project

Now open slide_up.xml file and write the code to set slide up animation properties like as shown below.

slide_up.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
    <
scale
        
android:duration="500"
        
android:fromXScale="1.0"
        
android:fromYScale="1.0"
        
android:toXScale="1.0"
        
android:toYScale="0.0" />
</
set>
 Now open slide_down.xml file and write the code to set slide down animation properties like as shown below.

slide_down.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
    <
scale
        
android:duration="500"
        
android:fromXScale="1.0"
        
android:fromYScale="0.0"
        
android:toXScale="1.0"
        
android:toYScale="1.0" />
</
set>
Now open your main activity file MainActivity.java from \java\com.tutlane.slideupdownexample path and write the code like as shown below.

MainActivity.java

package com.tutlane.slideupdownexample;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
    
private Button btnSDown;
    
private Button btnSUp;
    
private ImageView img;
    
@Override
    
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        
btnSDown = (Button)findViewById(R.id.btnSlideDown);
        
btnSUp = (Button)findViewById(R.id.btnSlideUp);
        
img = (ImageView)findViewById(R.id.imgvw);
        
btnSDown.setOnClickListener(new View.OnClickListener() {
            
@Override
            
public void onClick(View v) {
                Animation animSlideDown = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.
slide_down);
                
img.startAnimation(animSlideDown);
            }
        });
        
btnSUp.setOnClickListener(new View.OnClickListener() {
            
@Override
            
public void onClick(View v) {
                Animation animSlideUp = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.
slide_up);
                
img.startAnimation(animSlideUp);
            }
        });
    }
}
If you observe above code, we are adding an animation to the image using loadAnimation() method used startAnimation() method to apply the defined animation to imageview object.

Output of Android Slide Up / Down Animation Example

When we run above program in android studio we will get the result like as shown below.

Android Slide Up / Down Animations Example Result

If you observe above result, whenever we are clicking on Slide Up or Slide Down buttons, the image size varies based on our functionality.

This is how we can implement slide up and slide down animations for imageview in android applications based on our requirements.

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...