Android動畫Animation之Frame實作類別似電影的動畫效果

來源:互聯網
上載者:User

Frame 動畫,即順序播放事先做好的映像,跟電影類似。

接下來的案例是點擊按鈕實現播放動畫,點擊停止實現停止動畫播放!

1、:

      

2、main.xml檔案很簡單:

<Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:id="@+id/button"    android:text="開始"/><Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:id="@+id/stop"    android:text="停止"/>    <ImageView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical"        android:id="@+id/image"/>

3、然後在drawable檔案夾下定義一個frame.xml,為的是把所有的圖片都定義在該xml檔案中,它是一個animation列表,存放所有使用到的圖片!!

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" ><item android:drawable="@drawable/girl_1" android:duration="100" /><item android:drawable="@drawable/girl_2" android:duration="100" /><item android:drawable="@drawable/girl_3" android:duration="100" /><item android:drawable="@drawable/girl_4" android:duration="100" /><item android:drawable="@drawable/girl_5" android:duration="200" /><item android:drawable="@drawable/girl_6" android:duration="500" /><item android:drawable="@drawable/girl_7" android:duration="500" /><item android:drawable="@drawable/girl_8" android:duration="200" /><item android:drawable="@drawable/girl_9" android:duration="100" /><item android:drawable="@drawable/girl_10" android:duration="100" /><item android:drawable="@drawable/girl_11" android:duration="100" /></animation-list>

每個item裡面有個duration屬性,該屬性為了使該圖片持續多長時間!!

4、最後是java代碼:

package cn.csdn.anim;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class FrameActivity extends Activity implements OnClickListener {private Button button, stop;private ImageView image;private AnimationDrawable attackAnimation;//定義動畫的對象@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init() {image = (ImageView) findViewById(R.id.image);//顯示動畫的imageviewbutton = (Button) findViewById(R.id.button);//開始動畫stop = (Button) findViewById(R.id.stop);//停止動畫button.setOnClickListener(this);stop.setOnClickListener(this);image.setBackgroundResource(R.drawable.frame);//設定顯示動畫的image的背景資源參數是int,就是你自己寫的frame.xml,裡面是所有相關的圖片attackAnimation = (AnimationDrawable) image.getBackground();}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button:attackAnimation.start();//開始動畫break;case R.id.stop:attackAnimation.stop();//停止動畫break;}}}

AnimationDrawable類是:

An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background.

The simplest way to create a frame-by-frame animation is to define the animation in an XML file, placed in the res/drawable/ folder, and set it as the background to a View object. Then, call start() to
run the animation.

An AnimationDrawable defined in XML consists of a single <animation-list> element, and a series of nested <item> tags. Each item defines a frame
of the animation.

一個對象用於建立frame-by-frame動畫,其定義是由一系列的可畫的對象,可以使用作為一個視圖對象的背景情況。

最簡單的方法來建立一個frame-by-frame動畫定義動畫在XML檔案中,放置在雜誌/可畫/檔案夾,並將它設定為背景,一個視圖對象。然後,叫開始()啟動並執行動畫。

定義一個AnimationDrawable在XML組成一個單一的< animation-list >元素,一系列的嵌套的<項目>標籤。 每一項定義了一個幀動畫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.