天天看點

Android中動畫的幾種程式設計方式

 1、加載XML的方式

在衆多動畫的程式設計方式中,我最喜歡用這種方式,原因很簡單,直覺,友善修改……看看一個執行個體代碼:

建立一個aimn的檔案夾,在此檔案夾裡面建立一個XML動畫,裡面包含動畫的各種屬性。

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

<rotate

android:interpolator="@android:anim/accelerate_decelerate_interpolator"

android:fromDegrees="0"

android:toDegrees="360"

android:pivotX="0%"

android:pivotY="50%"

android:duration="1000"

/>

</set>

然後以代碼的方式加載,下面是一個加載XML動畫的部分代碼:

 Animation mAnimationRotate = AnimationUtils.loadAnimation(mContext, R.anim.rotate_animation);

                startAnimation(mAnimationRotate);

這種方式适用于自定義View中加載動畫。主要用來設定Frame 動畫,即順序播放事先做好的圖像,和電影類似。

2、這種方式也屬于加載XML的動畫

該動畫是放置在一個ImageView的控件裡播放的,是以必須先将此動畫設為ImageView的背景,然後再get出來,接着調用Animation的start()方法啟動動畫。大概代碼如下:

explo是一個ImageView的對象

explo.setBackgroundResource(R.anim.explosion);

anim = (AnimationDrawable) explo.getBackground();

anim.start();

3、寫死的方式

這種方式比較靈活,當然,這種方式也屬于自定義View中播放動畫,示例代碼如下:

mAnimationRotate = new RotateAnimation(0.0f, +360.0f,

Animation.RELATIVE_TO_SELF, 0.5f,

Animation.RELATIVE_TO_SELF, 0.5f);

mAnimationRotate.setDuration(1000);

this.startAnimation(mAnimationRotate);

2和3主要用來設定Frame動畫。