简体   繁体   English

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

[英]How to animate .gif images in an android?

Here is the code for xml:这是xml的代码:

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

Here the minepic is a gif animated image but after running the application its just showing a static image.这里的 minepic 是一个 gif 动画图像,但在运行应用程序后它只是显示一个静态图像。

Is there any solution about how to animate the .gif images in android application?有没有关于如何在 android 应用程序中为 .gif 图像设置动画的解决方案?

To give a precise and complete answer here is what you need to do step wise:要在此处给出准确完整的答案,您需要逐步执行以下操作:

  1. You would need to have different .png images which will act as frames for your animation.您需要使用不同的.png图像作为动画的帧。 Save them in res/drawable folder.将它们保存在res/drawable文件夹中。

  2. Create anim.xml file in res/drawable folder with following content: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. In the layout xml file inside which you want to show the animation:在要在其中显示动画的布局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. In the Java file which loads the the layout xml file and calls setContentView :在加载布局 xml 文件并调用setContentView的 Java 文件中:

     //... @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 ... } // ...

In order to stop the animation you can call .stop() on the AnimationDrawable .为了停止动画,您可以在AnimationDrawable上调用.stop() For more details about the available methods, you can see AnimationDrawable documentation.有关可用方法的更多详细信息,您可以查看AnimationDrawable文档。 Hope it helps someone.希望它可以帮助某人。

I wouldn't recommend using Movie or WebView classes but ImageView instead with source drawable set to animation-list .我不建议使用MovieWebView类,而是使用ImageView将 source drawable 设置为animation-list Look at example( mydrawable.xml ):看例子( 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" />

Obviously, you have to slice your .gif into seperate images before using this solution.显然,在使用此解决方案之前,您必须将 .gif 分割成单独的图像。

As far as I understand, your GIF image is not moving, so that's the native Android behaviour if you treat GIF like a static picture.据我了解,您的 GIF 图像不会移动,因此如果您将 GIF 视为静态图片,这就是原生的 Android 行为。 For me the best solution (not to reinvent the wheel!) was the open-source library gitDrawable .对我来说,最好的解决方案(不要重新发明轮子!)是开源库gitDrawable Check their README, everything is very simple: add dependency to gradle and use(in XML or code).检查他们的自述文件,一切都很简单:向 gradle 添加依赖项并使用(在 XML 或代码中)。 Example of usage in Java: Java 中的用法示例:

GifDrawable gifFromResource = new GifDrawable( getResources(), R.drawable.anim );
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 http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html

In this developers link it is defined clearly.在这个开发人员链接中,它被明确定义。

Well i was able to animate android images using this simple code:好吧,我能够使用这个简单的代码为 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();
 }


it was implemented easily using movieview它使用movieview很容易实现

or i can i give you a tutorial for this stuff或者我可以给你一个关于这个东西的教程

It's not good practice to use Threads (ie View.post(new Runnable)) because the view might have changed during the time the drawable is going to be painted (one case is using the animated image on ListView with items containing different background images), which may cause a ClassCastException if the ImageView, by the time the thread runs, has a background that is not an animated resource.使用线程(即 View.post(new Runnable))不是一个好习惯,因为在绘制 drawable 期间视图可能已经改变(一种情况是使用 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) {
  }
});

Example shown here此处显示的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM