Open In App

Explode Animation in Android

Last Updated : 21 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to show the android explode animation. This is something that is very interesting. You may have seen this feature in some game apps or even many money transfer apps. They show rewards like this. When you click on the reward it will explode and will show the reward that you have got. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Explode Animation in Android Sample GIF

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the build.gradle file

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.   

implementation 'tyrantgit:explosionfield:1.0.1'

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. We will create a simple ImageView which we will be exploding on click.

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">
  
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/explode"
        android:src="@drawable/bomb"/>
      
</LinearLayout>


Step 4: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
  
import androidx.appcompat.app.AppCompatActivity;
  
import tyrantgit.explosionfield.ExplosionField;
  
public class MainActivity extends AppCompatActivity {
      
    boolean explod = true;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        final ImageView explode = findViewById(R.id.explode);
        final ExplosionField explosionField = ExplosionField.attach2Window(this);
          
        explode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // as we click on the image it will explode
                if (explod) {
                    explosionField.explode(explode);
                    explod = false;
                }
            }
        });
    }
}


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads