天天看點

android 使用View Animation實作動畫加載界面

轉載請注明出處:http://blog.csdn.net/zhaokaiqiang1992

今天給大家一個使用view animation實作動畫加載界面的實作。

    首先先看一下實作效果。

android 使用View Animation實作動畫加載界面

    下面是實作代碼

[java] view

plaincopy

android 使用View Animation實作動畫加載界面
android 使用View Animation實作動畫加載界面

package com.example.animationloading;  

import java.util.timer;  

import java.util.timertask;  

import android.annotation.suppresslint;  

import android.app.dialog;  

import android.content.context;  

import android.os.bundle;  

import android.os.handler;  

import android.os.message;  

import android.view.animation.animation;  

import android.view.animation.rotateanimation;  

import android.widget.imageview;  

/** 

 *  

* @classname:  com.example.animationloading.loadingdialog 

* @description: 動畫加載dialog 

* @author zhaokaiqiang 

* @date 2014-10-27 下午4:42:52 

 */  

public class loadingdialog extends dialog {  

    protected static final string tag = "loadingdialog";  

    // 動畫間隔  

    private static final int duration = 800;  

    // 前景圖檔  

    private imageview img_front;  

    // 定時器,用來不斷的播放動畫  

    private timer animationtimer;  

    // 旋轉動畫  

    private rotateanimation animationl2r;  

    @suppresslint("handlerleak")  

    private handler handler = new handler() {  

        public void handlemessage(message msg) {  

            img_front.setanimation(animationl2r);  

            animationl2r.start();  

        };  

    };  

    public loadingdialog(context context) {  

        super(context, r.style.dialog);  

    }  

    @override  

    protected void oncreate(bundle savedinstancestate) {  

        super.oncreate(savedinstancestate);  

        setcontentview(r.layout.dialog_loading);  

        img_front = (imageview) findviewbyid(r.id.img_front);  

        animationtimer = new timer();  

        // 從左到右的旋轉動畫,設定旋轉角度和旋轉中心  

        animationl2r = new rotateanimation(0f, -90f,  

                animation.relative_to_self, 0.5f, animation.relative_to_self,  

                0.5f);  

        // 設定動畫的運作時長  

        animationl2r.setduration(duration);  

        // 動畫運作結束之後,儲存結束之後的狀态  

        animationl2r.setfillafter(true);  

        // 設定重複的次數  

        animationl2r.setrepeatcount(1);  

        //設定重複模式為逆運動  

        animationl2r.setrepeatmode(animation.reverse);  

        // 執行間隔任務,開始間隔是0,每隔duration * 2執行一次  

        animationtimer.schedule(new timertask() {  

            @override  

            public void run() {  

                handler.sendemptymessage(1);  

            }  

        }, 0, duration * 2);  

    protected void onstop() {  

        super.onstop();  

        animationtimer.cancel();  

}  

    當然,除了這種直接使用代碼的寫死方式,哦們還可以使用xml的方式,和寫死基本類似,把需要的屬性在xml裡面定義好即可,下面的代碼實作。

[html] view

android 使用View Animation實作動畫加載界面
android 使用View Animation實作動畫加載界面

<?xml version="1.0" encoding="utf-8"?>  

<rotate xmlns:android="http://schemas.android.com/apk/res/android"  

    android:duration="800"  

    android:fillafter="true"  

    android:fromdegrees="0"  

    android:pivotx="50%"  

    android:pivoty="50%"  

    android:repeatcount="1"  

    android:repeatmode="reverse"  

    android:todegrees="-90" >  

</rotate>  

    如果使用這種方式,那麼,上面的代碼就要變成下面這種了。

android 使用View Animation實作動畫加載界面
android 使用View Animation實作動畫加載界面

import android.view.animation.animationutils;  

 * @classname: com.example.animationloading.loadingdialog 

 * @description: 動畫加載dialog 

 * @author zhaokaiqiang 

 * @date 2014-10-27 下午4:42:52 

    private animation animation;  

    private context context;  

            img_front.setanimation(animation);  

            animation.start();  

        this.context = context;  

        animation = animationutils.loadanimation(context,  

                r.anim.anim_load_dialog);  

      下面是dialog的樣式

android 使用View Animation實作動畫加載界面
android 使用View Animation實作動畫加載界面

<style name="dialog" parent="android:style/theme.dialog">  

       <!-- 背景顔色及透明程度 -->  

       <item name="android:windowbackground">@android:color/transparent</item>  

       <item name="android:windowframe">@null</item>  

       <item name="android:windownotitle">true</item>  

       <!-- 是否浮現在activity之上 -->  

       <item name="android:windowisfloating">true</item>  

       <item name="android:windowcontentoverlay">@null</item>  

   </style>  

    github的項目位址:https://github.com/zhaokaiqiang/loadingdialog

繼續閱讀