Android之AnimationDrawable簡單模擬動態(tài)圖
Drawable animation可以加載Drawable資源實(shí)現(xiàn)幀動畫。AnimationDrawable是實(shí)現(xiàn)Drawable animations的基本類。
這里用AnimationDrawable 簡單模擬動態(tài)圖的實(shí)現(xiàn)。
fragment_main 布局文件 ---- 只需要放一個 ImageView即可
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yztc.frameanimation.MainActivity" >
<ImageView
android:id="@+id/iv_frame"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/girl_and_boy" />
</RelativeLayout>
girl_and_boy 布局文件 ---- 實(shí)現(xiàn)動畫
推薦用XML文件的方法實(shí)現(xiàn)Drawable動畫,不推薦在代碼中實(shí)現(xiàn)。這種XML文件存放在工程中res/drawable/目錄下。XML文件的指令(即屬性)為動畫播放的順序和時間間隔。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- onshot 屬性表示動畫只執(zhí)行一次 -->
<!-- duration 表示持續(xù)時間 -->
<item
android:drawable="@drawable/girl_1"
android:duration="200">
</item>
<item
android:drawable="@drawable/girl_2"
android:duration="200">
</item>
<item
android:drawable="@drawable/girl_3"
android:duration="200">
</item>
<item
android:drawable="@drawable/girl_4"
android:duration="200">
</item>
<item
android:drawable="@drawable/girl_5"
android:duration="300">
</item>
<item
android:drawable="@drawable/girl_6"
android:duration="400">
</item>
<item
android:drawable="@drawable/girl_7"
android:duration="500">
</item>
<item
android:drawable="@drawable/girl_8"
android:duration="400">
</item>
<item
android:drawable="@drawable/girl_9"
android:duration="300">
</item>
<item
android:drawable="@drawable/girl_10"
android:duration="200">
</item>
<item
android:drawable="@drawable/girl_11"
android:duration="200">
</item>
</animation-list>
MainActivity
package com.dragon.android.initgif;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
ImageView ivFrame = (ImageView) findViewById(R.id.iv_frame);
// 得到一個動畫圖片
AnimationDrawable background = (AnimationDrawable) ivFrame
.getBackground();
// 開始播放
background.start();
// 停止方法.
// background.stop();
}
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 本地廣播和強(qiáng)制下線功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 本地廣播和強(qiáng)制下線功能的實(shí)現(xiàn)代碼,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
關(guān)于gradle你應(yīng)該知道的一些小事
這篇文章主要給大家介紹了關(guān)于gradle你應(yīng)該知道的一些小事,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用gradle具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
Android開發(fā)中的Surface庫及用其制作播放器UI的例子
這篇文章主要介紹了Android開發(fā)中的Surface庫及用其制作播放器界面的例子,利用SurfaceView和SurfaceHolder可以高效地繪制和控制圖形界面,需要的朋友可以參考下2016-04-04
Android實(shí)現(xiàn)下載zip壓縮文件并解壓的方法(附源碼)
這篇文章主要給大家介紹了利用Android實(shí)現(xiàn)下載zip壓縮文件并解壓的方法,文中給出了示例代碼并提供了源碼下載,需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02
Android編程實(shí)現(xiàn)仿易信精美彈出框效果【附demo源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)仿易信精美彈出框效果,涉及Android窗口及動畫操作相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-01-01
關(guān)于Android Fragment對回退棧的詳細(xì)理解
這篇文章主要介紹了Android Fragment的回退棧示例詳細(xì)介紹的相關(guān)資料,在Android中Fragment回退棧是由Activity管理的,每個Activity都有自己的回退棧,其中保存了已經(jīng)停止(處于后臺)的Fragment實(shí)例,需要的朋友可以參考下2016-12-12

