<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#ffffff">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Frame by Frame Animation Test\n애니메이션이 멈추면 프레임이미지를 터치해보세요"
android:gravity="center"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal">
<ImageView android:id="@+id/img"
android:layout_width="80px"
android:layout_height="90px"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/btnStart"
android:text="프레임 애니메이션 시작"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</LinearLayout>
사용된 이미지
package com.dearpeople.android.test.ani;
import android.app.*;
import android.graphics.drawable.*;
import android.os.*;
import android.view.Gravity;
import android.view.View;
import android.widget.*;
public class FrameByFrameActivity extends Activity {
AnimationDrawable animation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnStart = (Button) findViewById(R.id.btnStart);
final ImageView imgView = (ImageView)findViewById(R.id.img);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startAnimation();
}
});
imgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(150, 50);
params.alignWithParent = true;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
imgView.setLayoutParams(params);
imgView.setBackgroundResource(R.drawable.t00_poi_07_sel);
}
});
}
/* 내부클래스, UI 쓰레드가 실행할 코드(Runnable)를 작성한다 */
class Starter implements Runnable {
// UI쓰레드는 Runnable객체를 받아서 실행하면 애니메이션이 시작된다
public void run() {
animation.start();
}
}
private void startAnimation(){
// 각 프레임을 저장할 객체를 생성한다
animation = new AnimationDrawable();
// 각 프레임으로 사용할 이미지를 등록한다
animation.addFrame(getResources().getDrawable(R.drawable.t00_poi_ani_1), 100);
animation.addFrame(getResources().getDrawable(R.drawable.t00_poi_ani_2), 100);
animation.addFrame(getResources().getDrawable(R.drawable.t00_poi_ani_3), 100);
animation.addFrame(getResources().getDrawable(R.drawable.t00_poi_ani_4), 100);
animation.addFrame(getResources().getDrawable(R.drawable.t00_poi_07), 100);
// 애니메이션을 한번만 실행할 것인지 반복할 것인지 설정한다
animation.setOneShot(true);
ImageView imageView = (ImageView) findViewById(R.id.img);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80, 90);
params.alignWithParent = true;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
imageView.setLayoutParams(params);
//배경을 애니메이션으로 설정한다
//imageView.setBackgroundDrawable(animation);
// 배경위에서 실행되는 애니메이션으로 사용할 경우
imageView.setImageDrawable(animation);
// UI thread가 애니메이션을 실행할 수 있도록 ImageView에게 Runnable객체를 보낸다.
imageView.post(new Starter());
}
}