How to show Animated GIF image in android application?

后端 未结 11 1560
臣服心动
臣服心动 2020-12-28 13:16

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

相关标签:
11条回答
  • 2020-12-28 13:52

    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);
    
    0 讨论(0)
  • 2020-12-28 13:55

    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!

    0 讨论(0)
  • 2020-12-28 13:57

    Best and easiest solution to display GIF image in Android and it will work perfectly:

    • Open build.gradle (Module: app)
    • put in dependencies: compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.+'
    • 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

    0 讨论(0)
  • 2020-12-28 13:57

    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();
        }
    

    enter image description here

    0 讨论(0)
  • 2020-12-28 13:58

    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!

    0 讨论(0)
  • 2020-12-28 14:00

    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);
    
    0 讨论(0)
提交回复
热议问题