I want to show an animated GIF image in an android application like the image below. I have tried the webview but no success. How to show the animated gif in the application
I tried so many library to use Animated gif . But every library is lagging and crushing . But now , after one or two day research i got a idea to use animated gif and the performance is very good no lagging no crushing.
Solution is that use Glide
Follow the below step .
in xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:background="#62b849"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/splace_image_view"
android:layout_centerInParent="true"/>
</RelativeLayout>
and in your java
file
ImageView imageView = findViewById(R.id.splace_image_view);
Glide
.with(this)
.load(R.drawable.football)
.into(imageView);
You can also use this lib to easily support a gifDrawable.
Just use GifImageView instead of normal ImageView:
<pl.droidsonroids.gif.GifImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_anim"/>
and locate your gif-file under the src attr. That's all!
Best and easiest solution to display GIF image in Android and it will work perfectly:
Open layout folder and put this code where you want to display GIF image: e-g activity_main.xml
<pl.droidsonroids.gif.GifImageView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:src="@drawable/your_gif_file_name"/>
android:src="@drawable/your_gif_file_name", Replace 'your_gif_file_name' with your desired gif image file
Try this way :
Movie movie,movie1;
InputStream is=null,is1=null;
long moviestart;
long moviestart1;
public GIFView(Context context) {
super(context);
is=context.getResources().openRawResource(R.drawable.hxps);
is1=context.getResources().openRawResource(R.drawable.cartoon);
movie=Movie.decodeStream(is);
movie1=Movie.decodeStream(is1);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFCCCCCC);
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();
System.out.println("now="+now);
if (moviestart == 0) { // first time
moviestart = now;
}
if(moviestart1==0)
{
moviestart1=now;
}
System.out.println("\tmoviestart="+moviestart);
int relTime = (int)((now - moviestart) % movie.duration()) ;
int relTime1=(int)((now - moviestart1)% movie1.duration());
System.out.println("time="+relTime+"\treltime="+movie.duration());
movie.setTime(relTime);
movie1.setTime(relTime1);
movie.draw(canvas,0,0);
movie1.draw(canvas,10,300);
this.invalidate();
}
You could just add a webview and show a gif image in that, Like:
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/Rotating_earth_(large).gif/200px-Rotating_earth_(large).gif");
This will show the gif image in your app.
Hope it helps! Good Luck!
You can use Glide :
ImageView imageView = (ImageView) findViewById(R.id.imageView);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
Glide.with(this).load(R.raw.sample_gif).into(imageViewTarget);