android gif動畫

 分解gif圖片的工具不少,我用的Photoshop。java

用Photoshop打開gif圖片,而後文件--->導出--->渲染視頻便可。android

相應設置點擊渲染便可。而後會出來4張圖片app

而後把圖片們放進資源文件夾中,我放在了res/raw中;ide

在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:事件間隔spa

而後在須要用到的控件中視頻

  
  
           
  
  
  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代碼:xml

  
  
           
  
  
  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.  
相關文章
相關標籤/搜索