How to make animation in android studio on a ImageView using java with Source code.

 

How to make animation in android studio on an ImageView using java with Source code.

Creating animation in Android Studio is a great way to add some visual flair to your app. In this blog post, we will walk you through the process of creating a simple animation on an ImageView using Java in Android Studio.

First, open up your Android Studio project and navigate to the layout file where you want to add the animation. In the layout file, add an ImageView to your layout. This will be the view that we will be animating.

Next, navigate to the Java file associated with your layout file. In this file, we will create an AnimationDrawable object, which we will use to hold the animation frames.


AnimationDrawable animationDrawable;
ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageView = findViewById(R.id.imageView);
    animationDrawable = new AnimationDrawable();
}

Now that we have our AnimationDrawable object, we can add the frames for our animation. We can add frames by calling the addFrame() method and passing in a Drawable object for each frame. You can use your own drawable or use the drawable from the drawable folder of your project.

animationDrawable.addFrame(getResources().getDrawable(R.drawable.frame_1), 100);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.frame_2), 100);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.frame_3), 100);

Finally, we will set the ImageView’s background to the AnimationDrawable and call the start() method to start the animation.

imageView.setBackground(animationDrawable);
animationDrawable.start();

And that’s it! With just a few lines of code, you’ve created a simple animation in Android Studio. You can play around with the duration of each frame and the number of frames to create more complex animations.

Here is the complete source code:

package com.example.animation;

import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    AnimationDrawable animationDrawable;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);
        animationDrawable = new AnimationDrawable();

        animationDrawable.addFrame(getResources().getDrawable(R.drawable.frame_1), 100);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.frame_2), 100);
        animationDrawable.addFrame(getResources().getDrawable(R.drawable.frame_3), 100);

        imageView.setBackground(animationDrawable);
        animationDrawable.start();
    }
}


I hope you understand. Thanks for Reading my blog.

Comments