1. 程式人生 > >實現網路請求時特定的載入動畫(幀動畫)

實現網路請求時特定的載入動畫(幀動畫)

第一篇部落格,有點小小的激動。有些做過的東西當時可能記得很清楚,但是時間久了就會慢慢忘記,所以才決定開始記錄一些。先寫個最簡單的載入動畫。

首先來看一下效果圖,就如中間哪裡書翻頁的效果。此動畫是用幀動畫來實現的。

幀動畫是順序播放一組預先定義好的圖片,類似與電影播放。系統提供了AnimationDrawable來使用幀動畫。幀動畫的使用也比較簡單接下來看一下它的使用吧。


1.首先通過XML來定義一個AnimationDrawable。在你的drawable檔案中新建一個名字為progress_alert_layout.xml的xml檔案 。如下所示,動畫的圖片就需要你的UI設計師來提供了。注意避免使用過多尺寸較大的圖片。

<?xml version="1.0" encoding="utf-8"?><!--
03.    根標籤為animation-list,其中oneshot代表著是否只展示一遍,設定為false會不停的迴圈播放動畫
04.    根標籤下,通過item標籤對動畫中的每一個圖片進行宣告
05.    android:duration 表示展示所用的該圖片的時間長度
06. -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item
        android:drawable="@drawable/new_progress1"
        android:duration="70" />
    <item
        android:drawable="@drawable/new_progress2"
        android:duration="70" />
    <item
        android:drawable="@drawable/new_progress3"
        android:duration="70" />
    <item
        android:drawable="@drawable/new_progress4"
        android:duration="70" />
    <item
        android:drawable="@drawable/new_progress5"
        android:duration="70" />
    <item
        android:drawable="@drawable/new_progress6"
        android:duration="70" />
    <item
        android:drawable="@drawable/new_progress7"
        android:duration="70" />
    <item
        android:drawable="@drawable/new_progress8"
        android:duration="70" />
    <item
        android:drawable="@drawable/new_progress9"
        android:duration="70" />
    <item
        android:drawable="@drawable/new_progress10"
        android:duration="70" />
    <item
        android:drawable="@drawable/new_progress11"
        android:duration="70" />
    <item
        android:drawable="@drawable/new_progress12"
        android:duration="70" />

    <item
        android:drawable="@drawable/new_progress13"
        android:duration="70" />

    <item
        android:drawable="@drawable/new_progress14"
        android:duration="70" />
    <item
        android:drawable="@drawable/new_progress15"
        android:duration="70" />

</animation-list>

2.這個動畫我是用Dialog顯示出來,為了方便使用我就寫了一個繼承Dialog的載入動畫類。

public class ProgressAnimAlert extends Dialog {

    private String text;
    private AnimationDrawable animationDrawable;

    public ProgressAnimAlert(Context context) {
        super(context, R.style.Translucent_NoTitle);
    }

    public ProgressAnimAlert(Context context, String text) {
        super(context, R.style.Translucent_NoTitle);  //這裡設定了Dialog的Style讓其背景為透明的,並且沒有title,具體程式碼就不貼了
        this.text = text;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progress_alert_layout);
        //按空白處不能取消動畫
        setCanceledOnTouchOutside(false);
        if (text!= null){
            TextView textView = (TextView) findViewById(R.id.progress_text);
            textView.setText(text);
        }
        ImageView progressImageView = (ImageView) findViewById(R.id.progress_image); //在ImageView上使用幀動畫
        animationDrawable = (AnimationDrawable) getContext().getResources().getDrawable(R.drawable.progress_alert_anim); //幀動畫的初始化
        progressImageView.setImageDrawable(animationDrawable); //將動畫設定在ImageView上
} /** * 開始幀動畫 */ @Override protected void onStart() { animationDrawable.start(); super.onStart(); } /** * 停止幀動畫 */ @Override protected void onStop() { animationDrawable.stop(); super.onStop(); }

3.Dialog的progress_alert_layout佈局檔案如下,這裡可以根據自己的需求定義自己需要的佈局樣式

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ffffff">

    <RelativeLayout
        android:id="@+id/progress_bg"
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:layout_centerInParent="true"
        android:background="@drawable/progress_alert_circle">

        <ImageView
            android:id="@+id/progress_image"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:paddingTop="10dp"
            android:scaleType="fitCenter" />

        <TextView
            android:id="@+id/progress_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/progress_image"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="-10dp"
            android:text="努力載入中..."
            android:textColor="#999999"
            android:textSize="12sp" />
    </RelativeLayout>
</RelativeLayout>

4.最後就是在程式碼中使用就可以了

ProgressAnimAlert progressAnimAlert = new ProgressAnimAlert(getView(), "正在載入中...");

通過呼叫Dialog的show()和dismiss()來控制載入動畫的顯示和消失。



相關推薦

實現網路請求特定載入動畫動畫

第一篇部落格,有點小小的激動。有些做過的東西當時可能記得很清楚,但是時間久了就會慢慢忘記,所以才決定開始記錄一些。先寫個最簡單的載入動畫。首先來看一下效果圖,就如中間哪裡書翻頁的效果。此動畫是用幀動畫來實現的。幀動畫是順序播放一組預先定義好的圖片,類似與電影播放。系統提供了A

【SpringBoot 基礎系列】實現一個自定義配置載入應用篇

