如何在Android中动画.gif图像?

Dan*_*nny 17 android gif jquery-animate animationdrawable

这是xml的代码:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/minepic" />
Run Code Online (Sandbox Code Playgroud)

这里的minepic是一个gif动画图像,但在运行应用程序后它只显示一个静态图像.

有没有关于如何在Android应用程序中动画.gif图像的解决方案?

Sho*_*uri 37

为了给出一个精确而完整的答案,您需要逐步做到:

  1. 您需要使用不同的.png图像作为动画的帧.将它们保存在res/drawable文件夹中

  2. 使用以下内容anim.xmlres/drawable文件夹中创建文件:

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
        android:oneshot="false">
    
        <item android:drawable="@drawable/image_3" android:duration="250" />
        <item android:drawable="@drawable/image_2" android:duration="250" />
        <item android:drawable="@drawable/image_1" android:duration="250" />
        <item android:drawable="@drawable/image" android:duration="250" />
    
    </animation-list>
    
    Run Code Online (Sandbox Code Playgroud)
  3. xml要在其中显示动画的布局文件中:

    //...
    <ImageView
         android:id="@+id/iv_animation"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:contentDescription="Animation" />
    //...
    
    Run Code Online (Sandbox Code Playgroud)
  4. 在Java文件中加载布局xml文件并调用 setContentView:

    //...
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        final ImageView animImageView = (ImageView) findViewById(R.id.iv_animation);
        animImageView.setBackgroundResource(R.drawable.anim);
        animImageView.post(new Runnable() {
            @Override
            public void run() {
                AnimationDrawable frameAnimation =
                    (AnimationDrawable) animImageView.getBackground();
                frameAnimation.start();
            }
        });
        // ... other code ... 
    }
    // ...
    
    Run Code Online (Sandbox Code Playgroud)

为了阻止你可以调用动画.stop()AnimationDrawable.有关可用方法的更多详细信息,请参阅AnimationDrawable文档.希望它可以帮助某人.

  • @Umair你可以使用`.stop()`或者可以预先定义它是否应该只循环一次或设置持续时间.在此处查看有关方法的更多信息:https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html (2认同)

Pio*_*otr 14

我不建议使用MovieWebView类,ImageView而是使用源drawable设置animation-list.看看example(mydrawable.xml):

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/image_1" android:duration="100" />
<item android:drawable="@drawable/image_2" android:duration="100" />
<item android:drawable="@drawable/image_3" android:duration="100" />
</animation-list> 

// in your layout : <ImageView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/my_drawable" />
Run Code Online (Sandbox Code Playgroud)

显然,在使用此解决方案之前,您必须将.gif切片为单独的图像.

  • 这不需要事先切片gif吗? (2认同)
  • 除非使用 AnimationDrawable,否则它不会动画。有没有办法只使用xml? (2认同)