gpt4 book ai didi

android - 如何在 android 中为 .gif 图像设置动画?

转载 作者:IT老高 更新时间:2023-10-28 23:17:37 25 4
gpt4 key购买 nike

这里是 xml 的代码:

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/minepic" />

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

Is there any solution about how to animate the .gif images in android application?

最佳答案

要在这里给出准确而完整的答案,您需要逐步进行:

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

  2. res/drawable文件夹中创建anim.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_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>
  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" />
    //...
  4. 在加载布局xml文件并调用的Java文件中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 ...
    }
    // ...

为了停止动画,您可以在 AnimationDrawable 上调用 .stop()。有关可用方法的更多详细信息,您可以查看AnimationDrawable文档。希望它可以帮助某人。

关于android - 如何在 android 中为 .gif 图像设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15022152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com