anim.xml 파일을 생성한다.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="300dp" android:layout_height="300dp"/> <ToggleButton android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ToggleButton" android:layout_gravity="right"/> <ToggleButton android:id="@+id/toggleButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ToggleButton" android:layout_gravity="right"/> </LinearLayout>anim.xml
<?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/red" android:duration="300" /> <item android:drawable="@drawable/yellow" android:duration="300" /> <item android:drawable="@drawable/blue" android:duration="300" /> </animation-list>r.java
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package com.gusfree.frameAnimation; public final class R { public static final class attr { } public static final class drawable { public static final int anim=0x7f020000; public static final int blue=0x7f020001; public static final int ic_launcher=0x7f020002; public static final int red=0x7f020003; public static final int yellow=0x7f020004; } public static final class id { public static final int imageView1=0x7f050000; public static final int toggleButton1=0x7f050001; public static final int toggleButton2=0x7f050002; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; } }
package com.gusfree.frameAnimation; import android.app.Activity; import android.content.res.Resources; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageView; import android.widget.ToggleButton; public class FrameAnimationActivity extends Activity { ImageView imageView; ToggleButton toggle; AnimationDrawable ani; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView=(ImageView)findViewById(R.id.imageView1); toggle =(ToggleButton)this.findViewById(R.id.toggleButton1); //프레임 에니메이션 객체를 만들자. ani = new AnimationDrawable(); Resources res = getResources();//리소스 가져와라 ani.addFrame(res.getDrawable(R.drawable.red),300);//1000이 1초 ,500=0.5초 ani.addFrame(res.getDrawable(R.drawable.blue),300); ani.addFrame(res.getDrawable(R.drawable.yellow),300); //에니메이션 무한반복 셋팅하자. ani.setOneShot(false); //xml로 만든 애니메이션을 이미지뷰에 셋팅하자. imageView.setBackgroundResource(R.drawable.anim); ani=(AnimationDrawable)imageView.getBackground(); toggle.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override //메서드 체크바꼈을 때 public void onCheckedChanged(CompoundButton arg0, boolean isChecked) { if(isChecked){ ani.start(); }else{ ani.stop(); } } }); } }
'Android > 2012.04월 강좌' 카테고리의 다른 글
7일차 복습 (0) | 2012.05.04 |
---|---|
6일차 TwinAnimaion (0) | 2012.05.03 |
6일차 FrameAnimation (2) | 2012.05.03 |
6일차 Dialog (0) | 2012.05.03 |
6일차 Menu 2 (Option menu) (0) | 2012.05.03 |