Normally android image view shows only a simple image like JPG, PNG, JPEG format, but sometime we need to show animated image on my activity. Animated image help user to easily understand. So, when we need to show animated (GIF) image in activity we'll need a another apply for that.
There are no any given tools to show animated image on activity but we can show animation images by adding a dependence in the grade file. After using this dependency we can add a view for GIF image like Image view.
Required dependence is:
compile 'com.github.Cutta:GifView:1.1'
Required repositories is:
maven { url 'https://jitpack.io' }
After adding these dependence and repositories to your gradle file you can use in your activity. Now create your XML layout:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.legendblogs.animatedimage.MainActivity">
<com.cunoraz.gifview.library.GifView
android:id="@+id/sample_gif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Legend Blogs"
android:textSize="25dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Mainactivity:
package com.example.legendblogs.animatedimage;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.cunoraz.gifview.library.GifView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GifView sample_gif = (GifView) findViewById(R.id.sample_gif);
sample_gif.setGifResource(R.drawable.legendblogs);
sample_gif.play();
}
}
Mainifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.legendblogs.animatedimage">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
String:
<resources>
<string name="app_name">Animated Image Sample</string>
</resources>
Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.example.legendblogs.animatedimage"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
maven { url 'http://repo1.maven.org/maven2' }
maven { url 'https://jitpack.io' }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.github.Cutta:GifView:1.1'
}
Write a comment