728x90
안녕하세요,
오늘은 TranslateAnimation을 활용하여 텍스트를 이동하는 애니메이션 예제를 만들어 보겠습니다.
TranslateAnimation은 Android에서 뷰를 X축과 Y축 방향으로 이동시키는 애니메이션 효과를 구현할 때 사용하는 클래스 입니다.
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TextView와 Button을 연결
TextView textView = findViewById(R.id.textView);
Button button = findViewById(R.id.button);
// 버튼 클릭 리스너 설정
button.setOnClickListener(v -> {
// TranslateAnimation 생성
TranslateAnimation animation = new TranslateAnimation(
0, // 시작 x 위치
300, // 끝 x 위치 (px 기준으로 이동)
0, // 시작 y 위치
300 // 끝 y 위치
);
animation.setDuration(1000); // 애니메이션 지속 시간 (밀리초)
animation.setFillAfter(true); // 애니메이션 종료 후 상태 유지
// TextView에 애니메이션 적용
textView.startAnimation(animation);
});
}
}
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="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="틱스트 입니다."
android:textSize="18sp"
android:layout_marginBottom="20dp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="텍스트 이동" />
</LinearLayout>
시연 영상입니다.
'안드로이드 자바' 카테고리의 다른 글
[JAVA][Android] TouchDelegate로 터치 영역 확장하기 (0) | 2024.12.22 |
---|---|
[Android][JAVA] TextView와 DynamicTextView 비교: 텍스트 레이아웃 처리의 차이점 (2) | 2024.12.19 |
[Java][Android] PhotoEditor 라이브러리 사용하여 이미지 편집하기 (2) | 2024.12.14 |
[Java][Android] AlphaAnimation을 활용하여 글자 페이드 인/페이드 아웃 애니메이션 적용 (0) | 2024.12.12 |
[Java][Android] SeekBar 사용하여 값 나타내기 (0) | 2024.12.08 |