溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中利用View Animation實(shí)現(xiàn)一個(gè)動(dòng)畫加載界面

發(fā)布時(shí)間:2021-03-29 15:30:48 來(lái)源:億速云 閱讀:139 作者:Leah 欄目:移動(dòng)開發(fā)

本篇文章給大家分享的是有關(guān)怎么在Android中利用View Animation實(shí)現(xiàn)一個(gè)動(dòng)畫加載界面,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

實(shí)現(xiàn)代碼

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; 
 

public class LoadingDialog extends Dialog { 
 
 protected static final String TAG = "LoadingDialog"; 
 // 動(dòng)畫間隔 
 private static final int DURATION = 800; 
 // 前景圖片 
 private ImageView img_front; 
 // 定時(shí)器,用來(lái)不斷的播放動(dòng)畫 
 private Timer animationTimer; 
 // 旋轉(zhuǎn)動(dòng)畫 
 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(); 
 
  // 從左到右的旋轉(zhuǎn)動(dòng)畫,設(shè)置旋轉(zhuǎn)角度和旋轉(zhuǎn)中心 
  animationL2R = new RotateAnimation(0f, -90f, 
    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 
    0.5f); 
  // 設(shè)置動(dòng)畫的運(yùn)行時(shí)長(zhǎng) 
  animationL2R.setDuration(DURATION); 
  // 動(dòng)畫運(yùn)行結(jié)束之后,保存結(jié)束之后的狀態(tài) 
  animationL2R.setFillAfter(true); 
  // 設(shè)置重復(fù)的次數(shù) 
  animationL2R.setRepeatCount(1); 
  //設(shè)置重復(fù)模式為逆運(yùn)動(dòng) 
  animationL2R.setRepeatMode(Animation.REVERSE); 
  // 執(zhí)行間隔任務(wù),開始間隔是0,每隔DURATION * 2執(zhí)行一次 
  animationTimer.schedule(new TimerTask() { 
 
   @Override 
   public void run() { 
    handler.sendEmptyMessage(1); 
   } 
  }, 0, DURATION * 2); 
 
 } 
 
 @Override 
 protected void onStop() { 
  super.onStop(); 
  animationTimer.cancel(); 
 } 
 
}

當(dāng)然,除了這種直接使用代碼的硬編碼方式,哦們還可以使用xml的方式,和硬編碼基本類似,把需要的屬性在xml里面定義好即可,下面的代碼實(shí)現(xiàn)。

<?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>

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

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.AnimationUtils; 
import android.widget.ImageView; 
 
/** 
 * 
 * @ClassName: com.example.animationloading.LoadingDialog 
 * @Description: 動(dòng)畫加載Dialog 
 * @author zhaokaiqiang 
 * @date 2014-10-27 下午4:42:52 
 * 
 */ 
public class LoadingDialog extends Dialog { 
 
 protected static final String TAG = "LoadingDialog"; 
 // 動(dòng)畫間隔 
 private static final int DURATION = 800; 
 // 前景圖片 
 private ImageView img_front; 
 // 定時(shí)器,用來(lái)不斷的播放動(dòng)畫 
 private Timer animationTimer; 
 
 private Animation animation; 
 
 private Context context; 
 
 @SuppressLint("HandlerLeak") 
 private Handler handler = new Handler() { 
 
  public void handleMessage(Message msg) { 
   img_front.setAnimation(animation); 
   animation.start(); 
  }; 
 
 }; 
 
 public LoadingDialog(Context context) { 
  super(context, R.style.dialog); 
  this.context = context; 
 } 
 
 @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(); 
 
  animation = AnimationUtils.loadAnimation(context, 
    R.anim.anim_load_dialog); 
   
  // 執(zhí)行間隔任務(wù),開始間隔是0,每隔DURATION * 2執(zhí)行一次 
  animationTimer.schedule(new TimerTask() { 
 
   @Override 
   public void run() { 
    handler.sendEmptyMessage(1); 
   } 
  }, 0, DURATION * 2); 
 
 } 
 
 @Override 
 protected void onStop() { 
  super.onStop(); 
  animationTimer.cancel(); 
 } 
 
}

下面是dialog的樣式

<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> 
  <!-- 是否浮現(xiàn)在activity之上 --> 
  <item name="android:windowIsFloating">true</item> 
  <item name="android:windowContentOverlay">@null</item> 
 </style>

以上就是怎么在Android中利用View Animation實(shí)現(xiàn)一個(gè)動(dòng)畫加載界面,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI