【问题标题】:How to animate .gif images in an android?如何在 android 中为 .gif 图像设置动画?
【发布时间】:2023-03-25 17:25:01
【问题描述】:

这里是 xml 的代码:

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

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

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

【问题讨论】:

标签: android jquery-animate gif animationdrawable


【解决方案1】:

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

  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 文档。希望它可以帮助某人。

【讨论】:

  • 不知道作者为什么不接受这个。这个解决方案很完美,谢谢。
  • 很好的答案,但有一个建议:您不必发布为 new Runnable() ,因为此后您无法访问 frameAnimation 对象,例如停止动画。查看谷歌的示例代码developer.android.com/reference/android/graphics/drawable/…
  • @kevinze 你是对的。这说得通。我认为如果需要它来无限循环动画,那么上面的片段仍然可以工作。否则,如果动画需要在某个点停止或需要访问可运行对象之外的对象,您的建议绝对是应该使用的。
  • @ShobhitPuri 我们如何停止动画?
  • @Umair 您可以使用.stop() 或者可以预先定义它是否应该只循环一次或设置持续时间。在此处查看有关方法的更多信息:developer.android.com/reference/android/graphics/drawable/…
【解决方案2】:

我不建议使用MovieWebView 类,而是使用ImageView 将源可绘制对象设置为animation-list。看例子(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" />

显然,在使用此解决方案之前,您必须将 .gif 分割成单独的图像。

【讨论】:

  • 这不需要事先对 gif 进行切片吗?
  • 是的,我认为这对他来说是可能的。
  • 在这种情况下,这绝对是更好的方法。但是当例如下载一个 gif 并显示它时。这可能是个问题。
  • 感谢@Piotr 它的好主意,我刚刚检查过了,但是我们不能在不写任何some.xml 的情况下直接为 gif 图像设置动画,并且我们正在定义我们试图对其进行动画处理的图像切片。
  • 除非使用 AnimationDrawable,否则它不是动画。有什么办法只使用xml?
【解决方案3】:

据我了解,您的 GIF 图片不会移动,因此如果您将 GIF 视为静态图片,这就是 Android 的原生行为。对我来说,最好的解决方案(不要重新发明轮子!)是开源库gitDrawable。检查他们的 README,一切都非常简单:将依赖项添加到 gradle 并使用(在 XML 或代码中)。 Java中的使用示例:

GifDrawable gifFromResource = new GifDrawable( getResources(), R.drawable.anim );

【讨论】:

    【解决方案4】:
    And i think this is the one way of solution by writing the anim-space.xml 
    In which the slices of the image are added to the items one by one and setting that xml to the imageview of our layout xml.
    

    http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html

    在此开发人员链接中已明确定义。

    【讨论】:

      【解决方案5】:

      我能够使用这个简单的代码为 android 图像制作动画:

      canvas.drawColor(Color.WHITE);
      super.onDraw(canvas);
      
      
      long now=android.os.SystemClock.uptimeMillis();
      System.out.println("now="+now);
      if (moviestart == 0) { // first time
      moviestart = now;
      
      }
      
      System.out.println("\tmoviestart="+moviestart);
      int relTime = (int)((now - moviestart) % movie.duration()) ;
       System.out.println("time="+relTime+"\treltime="+movie.duration());
      movie.setTime(relTime);
      movie.draw(canvas,this.getWidth()/2-20,this.getHeight()/2-40);
      this.invalidate();
       }
      


      使用movieview很容易实现

      或者我可以给你tutorial这个东西

      【讨论】:

        【解决方案6】:

        使用线程(即 View.post(new Runnable))不是一个好习惯,因为在绘制可绘制对象期间视图可能已更改(一种情况是使用 ListView 上的动画图像,其中包含不同的项目背景图像),如果 ImageView 在线程运行时具有不是动画资源的背景,则可能会导致 ClassCastException。

        ImageView loadingImg = (ImageView)v.findViewById(R.id.image);
        loadingImg.setBackgroundResource(R.drawable.progressdialog);
        loadingImg.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
          @Override
          public void onViewAttachedToWindow(View v) {
            AnimationDrawable loadingAnimation = (AnimationDrawable) v.getBackground();
            loadingAnimation.start();
          }
        
          @Override
          public void onViewDetachedFromWindow(View v) {
          }
        });
        

        示例显示here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-28
          • 2014-12-29
          • 2016-10-14
          • 2016-11-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多