
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use Fade In and Fade Out Animation in Java for Android
Fade in and fade out animation works based on alpha animation class. This example demonstrate about How to use Fade In and Fade Out Android Animation in Java.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/login.xml.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/parent" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> <Button android:id="@+id/fadeIn" android:text="Fade In" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/fadeOut" android:text="Fade Out" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
In the above code, we have taken imageview and two button. FadeIn button will provide fade enter animation to image view and FadeOut button provide fade exit animation to imageview.
Step 3 − Add the following code to src/MainActivity.java
package com.example.andy.myapplication; import android.animation.ObjectAnimator; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ImageView imageView = findViewById(R.id.imageView); Button fadeIn = findViewById(R.id.fadeIn); fadeIn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f); alphaAnimation.setDuration(1000); alphaAnimation.setRepeatCount(1); alphaAnimation.setRepeatMode(Animation.REVERSE); imageView.startAnimation(alphaAnimation); } }); Button fadeOut = findViewById(R.id.fadeOut); fadeOut.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ObjectAnimator fadeOut = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0); fadeOut.setDuration(2000); fadeOut.start(); } }); } }
To provide fade in animation to imageview, use the following code -
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f); alphaAnimation.setDuration(1000); alphaAnimation.setRepeatCount(1); alphaAnimation.setRepeatMode(Animation.REVERSE); imageView.startAnimation(alphaAnimation);
To provide fade out animation to imageview, use the following code -
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0); fadeOut.setDuration(2000); fadeOut.start();
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −
Now click on fade in button it will provide animation to imageview as shown below -
now click on fade out animation, it will provide fade out animation to imageview as shown below -
Click here to download the project code