![](https://spring.hhui.top/spring-blog/imgs/200507/logo.jpg) > [【SpringBoot 基礎系列】實現一個自定義配置載入器(應用篇)](https://mp.weixin.qq.com/s?__biz=MzU3MTAzNTMzMQ==&

Android - 動畫動畫,補間動畫,屬性動畫,以及插值器

一: 動畫的分類 幀動畫 補間動畫 屬性動畫 二:解析 1. 幀動畫 (1)定義 這些圖片是將一些列的drawable組合在一起,進行連續的播放, 類似於以前電影源用膠捲進行動畫播放 (2)有圖有真相 (3)準備圖片 看著是不是還行,哈哈,

Android中的動畫動畫、補間動畫、屬性動畫

總的來說,安卓動畫可以分為兩類,最初的傳統動畫和Android3.0之後的屬性動畫。 傳統動畫包括:幀動畫( Frame Animation)和補間動畫(Tweened Animation)。 下面來具體說一下各種動畫的使用及特點: 幀動畫:是最容易實

使用Animation list實現網路請求過程中的載入動畫dialog

效果圖 實現過程主要是藉助於AnimationList實現幀動畫 animation_list.xml檔案 <?xml version="1.0" encoding="utf-8"?

Android--menu和OkHttp框架未封裝,結合Executors執行緒池實現網路請求的案例

涉及到的 知識點: 1.安卓UI元件menu 2.OkHttp框架 3.Executors(執行緒池) OkHttp結構簡介 案例程式碼 import android.os.Bundle; import android.suppo

SpringMVC-實現PUT請求上傳文件

Enctype multi think pic 不可 form OS his win 因為在圖片上傳的時候使用的是二進制的方式上傳,所以使用隱藏域進行方法轉換方式失效,轉方法: https://www.cnblogs.com/morethink/p/6378015.html

Android RxJava 實戰系列:優雅實現 網路請求巢狀回撥

轉自-----http://blog.csdn.net/carson_ho/article/details/78315696,請為大神打call 前言 Rxjava,由於其基於事件流的鏈式呼叫、邏輯簡潔 & 使用簡單的特點,深受各大 Android

Android RxJava 實戰講解:優雅實現 網路請求輪詢

轉自-----http://blog.csdn.net/carson_ho/article/details/78256466 前言 Rxjava,由於其基於事件流的鏈式呼叫、邏輯簡潔 & 使用簡單的特點,深受各大 Android開發者的歡迎。

python3 urllib3發出網路請求SSL: CERTIFICATE_VERIFY_FAILED

python3使用urllib3請求時出現SSL: CERTIFICATE_VERIFY_FAILED錯誤: 安裝安全模式:pip install requests[security] 此時如果出現command 'x86_64-linux-gnu-gcc' failed with ex

Qt開場動畫gif效果實現

    Qt自己提供了一個開場動畫的類QSplashScreen,可以實現簡單的圖片開場的效果,但是是靜態的圖片。 Qt播放gif格式圖片是利用的QMovie實現的。因此利用QMoviee和QTimer,每隔一段時間將QSplashScreen重繪一次,來實現gi

Swift使用Alamofire實現網路請求

  Alamofire是一個用Swift編寫的HTTP網路庫,由此前熱門開源專案AFNetworking的的作者mattt開發,可非常簡單地用於非同步網路通訊。 要獲取最新版本的 Alamofire,前往https://github.com/Alamofire/Alamofire然後單擊網頁右邊

OKHTTP-RecyclerView實現網路請求資料

build.gradle中匯入需要依賴 implementation ‘com.google.code.gson:gson:2.8.5’ implementation ‘com.android.support:design:27.1.1’ compile ‘cn.yipianfengye

[轉]自定義Drawable實現靈動的紅鯉魚動畫上篇

此篇中的小魚動畫是模仿國外一個大牛做的flash動畫,第一眼就愛上它了,簡約靈動又不失美學,於是抽空試著嘗試了一下,如下是我用Android實現的效果圖:   小魚兒

jQuery.ajax設定請求實現post請求傳送資料的格式Form Data和Request Payload

 Request Payload 請求頭部的 Content-Type: application/json,請求正文是一個 json 格式的字串 Form Data 請求頭部的 Content-Typ

Android網路請求,漢字轉化為url編碼

 在開始討論編碼解碼之前,首先來明確一下問題。 什麼是application/x-www-form-urlencoded字串? 答:它是一種編碼型別。當URL地址裡包含非西歐字元的字串時,系統會將這些字元轉換成application/x-www-form-urlenc

解析Node.js通過axios實現網路請求

本次給大家分享一篇node.js通過axios實現網路請求的方法,寫的十分的全面細緻,具有一定的參考價值,對此有需要的朋友可以參考學習下。如有不足之處,歡迎批評指正。 1、使用Npm 下載axios npm install --save axios var update_u

iOS NSURLSession 實現網路請求-檔案下載-上傳-後臺下載

 *  會話NSURLSession     NSURLConnection通過全域性狀態來管理cookies, 認證資訊等公共資源, 如果兩個連線需要使用不同的資源配置情況時就無法解決,     這個問題在NSURLSession可以解決, NSURLSession同

Android:RxJava 結合 Retrofit 優雅實現 網路請求輪詢

前言 Rxjava,由於其基於事件流的鏈式呼叫、邏輯簡潔 & 使用簡單的特點,深受各大 Android開發者的歡迎。 RxJava如此受歡迎的原因,在於其提供了豐富 & 功能強大的操作符,幾乎能完成所有的功能需求 今天,我將為

[Swift通天遁地]四、網路和執行緒-(4)使用Alamofire實現網路請求

本文將演示如何使用第三方庫實現網路請求服務。 首先確保在專案中已經安裝了所需的第三方庫。 點選【Podfile】,檢視安裝配置檔案。 1 source 'https://github.com/CocoaPods/Specs.git' 2 platform :ios, '12.0' 3 use_