본문 바로가기

Android/Animation

Android Animation example

Android 프로젝트의 res/anim/ 폴더에 애니메이션 정보를 xml 파일에 저장하고 로드하여 특정 View에 적용하는 예

main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onBtnClick" android:text="애니메이션 시작" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/persian_smile" /> </LinearLayout>

res/anim/flow.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100%p" android:toXDelta="0%p" android:duration="6000" android:repeatCount="3"/> <alpha android:fromAlpha="0.5" android:toAlpha="1" android:duration="6000" android:repeatCount="3"/> </set>

TestActivity.java

package com.example.androidapp; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.webkit.JsResult; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; public class TestActivity extends Activity { ImageView iv; Animation animation; public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);

iv = (ImageView) findViewById(R.id.imageView1); animation = AnimationUtils.loadAnimation(this, R.anim.flow);

animation.setAnimationListener(new Animation.AnimationListener() { public void onAnimationStart(Animation animation) { } public void onAnimationRepeat(Animation animation) { } public void onAnimationEnd(Animation animation) { } }); } public void onBtnClick(View v) { iv.startAnimation(animation); } }