Frame動畫:
1、找到一組圖片c01.jpg,c02.jpg,c03.jpg,c04.jpg,c05.jpg,copy到res/drawable目錄下;
2、在res/drawable目錄下新建XML文件:frame_anim.xml
[html]
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@drawable/c01" android:duration="500"/>
<item android:drawable="@drawable/c02" android:duration="500"/>
<item android:drawable="@drawable/c03" android:duration="500"/>
<item android:drawable="@drawable/c04" android:duration="500"/>
<item android:drawable="@drawable/c05" android:duration="500"/>
</animation-list>
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@drawable/c01" android:duration="500"/>
<item android:drawable="@drawable/c02" android:duration="500"/>
<item android:drawable="@drawable/c03" android:duration="500"/>
<item android:drawable="@drawable/c04" android:duration="500"/>
<item android:drawable="@drawable/c05" android:duration="500"/>
</animation-list>3、在res/layout目錄下新建XML文件:frame_anim_layout.xml
[html]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:id="@+id/imgFrame"
android:background="@drawable/frame_anim"></ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="開始"
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"></Button>
<Button
android:text="結束"
android:id="@+id/btnEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"></Button>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:id="@+id/imgFrame"
android:background="@drawable/frame_anim"></ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="開始"
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"></Button>
<Button
android:text="結束"
android:id="@+id/btnEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"></Button>
</LinearLayout>
</LinearLayout>4、Activity文件中的代碼:
[java]
package com.bison;
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 AnimationDemoActivity extends Activity implements OnClickListener {
// 開始按鈕
private Button btnStart;
// 結束按鈕
private Button btnEnd;
private ImageView imgFrame;
// 聲明Frame動畫對象
private AnimationDrawable frameAnim;
/** 初始化 */
public void init() {
btnStart = (Button) findViewById(R.id.btnStart);
btnEnd = (Button) findViewById(R.id.btnEnd);
btnStart.setOnClickListener(this);
btnEnd.setOnClickListener(this);
imgFrame = (ImageView) findViewById(R.id.imgFrame);
// 將ImageView的backgroud聲明給Frame動畫對象
frameAnim = (AnimationDrawable) imgFrame.getBackground();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 加載layout.frame_anim_layout頁面
setContentView(R.layout.frame_anim_layout);
init();
}
public void onClick(View v) {
// 判斷按鈕事件
switch (v.getId()) {
case R.id.btnStart:
frameAnim.start();
break;
case R.id.btnEnd:
frameAnim.stop();
break;
}
}
}
package com.bison;
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 AnimationDemoActivity extends Activity implements OnClickListener {
// 開始按鈕
private Button btnStart;
// 結束按鈕
private Button btnEnd;
private ImageView imgFrame;
// 聲明Frame動畫對象
private AnimationDrawable frameAnim;
/** 初始化 */
public void init() {
btnStart = (Button) findViewById(R.id.btnStart);
btnEnd = (Button) findViewById(R.id.btnEnd);
btnStart.setOnClickListener(this);
btnEnd.setOnClickListener(this);
imgFrame = (ImageView) findViewById(R.id.imgFrame);
// 將ImageView的backgroud聲明給Frame動畫對象
frameAnim = (AnimationDrawable) imgFrame.getBackground();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 加載layout.frame_anim_layout頁面
setContentView(R.layout.frame_anim_layout);
init();
}
public void onClick(View v) {
// 判斷按鈕事件
switch (v.getId()) {
case R.id.btnStart:
frameAnim.start();
break;
case R.id.btnEnd:
frameAnim.stop();
break;
}
}
}
PS:Frame動畫原理類似於電影膠片,一幕一幕的閃過,在人的視覺停留期快速變動,形成組圖,產生動畫。
摘自 今非昔…畢…的專欄