分解gif图片的工具很多,我用的Photoshop。

用Photoshop打开gif图片,然后文件--->导出--->渲染视频即可。

android gif动画_第1张图片

android gif动画_第2张图片

相应设置点击渲染即可。然后会出来4张图片

android gif动画_第3张图片

然后把图片们放进资源文件夹中,我放在了res/raw中;

android gif动画_第4张图片

在drawable中建立一个xml文件

            
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:oneshot="false" > 
  4.     <item 
  5.         android:drawable="@raw/gj1" 
  6.         android:duration="100"/> 
  7.     <item 
  8.         android:drawable="@raw/gj2" 
  9.         android:duration="100"/> 
  10.     <item 
  11.         android:drawable="@raw/gj3" 
  12.         android:duration="100"/> 
  13.     <item 
  14.         android:drawable="@raw/gj4" 
  15.         android:duration="100"/> 
  16.  
  17. animation-list> 

android:oneshot:true播放一次,false重复播放

android:duration:事件间隔

然后在需要用到的控件中

            
  1. <ImageView 
  2.        android:id="@+id/iv_gj" 
  3.        android:layout_width="wrap_content" 
  4.        android:layout_height="wrap_content" 
  5.        android:background="@drawable/gif_gj" /> 

java代码:

            
  1. package com.example.giftest; 
  2.  
  3. import android.app.Activity; 
  4. import android.graphics.drawable.AnimationDrawable; 
  5. import android.os.Bundle; 
  6. import android.widget.ImageView; 
  7.  
  8. public class MainActivity extends Activity { 
  9.  
  10.     private ImageView iv_gj; 
  11.  
  12.     @Override 
  13.     protected void onCreate(Bundle savedInstanceState) { 
  14.         super.onCreate(savedInstanceState); 
  15.         setContentView(R.layout.activity_main); 
  16.         iv_gj = (ImageView) findViewById(R.id.iv_gj); 
  17.     } 
  18.  
  19.     /* 
  20.      * 如果想让动画一开始就播放,可以重写此方法 
  21.      */ 
  22.     @Override 
  23.     public void onWindowFocusChanged(boolean hasFocus) { 
  24.          
  25.         /* 触发动画 */ 
  26.         AnimationDrawable ad = (AnimationDrawable) iv_gj.getBackground();
  27.  
  28. //如果xml中是这样设置的:android:src="@drawable/gif_gj",则使用下面的方法 //AnimationDrawable ad = (AnimationDrawable) iv_gj.getDrawable();
  29.         ad.stop(); 
  30.         ad.start(); 
  31.         /* ***** */ 
  32.          
  33.         super.onWindowFocusChanged(hasFocus); 
  34.     } 
  35.  

 

更多相关文章

  1. Android 4.1源码编译找不到资源文件解决办法
  2. 从Android读取Unity assets下的图片并保存为Bitmap格式
  3. 开机动画(闪动的ANDROID字样的动画图片)
  4. Android图文详解属性动画
  5. Android开机动画过程
  6. Android 根文件系统分析
  7. Android中的AndroidManifest.xml文件

随机推荐

  1. 使用Java语言如何实现快速文件复制?
  2. [源码和文档分享]基于java的RPG回合制游
  3. 工作中傻傻的错-2011/11
  4. 使用字符串参数调用AndroidJni静态方法。
  5. 在jasper中获取组部分报告API java
  6. 抢分啦!谁能帮我解决java的Socket的问题?
  7. JNI学习笔记(五)——fields和methods
  8. Java常量表达式相关的编译优化代码
  9. 请问JAVA求职英语水平的要求
  10. Java NIO 学习笔记(七)----NIO/IO 的对比和