본문 바로가기
안드로이드 자바

[Java][Android] ScaleAnimation 사용하여 애니메이션 효과 넣기

by teamnova 2025. 5. 13.
728x90

안녕하세요

오늘은 ScaleAnimation을 사용해서 애니메이션 효과를 넣어보도록 하겠습니다.

 

전체 코드입니다.

 

activity_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"
  android:gravity="center"
  android:padding="24dp">

  <TextView
    android:id="@+id/myText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="안녕하세요!"
    android:textSize="24sp"
    android:textStyle="bold" />

  <Button
    android:id="@+id/scaleButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="시작하기"
    android:layout_marginTop="20dp" />
</LinearLayout>

 

MainActivity.java

public class MainActivity extends AppCompatActivity {

  private TextView myText;
  private Button scaleButton;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myText = findViewById(R.id.myText);
    scaleButton = findViewById(R.id.scaleButton);

    scaleButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

        // ScaleAnimation(fromX, toX, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue)
        ScaleAnimation scaleAnim = new ScaleAnimation(
            1.0f, 1.5f, // X: 원래 크기 → 1.5배
            1.0f, 1.5f, // Y: 원래 크기 → 1.5배
            ScaleAnimation.RELATIVE_TO_SELF, 0.5f, // 중심 기준 X
            ScaleAnimation.RELATIVE_TO_SELF, 0.5f  // 중심 기준 Y
        );

        scaleAnim.setDuration(300);       // 지속 시간 (ms)
        scaleAnim.setRepeatCount(1);       // 1회 반복
        scaleAnim.setRepeatMode(ScaleAnimation.REVERSE); // 원래 크기로 되돌아가기

        myText.startAnimation(scaleAnim);
      }
    });
  }
}

 

 

시연 영상 입니다.