본문 바로가기

Android/Layout Animation

Android Layout Animation example

Android Layout Animation example

Layout Animation은 Layout객체 안에 View가 동적으로 추가될 때 추가되는 View에 애니메이션을 적용하여 화면에 나타내는 방법을 제시한다.

LinearLayout 안에 동적으로 추가되는 뷰가 위에서 아래로 이동하여 제자리에 위치하는 애니메이션 예제

package com.dearpeople.android.test.animation;

import android.app.*;
import android.content.*;
import android.graphics.Color;
import android.os.*;
import android.view.*;
import android.view.animation.*;
import android.widget.*;
import android.widget.TableLayout.LayoutParams;

public class LayoutAniActivity extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button startAni = (Button) this.findViewById(R.id.startBtn);
        startAni.setOnClickListener(this);
    }
    /* ViewGroup를 아규먼트로 받아서 해당 ViewGroup에 추가되는 뷰가 있을 경우에 적용하는 애니메이션을 정의함 */
    public static void setLayoutAnim_slidedownfromtop(ViewGroup panel) {

       AnimationSet set = new AnimationSet(true);

       Animation animation = new AlphaAnimation(0.0f, 1.0f);
       animation.setDuration(1000);
       set.addAnimation(animation);

       animation = new TranslateAnimation(
           Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
           Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
       );
       animation.setDuration(1000);
       set.addAnimation(animation);
       /*
       ScaleAnimation scale = new ScaleAnimation(-1, 1, -1, 1,
         ScaleAnimation.RELATIVE_TO_SELF, 1f,
         ScaleAnimation.RELATIVE_TO_SELF, 1f);
       set.addAnimation(scale);
       */
       panel.startAnimation(set);
    }

 @Override
 public void onClick(View v) {
   final LinearLayout aniLayout = (LinearLayout)this.findViewById(R.id.aniLayout);
   
        Button bt = new Button(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, 0);
        params.topMargin = 0;
        params.width = 80;
        params.height = LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER_HORIZONTAL;
        bt.setLayoutParams(params);
        bt.setText("애\n니\n메\n이\n션\n테\n스\n트");
        bt.setBackgroundColor(Color.WHITE);
        bt.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    aniLayout.removeView(v);
   }
  });
       
        setLayoutAnim_slidedownfromtop(aniLayout);
        aniLayout.addView(bt);
 }
}




main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 <Button
  android:id="@+id/startBtn"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Add ViewGroup">
 </Button>
 <LinearLayout
  android:id="@+id/aniLayout"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:layout_gravity="center_horizontal"/>
</LinearLayout>