如何在安卓应用中展示动态GIF图片?

34

我想在Android应用程序中展示一个动态GIF图片,就像下面这张图片。我尝试过使用WebView但没有成功。如何在应用程序中展示动态gif?enter image description here


这将有所帮助 https://dev59.com/UHA65IYBdhLWcg3wvhTi - Rajeev N B
还可以看看这个,https://dev59.com/eWox5IYBdhLWcg3whUgV - Aerrow
针对Android Pie及以上版本,请查看https://dev59.com/UHA65IYBdhLWcg3wvhTi#51737794。 - Pavneet_Singh
12个回答

31
你还可以使用这个库来轻松支持一个gifDrawable。只需使用GifImageView替代普通的ImageView:
<pl.droidsonroids.gif.GifImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/your_anim"/>

找到你的gif文件并将其放在src属性下面。就这样!


自 API 8+ 以来,它肯定可以正常工作。也许您实现了错误的依赖项或存储库。 - RyuZz
2
这是不重复动画,你能告诉我如何做吗? - Lakhwinder Singh
2
@warlock,你的gif文件应该自动重复播放,这是无法通过编程实现的。在Photoshop中,你可以这样做:“文件>保存为Web和设备”,然后你可以设置循环选项。 - RyuZz

20

你可以使用Glide

ImageView imageView = (ImageView) findViewById(R.id.imageView);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
Glide.with(this).load(R.raw.sample_gif).into(imageViewTarget);

你如何设置持续时间?我的意思是动画速度。 - sasan

10

经过长时间的谷歌搜索,我知道没有原生支持GIF图片。在应用程序中显示动画GIF图像没有合适的解决方案。您可以查看此解决方案来使动画GIF在布局中播放。


5
我尝试过很多库来使用动画gif。 但每个库都会出现卡顿和崩溃的情况。 现在,经过一两天的研究,我有了一个使用动画gif的想法,性能非常好,没有卡顿也没有崩溃。
解决方案是使用Glide
按照以下步骤操作。
在xml文件中。
<?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> 

在你的Java文件中

 ImageView imageView = findViewById(R.id.splace_image_view);

Glide
        .with(this)
        .load(R.drawable.football)
        .into(imageView);

4

在Android中显示GIF图像的最佳和最简单的解决方案,它可以完美地工作:

  • 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


2

您不需要使用任何库,只需使用以下代码:

步骤1: 创建一个名为GIFView.java的文件

package com.thigale.testproject;

/**
 * Created by Thigale Sameer on 11-12-16.
 */

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;

import java.io.InputStream;

public class GifView extends View {
public Movie mMovie;
public long movieStart;
private int gifId;

public GifView(Context context) {
    super(context);
}

public GifView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initializeView(attrs.getAttributeResourceValue("http://schemas.android.com/apk/res-auto", "src", 0));
}

public GifView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initializeView(attrs.getAttributeResourceValue("http://schemas.android.com/apk/res-auto", "src", 0));
}

private void initializeView(final int id) {
    InputStream is = getContext().getResources().openRawResource(id);
    mMovie = Movie.decodeStream(is);
    this.gifId = id;
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.TRANSPARENT);
    super.onDraw(canvas);
    long now = android.os.SystemClock.uptimeMillis();

    if (movieStart == 0) {
        movieStart = now;
    }

    if (mMovie != null) {
        int relTime = (int) ((now - movieStart) % mMovie.duration());
        mMovie.setTime(relTime);
        mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight() - mMovie.height());
        this.invalidate();
    }
}

public void setGIFResource(int resId) {
    this.gifId = resId;
    initializeView(this.gifId);
}

public int getGIFResource() {
    return this.gifId;
}
}

步骤 2: 在 res/attrs.xml 文件中添加以下行

<declare-styleable name="GIFView">
  <attr name="src" format="reference" />
</declare-styleable>

步骤3: 在特定的活动中,在 AndroidManifest.xml 中添加此行

android:hardwareAccelerated="false"

步骤4:在您的XML中创建此视图:

<com.thigale.testproject.GifView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    components:src="@drawable/loading" />

步骤5 在创建视图的父活动中添加以下行:

xmlns:components="http://schemas.android.com/apk/res-auto"

1

@aman.nepid 给你的链接中提供了所有内容。在那个示例中,wheel1、wheel2、wheel3 是不同的框架。如果你有一个设计师,就请他/她给出 GIF 图像作为框架。 - TNR

1
你可以添加一个WebView并在其中显示一个GIF图像,例如:
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");

这将在您的应用程序中显示gif图像。

希望能帮到您!祝好运!


1
如果我们需要从Uri显示一个GIF,你可以使用Fresco。

build.gradle(:app)

dependencies {
    implementation 'com.facebook.fresco:fresco:3.0.0'
    implementation 'com.facebook.fresco:animated-gif:3.0.0'
    implementation 'com.facebook.fresco:animated-webp:3.0.0'
    implementation 'com.facebook.fresco:webpsupport:3.0.0'
}

activity_main.xml

   <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/displaygif"
        android:layout_width="150dp"
        android:layout_height="150dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5"
        app:srcCompat="@drawable/anyimage"
        />

MainActivity.java

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView; 
...
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Fresco.initialize(this);
            setContentView(R.layout.activity_main);
...
            //convert string to Uri
            Uri myUri = Uri.parse("..anygifUri..");
            SimpleDraweeView displaygif = (SimpleDraweeView) findViewById(R.id.displaygif);
            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setUri(myUri)
                    .setAutoPlayAnimations(true)
                    .build();
            displaygif.setController(controller);

0

尝试这种方式:

    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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接