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

  您的位置:華清遠見教育科技集團 >> 新聞動態(tài) >> Android資料 >> Android幀動畫實例詳解  
 
Android幀動畫實例詳解
分享到:

幀動畫,是按順序播放一組圖像。幀動畫有兩個比較重要的屬性需要注意,一個是android:oneshot屬性,用于設(shè)置播放模式,是單次播放還是循環(huán)播放;一個是android:duration屬性,用于設(shè)置每幀的持續(xù)時間,單位為毫秒。幀動畫的資源文件位于res/anim文件夾下。

為了連續(xù)顯示一組圖像,產(chǎn)生動畫效果,Android需要利用XML資源文件和AnimationDrawable來協(xié)同工作,下面是Android的一個實現(xiàn)。

XML文件lock_anim.xml的實現(xiàn)如下:

代碼:幀動畫的處理

……
    < animation-list
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:oneshot="false">
            < item android:drawable="@drawable/lock_anim_00" android:duration="400" />
            < item android:drawable="@drawable/lock_anim_02" android:duration="400" />
            < item android:drawable="@drawable/lock_anim_04" android:duration="400" />
            < item android:drawable="@drawable/lock_anim_06" android:duration="400" />
            < item android:drawable="@drawable/lock_anim_08" android:duration="400" />
            < item android:drawable="@drawable/lock_anim_10" android:duration="400" />
            < item android:drawable="@drawable/lock_anim_12" android:duration="400" />
            < item android:drawable="@drawable/lock_anim_14" android:duration="400" />
    < /animation-list>
    …

Android代碼的實現(xiàn)如下:

private View mImageView;
    private AnimationDrawable mAnimation;
    mImageView=(ImageView) findViewById(R.id.lock_anim);    //獲取視圖句柄
    mImageView.setBackgroundResource(R.drawable.lock_anim);    //設(shè)置背景資源
    mImageView.setOnClickListener(this);    //設(shè)置監(jiān)聽器
    mAnimation=(AnimationDrawable) mImageView.getBackground();    //獲取背景
    mAnimation .start()    //播放背景

在默認情況下,AnimationDrawable對背景的播放是循環(huán)模式。如果需要改為單次模式,需要設(shè)置android:oneshot屬性為“true”。

需要注意的是AnimationDrawable的start()方法不能在Activity的onCreate()方法中調(diào)用,這是因為此時圖像資源尚未完全加載。如果希望能在Activity啟動后立即開始動畫,可以在Activity的onWindowFocusChanged()方法中執(zhí)行start()方法調(diào)用。

下面是一個具體幀動畫例子的實現(xiàn):

代碼:幀動畫的實例

package com.miaozl.test;
    import android.app.Activity;
    import android.graphics.drawable.AnimationDrawable;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.widget.ImageView;
    public class AnimationActivity extends Activity {
        AnimationDrawable photoAnimation;
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.animation);
            ImageView photoImage = (ImageView) findViewById(R.id.anim);
            photoImage.setBackgroundResource(R.anim.animation);
            photoAnimation = (AnimationDrawable) photoImage.getBackground();
        }
        public void onWindowFocusChanged (boolean hasFocus){
            if(hasFocus){ //判斷窗口是否已經(jīng)完全顯示
                photoAnimation.start(); //開始動畫播放
            }
        }
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                photoAnimation.stop(); //停止動畫播放
                return true;
            }
        return super.onTouchEvent(event);
        }
    }

下面是資源文件res/anim-hdpi/animation.xml的實現(xiàn):

< animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">    //設(shè)置為循環(huán)播放
        < item android:drawable="@drawable/photo1" android:duration="200"/>
        < item android:drawable="@drawable/photo2" android:duration="200" />
        < item android:drawable="@drawable/photo3" android:duration="200" />
        < item android:drawable="@drawable/photo4" android:duration="200" />
        < item android:drawable="@drawable/photo5" android:duration="200" />
        < item android:drawable="@drawable/photo6" android:duration="200" />
    < /animation-list>

幀動畫是應(yīng)用層常用的動畫效果,需要說明的是,在Android中,尚不支持gif動畫,在播放gif動畫時,僅能顯示首幀。

 更多相關(guān)文章

·Android 控件動畫效果的實現(xiàn)
·Android中的四種補間動畫
·Android開發(fā)中的人臉檢測技術(shù)
·Android中如何實現(xiàn)圖像瀏覽
·Android圖像旋轉(zhuǎn)源碼分享