如何在Android动画.gif图片? [英] How to animate .gif images in an android?

查看:127
本文介绍了如何在Android动画.gif图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是$ C $下的xml:

 < ImageView的
    机器人:ID =@ + ID / imageView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:SRC =@可绘制/ minepic/>
 

下面的minepic是一个GIF动画形象,但在运行应用程序后,它只是显示静态图像。

  

时的明镜有关如何在android应用动画.gif图片任何解决方案。

解决方案

要给予precise这里完整的答案是什么,你需要聪明的执行步骤:

  1. 您需要有不同的巴纽图像将作为 帧的动画。将它们保存在 RES /绘制文件夹。

  2. 科瑞 anim.xml RES文件/绘制文件夹下面的内容:

     < XML版本=1.0编码=UTF-8&GT?;
    <动画列表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:单稳=假>
    
        <项目机器人:可绘制=@可绘制/ IMAGE_3机器人:时间=250/>
        <项目机器人:可绘制=@可绘制/ IMAGE_2机器人:时间=250/>
        <项目机器人:可绘制=@可绘制/ IMAGE_1机器人:时间=250/>
        <项目机器人:可绘制=@可绘制/图像机器人:时间=250/>
    
    < /动画列表>
     

  3. 在布局 XML 文件中要显示动画:

      // ...
    < ImageView的
         机器人:ID =@ + ID / ivAnimation
         机器人:layout_width =WRAP_CONTENT
         机器人:layout_height =WRAP_CONTENT
         机器人:layout_centerHorizo​​ntal =真
         机器人:contentDescription =动画/>
    // ...
     

  4. 在它加载的布局XML文件并调用Java文件 的setContentView

      // ...
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
    
        最后ImageView的animImageView =(ImageView的)findViewById(R.id.ivAnimation);
        animImageView.setBackgroundResource(R.drawable.anim);
        animImageView.post(新的Runnable(){
            @覆盖
            公共无效的run(){
                AnimationDrawable frameAnimation =
                    (AnimationDrawable)animImageView.getBackground();
                frameAnimation.start();
            }
        });
        // ...等code ...
    }
    // ...
     

希望它可以帮助别人。

Here is the code for 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.

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

解决方案

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. Save them in res/drawable folder.

  2. Creat anim.xml file in res/drawable folder with following content:

    <?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:

    //...
    <ImageView
         android:id="@+id/ivAnimation"
         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:

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

Hope it helps someone.

这篇关于如何在Android动画.gif图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