顯示gif動畫(幀動畫的播放)

在android上顯示gif不太方便,雖然有控件能夠實現,可是效果不是很好,保險點兒的做法仍是使用幀動畫來處理。
①在XML中定義animation-list:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" >java

    <item android:drawable="@drawable/f_1" android:duration="30"/>
    <item android:drawable="@drawable/f_2" android:duration="30"/>
    <item android:drawable="@drawable/f_3" android:duration="30"/>
    <item android:drawable="@drawable/f_4" android:duration="30"/>
</animation-list>
②在XML佈局中定義ImageView,指定其src屬性或background屬性爲"@anim/loading"
③在java代碼中:
ImageView imageView = (ImageView) findViewById(R.id.frame);
AnimationDrawable drawable = (AnimationDrawable) imageView.getDrawable();
// 使用background屬性時
// AnimationDrawable drawable = (AnimationDrawable) imageView.getBackground();
drawable.setOneShot(false); // 重複播放
④在java代碼中調用:
drawable.start()或drawable.stop()實現動畫的播放和中止。

值得注意的是上面的代碼在android4.0系統沒問題,可是在2.3系統上動畫無效,因此咱們須要使用另外一種方式。android

// 兼容android 4.0 如下系統
imageView.post(new Runnable() {ide

  @Override
  public void run() {
    draw.start();
  }
});佈局

相關文章
相關標籤/搜索