============================首先看看官網上關于Frame animation的介紹================================
地址:
http://developer.android.com/guide/topics/resources/animation-resource.html#Frame
Frame animation
An animation defined in XML that shows a sequence of images in order (like a film).
res/drawable/
filename
.xml
The filename will be used as the resource ID.
AnimationDrawable
.
R.drawable.
filename
In XML:
@[
package
:]drawable.
filename
<?
xml version
=
"1.0"
encoding
=
"utf-8"
?>
<
animation-list
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:oneshot
=
["true"
|
"false"
]
>
<
item
android:drawable
=
"@[package:]drawable/
drawable_resource_name
"
android:duration
=
"
integer
"
/>
</animation-list>
<animation-list>
<item>
elements.
attributes:
android:oneshot
<item>
<animation-list>
element.
attributes:
android:drawable
android:duration
step1:新建一個Android項目FrameAnimationDemo
step2:準備好該應用使用的圖片,用來做Frame Animation的,并將動畫放入drawable目錄下
step3:新建一個用來描述Frame動畫的xml文件,res/anim/ frame.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/p1" android:duration="500" />
<item android:drawable="@drawable/p2" android:duration="500" />
<item android:drawable="@drawable/p3" android:duration="500" />
<item android:drawable="@drawable/p4" android:duration="500" />
<item android:drawable="@drawable/p5" android:duration="500" />
<item android:drawable="@drawable/p6" android:duration="500" />
</animation-list>
<!-- android:oneshot指示是否只運行一次,設置為false則意味著循環播放
<item>元素代表一幀動畫,
android:drawable指定此幀動畫所對應的圖片資源,
android:druation代表此幀持續的時間,整數,單位為毫秒。
-->
step4:該應用的布局文件 res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Frame動畫圖片 -->
<ImageView android:id="@+id/ImgDance" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1" />
<!-- android:layout_weight="1" 不設置該屬性,下面的兩個按鈕會被覆蓋不顯示出來 -->
<!-- 動畫控制按鈕 -->
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="開始跳舞"
android:onClick="runFrame" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="結束跳舞"
android:onClick="stopFrame" />
</LinearLayout>
step5:該應用的主文件,FrameActivity.java
package cn.oyp.frame;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class FrameActivity extends Activity {
// 顯示動畫的組件
private ImageView imgDance;
// Frame動畫
private AnimationDrawable animDance;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 實例化組件
imgDance = (ImageView) super.findViewById(R.id.ImgDance);
}
/**
* 如果在onCreate()中調用AnimationDrawable的start()方法,則它只停留在第一幀,并沒有出現我們期望的動畫,
* 這是因為窗口Window對象還沒有完全初始化,AnimationDrawable不能完全追加到窗口Window對象中。
* 而onWindowFocusChanged是在onCreate之后被調用的,當Activity展示給用戶時,onWindowFocusChanged方法就會被調用,
* 所以在這兒調用AnimationDrawable的start()方法可以實現動畫效果。
*/
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// 將動畫資源文件res/anim/frame.xml設置為ImageView的背景
imgDance.setBackgroundResource(R.anim.frame);
// 獲取ImageView背景,此時已被編譯成AnimationDrawable
animDance = (AnimationDrawable) imgDance.getBackground();
animDance.start();
}
/**
* 按鈕:停止‘跳舞’動畫
*/
public void stopFrame(View view) {
animDance = (AnimationDrawable) imgDance.getBackground();
if (animDance.isRunning()) { // 如果正在運行,就停止
animDance.stop();
}
}
/**
* 按鈕:開始‘跳舞’動畫
*/
public void runFrame(View view) {
// 完全編碼實現的動畫效果
animDance = new AnimationDrawable();
for (int i = 1; i <= 6; i++) {
// 根據資源名稱和目錄獲取R.java中對應的資源ID
int id = getResources().getIdentifier("p" + i, "drawable",
getPackageName());
// 根據資源ID獲取到Drawable對象
Drawable drawable = getResources().getDrawable(id);
// 將此幀添加到AnimationDrawable中
animDance.addFrame(drawable, 500);
}
animDance.setOneShot(false); // 設置為loop
imgDance.setBackgroundDrawable(animDance); // 將動畫設置為ImageView背景
animDance.start(); // 開始動畫
}
}
效果如下:
=================================================================================================
作者:歐陽鵬 歡迎轉載,與人分享是進步的源泉!
轉載請保留原文地址 : http://blog.csdn.net/ouyang_peng
==================================================================================================
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

