이번 시간에는 아이콘을 움직이는 방법에 대해 알아보겠습니다.
안드로이드에는 움직이는 동작을 실행시키기 위해서
다양한 애니메이션 기능이 있으며, 여기서는 속성 애니메이션을
사용해 보겠습니다.
ImageView를 통해 아이콘을 하나 만들어 놓고,
5개의 버튼을 만든 후, 5개의 기능으로
가로로 이동, 세로로 이동, 회전, 사라짐, 나타남을
구현했습니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:src="@drawable/lion" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:orientation="horizontal">
<Button
android:id="@+id/transX_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="showTranslationX"
android:text="가로방향" />
<Button
android:id="@+id/transY_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="showTranslationY"
android:text="세로방향" />
<Button
android:id="@+id/rotate_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="showRotate"
android:text="회전" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/alpha_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="showAlpha"
android:text="사라짐" />
<Button
android:id="@+id/alpha_btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="showAlpha2"
android:text="나타남" />
</LinearLayout>
</LinearLayout>
이미지 res -> drawable
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
long animationDuration = 1000; //1초
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
}
//가로방향
public void showTranslationX(View view) {
ValueAnimator animatorX = ObjectAnimator.ofFloat(imageView, "translationX", 100f, 200f, 50f);
animatorX.setDuration(animationDuration);
animatorX.start();
}
//세로방향
public void showTranslationY(View view) {
ValueAnimator animatorY = ObjectAnimator.ofFloat(imageView, "translationY", 100f, 200f, 50f);
animatorY.setDuration(animationDuration);
animatorY.start();
}
//회전
public void showRotate(View view) {
ValueAnimator rotateAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);
rotateAnimator.setDuration(animationDuration);
rotateAnimator.start();
}
//사라짐
public void showAlpha(View view) {
ValueAnimator alphaAnimator = ObjectAnimator.ofFloat(imageView, View.ALPHA, 1.0f, 0f);
alphaAnimator.setDuration(animationDuration);
alphaAnimator.start();
}
//나타남
public void showAlpha2(View view) {
ValueAnimator alphaAnimator = ObjectAnimator.ofFloat(imageView, View.ALPHA, 0f, 1.0f);
alphaAnimator.setDuration(animationDuration);
alphaAnimator.start();
}
}
참조사이트
https://developer.android.com/guide/topics/graphics/prop-animation?hl=ko
속성 애니메이션 개요 | Android 개발자 | Android Developers
속성 애니메이션 개요 속성 애니메이션 시스템은 거의 모든 항목을 애니메이션으로 만들 수 있는 강력한 프레임워크입니다. 애니메이션을 정의하여 화면에 그리는지에 관계없이 시간 경과에
developer.android.com
2021.12.24 - [안드로이드] - [안드로이드] 동물 리스트(ListView) 만드는 방법
[안드로이드] 동물 리스트(ListView) 만드는 방법
이번 시간에는 ListView를 통해 동물 리스트를 만들어 보겠습니다. 1. 동물 클래스 만들기 Animal.java java -> 프로젝트명 클릭 -> 마우스 오른쪽 -> New -> Java Class Name -> Animal -> 엔터 public c..
aries574.tistory.com
2021.12.25 - [안드로이드] - [안드로이드] 동물 리스트(ListView) 검색(SearchView) 기능 추가 방법
[안드로이드] 동물 리스트(ListView) 검색(SearchView) 기능 추가 방법
2021.12.24 - [안드로이드] - [안드로이드] ListView 동물 리스트 만드는 방법 프로젝트명 클릭 -> 마우스 오른쪽 -> New -> Java Class Name -> Animal -> 엔터 public c.." data-og-host="aries574.tistory.com"..
aries574.tistory.com
2021.12.23 - [안드로이드] - [안드로이드] NumberPicker 숫자 선택하는 방법 알아보기
[안드로이드] NumberPicker 숫자 선택하는 방법 알아보기
이번 시간에는 계정을 만들 때 흔히 볼 수 있는 숫자 선택하는 기능을 알아보겠습니다. 안드로이드에서는 NumberPicker를 통해 쉽게 만들 수 있습니다. 1. 메인화면 구성 (activity_main.xml) <?xml version="1.
aries574.tistory.com
[안드로이드] 애니메이션으로 뷰(View) 표시 및 숨기기 - 카드플립(CardFlip) (0) | 2021.12.28 |
---|---|
[안드로이드] 애니메이션으로 뷰(View) 표시 및 숨기기 - 크로스페이드(CrossFade) 쉽게 만드는 방법 (0) | 2021.12.27 |
[안드로이드] 동물 리스트(ListView) 검색(SearchView) 기능 추가 방법 (2) | 2021.12.25 |
[안드로이드] 동물 리스트(ListView) 만드는 방법 (6) | 2021.12.24 |
[안드로이드] NumberPicker 숫자 선택하는 방법 알아보기 (0) | 2021.12.23 |
댓글 영역