Android開發AnimationDrawable控制逐幀播放動畫

來源:互聯網
上載者:User

當我們點擊按鈕時,該圖片會不停的旋轉,當再次點擊按鈕時,會停止在當前的狀態。

activity代碼:

 

 代碼如下 複製代碼

package cn.com.chenzheng_java.animation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
/**
* @description android中的逐幀動畫.
* 逐幀動畫的原理很簡單,跟電影的播放一樣,一張張類似的圖片不停的切換,當切換速度達到一定值時,
* 我們的視覺就會出現殘影,殘影的出現保證了視覺上變化的連續性,這時候圖片的切換看在我們眼中就跟真實的一樣了。
* 想使用逐幀動畫:
* 第一步:需要在res/drawable檔案夾下建立一個xml檔案,該檔案詳細定義了動畫播放時所用的圖片、切換每張圖片
*        所用的時間、是否為連續播放等等。(有些文章說,在res/anim檔案夾下放置該檔案,事實證明,會出錯哦)
* 第二步:在代碼中,將該動畫布局檔案,賦值給特定的圖片展示控制項,如本例子中的ImageView。
* 第三步:通過imageView.getBackGround()擷取相應的AnimationDrawable對象,然後通過該對象的方法進行控制動畫
* @author chenzheng_java
*
*/
public class Animation1Activity extends Activity {
ImageView imageView ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation1);

imageView = (ImageView) findViewById(R.id.imageView_animation1);
imageView.setBackgroundResource(R.drawable.animation1_drawable);

}

public void myClickHandler(View targetButton){
// 擷取AnimationDrawable對象
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();

// 動畫是否正在運行
if(animationDrawable.isRunning()){
//停止動畫播放
animationDrawable.stop();
}
else{
//開始或者繼續動畫播放
animationDrawable.start();
}

}
}

 

animation1.xml檔案:

 代碼如下 複製代碼


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<Button  android:id="@+id/button_animation1" android:text="動畫開始"
android:layout_gravity="center_horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="myClickHandler"></Button>
<ImageView android:id="@+id/imageView_animation1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"></ImageView>
</LinearLayout>

存放動畫檔案的xml檔案:

 

 代碼如下 複製代碼
<?xml version="1.0" encoding="utf-8"?>
<!--
根標籤為animation-list,其中oneshot代表著是否只展示一遍,設定為false會不停的迴圈播放動畫
根標籤下,通過item標籤對動畫中的每一個圖片進行聲明
android:duration 表示展示所用的該圖片的時間長度
-->
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"
>
<item android:drawable="@drawable/a1" android:duration="50"></item>
<item android:drawable="@drawable/a2" android:duration="50"></item>
<item android:drawable="@drawable/a3" android:duration="50"></item>
<item android:drawable="@drawable/a4" android:duration="50"></item>
<item android:drawable="@drawable/a5" android:duration="50"></item>
<item android:drawable="@drawable/a6" android:duration="50"></item>
</animation-list>

除此之外:在AnimationDrawable中,我們還可以看到如下的幾個重要方法:

setOneShot(boolean flag) 和在設定檔中進行配置一樣,可以設定動畫是否播放一次,false為連續播放;

addFrame (Drawable frame, int duration) 動態添加一個圖片進入該動畫中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.