97干视频,99国产精品懂色,亚洲精品99久久久久中文字幕,伊人五月丁香综合AⅤ,国产精品成人免费999

  您的位置:華清遠見教育科技集團 >> 新聞動態(tài) >> Android資料 >> Android開發(fā)之Frame動畫實現(xiàn)方法  
 
Android開發(fā)之Frame動畫實現(xiàn)方法
分享到:

Frame動畫是順序播放圖片來產(chǎn)生動畫效果的,類似電影。例如,要實現(xiàn)一個人物走動的動畫,可以通過三張圖片來實現(xiàn),第一張兩腳都著地,第二張左腳著地,第三張右腳著地。然后,按順序播放就實現(xiàn)任務(wù)行走的動畫效果了。

Frame動畫是通過AnimationDrawable類實現(xiàn)的,該類中的兩個重要方法是start()和stop(),分別用來開始和停止動畫。動畫一般通過XML配置文件來進行配置,在工程的res/anim/目錄下創(chuàng)建一個XML配置文件,該配置文件中有一個<animation-list>根元素和若干個<item>子元素。

下面通過一個實例來演示Frame動畫的實現(xiàn)。該實例通過順序播放6張圖片來實現(xiàn)一個人跳起來的動畫效果。6張靜態(tài)圖片如圖1所示。


圖1 Frame動畫靜態(tài)圖片

實例步驟說明如下:
    (1)創(chuàng)建一個工程“Graphic_Animation2”。
    (2)在該工程的res/drawable/目錄下添加上述6張資源文件。
    (3)在該工程的res/anim/目錄下創(chuàng)建一個XML動畫文件,代碼如代碼清單1所示。

代碼清單1 jump.xml


<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="true">
        <item android:drawable="@drawable/p01" android:duration="500"/>
        <item android:drawable="@drawable/p02" android:duration="500"/>
        <item android:drawable="@drawable/p03" android:duration="500"/>
        <item android:drawable="@drawable/p04" android:duration="500"/>
        <item android:drawable="@drawable/p05" android:duration="500"/>
        <item android:drawable="@drawable/p06" android:duration="500"/>
    </animation-list>

(4)在工程的res/layout/目錄下創(chuàng)建一個main.xml布局文件,在該布局文件中添加兩個Button:一個用來開始動畫,另一個用來停止動畫。添加一個ImageView顯示動畫。設(shè)置ImageView的背景色android:background="@anim/dance"。代碼如代碼清單2所示。

代碼清單2 jump.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"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@anim/jump"
            android:id="@+id/ImageView01"></ImageView>

        <Button
            android:layout_height="wrap_content"
            android:text="Start..."
            android:layout_width="wrap_content"
            android:id="@+id/Button01"></Button>
        <Button
            android:layout_height="wrap_content"
            android:text="Stop..."
            android:layout_width="wrap_content"
            android:id="@+id/Button02"></Button>
    </LinearLayout>

(5)在MainActivity類中聲明Button、ImageView和AnimationDrawable實例,在onCreate()方法中將其實例化。獲得ImageView視圖的背景色,并轉(zhuǎn)化為AnimationDrawable對象。為按鈕添加單擊事件監(jiān)聽器,在事件方法中分別開始動畫和結(jié)束動畫。

代碼清單3 MainActivity.java


package cn.com.farsight.graphic_Animation;

    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 MainActivity extends Activity {
        //聲明使用到的Button視圖組件
        private Button b1,b2;
        //聲明使用到的ImageView組件
        private ImageView myImage;
        //聲明AnimationDrawable
        private AnimationDrawable jumpAnimation;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            //實例化視圖組件
            myImage=(ImageView) findViewById(R.id.ImageView01);
            b1 = (Button) findViewById(R.id.Button01);
            b2 = (Button) findViewById(R.id.Button02);

            //獲得背景色,并轉(zhuǎn)換為AnimationDrawable對象
            jumpAnimation = (AnimationDrawable) myImage.getBackground();

            //為按鈕添加監(jiān)聽事件
            b1.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //開始動畫
                    jumpAnimation.start();
                }
            });
            b2.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //停止動畫
                    jumpAnimation.stop();
                }
            });
        }
    }

程序運行結(jié)果如圖2所示。

 
圖2 Frame動畫效果

 更多相關(guān)文章

·Android開發(fā)之Tween動畫實現(xiàn)方法
·Android 屬性動畫開發(fā)源碼
·Android幀動畫實例詳解
·Android 控件動畫效果的實現(xiàn)
·Android中的四種補間動畫