How To Make Animated Gif On Android
In this article, we are going to testify an animated gif in our project using a library. There are many methods to show a gif. We tin can too show a gif using WebView. Here nosotros are going to use this library to bear witness the gif. Then hither we are going to acquire how to implement that feature. A sample GIF is given below to get an idea nigh what we are going to exercise in this commodity. Note that we are going to implement this projection using the Coffee language.
Step by Pace Implementation
Method one
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 equally the programming language.
Step ii: Insert the following dependency to build.gradle file of your projection
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the beneath dependency in the dependencies section.
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.22'
Notation that Maven key repository should be divers eg. in top-level build.gradle similar this:
buildscript {
repositories {
mavenCentral()
}
}
allprojects {
repositories {
mavenCentral()
}
}
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below lawmaking to that file. Beneath is the lawmaking for the activity_main.xml file.
XML
<?
xml
version
=
"1.0"
encoding
=
"utf-eight"
?>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
tools:context
=
".MainActivity"
>
<
pl.droidsonroids.gif.GifImageView
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:background
=
"#fff"
android:src
=
"@drawable/android"
/>
</
LinearLayout
>
Step 4: Working with the MainActivity.coffee file
There is nothing to do with the MainActivity.java file.
Method 2
Here we are loading the gif using ImageView and Glide Library. Insert the following dependency to build.gradle file of your project.
implementation 'com.github.bumptech.glide:glide:iv.9.0'
Step i: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below lawmaking to that file. Below is the lawmaking for the activity_main.xml file.
XML
<?
xml
version
=
"1.0"
encoding
=
"utf-eight"
?>
<
androidx.constraintlayout.widget.ConstraintLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
tools:context
=
".MainActivity"
>
<
ImageView
android:id
=
"@+id/imageview"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
app:layout_constraintBottom_toBottomOf
=
"parent"
app:layout_constraintLeft_toLeftOf
=
"parent"
app:layout_constraintRight_toRightOf
=
"parent"
app:layout_constraintTop_toTopOf
=
"parent"
/>
</
androidx.constraintlayout.widget.ConstraintLayout
>
Step 2: Working with the MainActivity.coffee 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 within the code to understand the code in more item.
Java
import
android.bone.Bundle;
import
android.widget.ImageView;
import
androidx.appcompat.app.AppCompatActivity;
import
com.bumptech.glide.Glide;
public
class
MainActivity
extends
AppCompatActivity {
ImageView imageView;
@Override
protected
void
onCreate(Packet savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageview);
Glide.with(
this
).load(R.drawable.android).into(imageView);
}
}
Method iii
Here nosotros are loading the gif using WebView.
Stride 1: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add together the below code to that file. Below is the code for the activity_main.xml file. We will create a unproblematic WebView in this file.
XML
<?
xml
version
=
"i.0"
encoding
=
"utf-8"
?>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
tools:context
=
".MainActivity"
>
<
WebView
android:id
=
"@+id/webvidew"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
/>
</
LinearLayout
>
Footstep two: Working with the MainActivity.coffee file
Go to the MainActivity.java file and refer to the post-obit code. Beneath is the code for the MainActivity.coffee file. Comments are added inside the lawmaking to empathise the code in more detail.
Java
import
android.os.Package;
import
android.webkit.WebView;
import
android.webkit.WebViewClient;
import
androidx.appcompat.app.AppCompatActivity;
public
course
MainActivity
extends
AppCompatActivity {
WebView webView;
@Override
protected
void
onCreate(Package savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webvidew);
webView.getSettings().setJavaScriptEnabled(
truthful
);
webView.setWebViewClient(
new
WebViewClient());
webView.loadUrl(
"//Add the link of gif here"
);
}
}
Output:
Source: https://www.geeksforgeeks.org/how-to-use-animated-gif-in-android-app/
Posted by: robinscomagese.blogspot.com
0 Response to "How To Make Animated Gif On Android"
Post a Comment