分解gif圖片的工具不少,我用的Photoshop。java
用Photoshop打開gif圖片,而後文件--->導出--->渲染視頻便可。android
相應設置點擊渲染便可。而後會出來4張圖片app
而後把圖片們放進資源文件夾中,我放在了res/raw中;ide
在drawable中創建一個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="@raw/gj1"
- android:duration="100"/>
- <item
- android:drawable="@raw/gj2"
- android:duration="100"/>
- <item
- android:drawable="@raw/gj3"
- android:duration="100"/>
- <item
- android:drawable="@raw/gj4"
- android:duration="100"/>
- </animation-list>
android:oneshot:true播放一次,false重複播放動畫
android:duration:事件間隔spa
而後在須要用到的控件中視頻
- <ImageView
- android:id="@+id/iv_gj"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/gif_gj" />
java代碼:xml
- package com.example.giftest;
- import android.app.Activity;
- import android.graphics.drawable.AnimationDrawable;
- import android.os.Bundle;
- import android.widget.ImageView;
- public class MainActivity extends Activity {
- private ImageView iv_gj;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- iv_gj = (ImageView) findViewById(R.id.iv_gj);
- }
- /*
- * 若是想讓動畫一開始就播放,能夠重寫此方法
- */
- @Override
- public void onWindowFocusChanged(boolean hasFocus) {
- /* 觸發動畫 */
- AnimationDrawable ad = (AnimationDrawable) iv_gj.getBackground();
- //若是xml中是這樣設置的:android:src="@drawable/gif_gj",則使用下面的方法 //AnimationDrawable ad = (AnimationDrawable) iv_gj.getDrawable();
- ad.stop();
- ad.start();
- /* ***** */
- super.onWindowFocusChanged(hasFocus);
- }
- }