Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

仿慕課網下拉加載動畫:自定義 ImageView + 幀動畫

編輯:關於 Android 程式設計

下拉刷新時那個轉圈的加載動畫,其實就是一組幀圖在循環播放。要讓它能隨時開關、又能到處複用,最乾脆的做法是繼承 ImageView,把播放與停止包成方法,並掛在 setVisibility 上——顯示就播,隱藏就停。

一、自定義 ImageView

package com.example.loading_drawable;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

public class MyImgView extends ImageView {

    private AnimationDrawable bgAnim;

    public MyImgView(Context context) {
        super(context, null);
        initView();
    }

    public MyImgView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        initView();
    }

    public MyImgView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        setBackgroundResource(R.drawable.flash_anim);
        bgAnim = (AnimationDrawable) getBackground();
    }

    /** 開始播放 */
    public void startAnim() {
        if (bgAnim != null && !bgAnim.isRunning()) {
            bgAnim.start();
        }
    }

    /** 停止播放 */
    public void stopAnim() {
        if (bgAnim != null && bgAnim.isRunning()) {
            bgAnim.stop();
        }
    }

    /** 顯示時自動播放,隱藏時自動停止 */
    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == View.VISIBLE) {
            startAnim();
        } else {
            stopAnim();
        }
    }
}

這裡有個容易忽略的細節:三個建構子都要呼叫 initView()。因為程式碼 new 出來的 view 走的是一參數版本,XML 宣告的走兩參數版本,帶 defStyle 的則是三位元組版本。只寫一處,另一種建立方式就會拿到未初始化的空殼。

二、幀動畫資源檔

在 res/drawable/ 下建立 flash_anim.xml,用 animation-list 把各幀串起來。oneshot="false" 表示循環播放,每幀 duration 為 50 毫秒,換算下來約每秒 20 幀:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
    android:oneshot="false">

    <item android:drawable="@drawable/a01_02" android:duration="50" />
    <item android:drawable="@drawable/a01_04" android:duration="50" />
    <item android:drawable="@drawable/a01_06" android:duration="50" />
    <item android:drawable="@drawable/a01_08" android:duration="50" />
    <item android:drawable="@drawable/a01_10" android:duration="50" />
    <item android:drawable="@drawable/a01_12" android:duration="50" />
    <item android:drawable="@drawable/a01_14" android:duration="50" />
    <item android:drawable="@drawable/a01_16" android:duration="50" />
    <item android:drawable="@drawable/a01_25" android:duration="50" />
    <item android:drawable="@drawable/a01_26" android:duration="50" />
    <item android:drawable="@drawable/a01_27" android:duration="50" />
    <item android:drawable="@drawable/a01_28" android:duration="50" />
    <item android:drawable="@drawable/a01_30" android:duration="50" />
    <item android:drawable="@drawable/a01_31" android:duration="50" />
    <item android:drawable="@drawable/a01_32" android:duration="50" />
    <item android:drawable="@drawable/a01_41" android:duration="50" />
    <item android:drawable="@drawable/a01_42" android:duration="50" />
    <item android:drawable="@drawable/a01_43" android:duration="50" />
    <item android:drawable="@drawable/a01_44" android:duration="50" />
    <item android:drawable="@drawable/a01_45" android:duration="50" />
    <item android:drawable="@drawable/a01_46" android:duration="50" />
    <item android:drawable="@drawable/a01_47" android:duration="50" />
    <item android:drawable="@drawable/a01_48" android:duration="50" />
    <item android:drawable="@drawable/a01_57" android:duration="50" />
    <item android:drawable="@drawable/a01_58" android:duration="50" />
    <item android:drawable="@drawable/a01_59" android:duration="50" />
    <item android:drawable="@drawable/a01_60" android:duration="50" />
    <item android:drawable="@drawable/a01_61" android:duration="50" />
    <item android:drawable="@drawable/a01_62" android:duration="50" />
    <item android:drawable="@drawable/a01_63" android:duration="50" />
    <item android:drawable="@drawable/a01_64" android:duration="50" />

</animation-list>

三、在主頁面使用

這裡用純程式碼組版面,方便直接嵌進現有專案:

package com.example.loading_drawable;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

/** 慕課網風格的下拉刷新進度顯示控件 */
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout rootLayout = new LinearLayout(this);
        rootLayout.setOrientation(LinearLayout.VERTICAL);
        rootLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        rootLayout.setGravity(Gravity.CENTER);

        Button btn = new Button(this);
        btn.setText("展現動畫");

        final MyImgView imgView = new MyImgView(MainActivity.this);
        imgView.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        imgView.setVisibility(View.GONE);   // 初始隱藏,動畫不會播放

        rootLayout.addView(btn);
        rootLayout.addView(imgView);
        setContentView(rootLayout);

        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                imgView.setVisibility(View.VISIBLE);   // 顯示即播放
            }
        });
    }
}

設計思路小結

這個做法的巧妙之處在於把「播放時機」綁進 setVisibility:呼叫端完全不需要記得去呼叫 startAnim(),只要照平常的方式控制顯示/隱藏,動畫就會跟著開關。用在下拉刷新、上拉加載這種「需要時才出現」的場景特別合適。

實務上要留意兩點。第一,幀動畫的所有圖片都會一次性載入記憶體,31 張圖若解析度偏高,記憶體佔用量會很可觀;圖片建議先壓到實際顯示尺寸,必要時改用 AnimationDrawable 搭配 Bitmap 池或替換成 ProgressBar/屬性動畫實作。第二,start() 在 view 尚未 attach 到視窗時不會生效,若在 onCreate 中立刻顯示,記得用 post() 延後一輪。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved