Friday 30 March 2018

How to create an animated GIF from multiple JPEGs(images) in Android (development) code

This tutorial show how to create an animated GIF from multiple JPEGs(images) 
in Android Studio code


Their are mainly 3 files use in this project
1.src/MainActivity.java:-Code of (Combining the multiple image and create into single animation) in  MainActivity.java screen.
2.res/layout/activity_main.xml:-activity_main.xml is the layout file of MainActivity.java 
3.res/drawable:-images store here.

Code starting here->
STEP 1. Create a new Project  ImageAnimation  in Android Studio.
STEP 2. Paste this .png images in res/drawable folder or you can download your image from internet.
You can download images from this link below :
STEP3. Create and Write code in java file  MainActivity.java  and  xml file  activity_main.xml  under -
1. res/layout/activity_main.xml:-activity_main.xml is the layout file of MainActivity.java

                                  activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.amitrawat.imageanimation.MainActivity">

    <ImageView
        android:id="@+id/imageanim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/speaker1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

2. src/MainActivity.java :- Code of (Combining the multiple image and create into single animation) in MainActivity.java screen.

MAINACTIVITY.JAVA
package com.example.amitrawat.imageanimation;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    ImageView imageanim;
    AnimationDrawable animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageanim = findViewById(R.id.imageanim);
        animation new AnimationDrawable();
        animation.addFrame(getResources().getDrawable(R.drawable.speaker1), 500);
        animation.addFrame(getResources().getDrawable(R.drawable.speaker2), 500);
        animation.addFrame(getResources().getDrawable(R.drawable.speaker3), 500);
        animation.addFrame(getResources().getDrawable(R.drawable.speaker4), 500);
        animation.addFrame(getResources().getDrawable(R.drawable.speaker1), 500);
        animation.setOneShot(true);//If true, the animation will only run a single time
 and then stop.
        imageanim.setImageDrawable(animation);
        animation.start();

        imageanim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                animation new AnimationDrawable();
                animation.addFrame(getResources().getDrawable(R.drawable.speaker1), 500);
                animation.addFrame(getResources().getDrawable(R.drawable.speaker2), 500);
                animation.addFrame(getResources().getDrawable(R.drawable.speaker3), 500);
                animation.addFrame(getResources().getDrawable(R.drawable.speaker4), 500);
                animation.addFrame(getResources().getDrawable(R.drawable.speaker1), 500);
                animation.setOneShot(true);//If true, the animation will only run a 
                single time and then stop.
                imageanim.setImageDrawable(animation);
                animation.start();
            }
        });

    }
}