Android 利用 loadAnimation 載入 anim XML 來完成混合型動畫

之前介紹的 Android 動畫內容,

是由 Java 程式控制,

如, 混合型動畫 : http://tomkuo139.blogspot.com/2009/11/android-animationset.html.

以下介紹利用 XML 也可完成動畫內容的設定 :

檔案結構 :


main.xml :
 檔案內容
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:id="@+id/widget10"
android:layout_width="300px"
android:layout_height="400px"
android:layout_x="5px"
android:layout_y="5px"
android:src="@drawable/a2"
/>
<ImageView
android:id="@+id/widget28"
android:layout_width="75px"
android:layout_height="70px"
android:layout_x="24px"
android:layout_y="38px"
android:src="@drawable/icon"
/>
</AbsoluteLayout>

tom_anim.xml :
 檔案內容
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1.0"
android:toAlpha="0.0"
android:repeatCount="-1"
android:duration="2000" />
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="30"
android:pivotY="30"
android:repeatCount="-1"
android:duration="2000" />
</set>

Java 部分程式碼 :
 程式碼
public class helloWorld extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// 要使用 findViewById, 一定要使用 layout / *.xml 做為使用者介面
setContentView( R.layout.main );

// 取得 UI 介面中的 View 物件
// 取得 View 物件後,再透過轉換成實際的物件

ImageView iv = (ImageView)this.findViewById(R.id.widget28);

// 取得動畫設定檔
Animation am = AnimationUtils.loadAnimation(this, R.anim.tom_anim);

// 圖片配置動畫
iv.setAnimation(am);

// 動畫開始
am.startNow();
}
}

範例結果 :


Related Posts Plugin for WordPress, Blogger...