Kelly's journey to a coding master

AnimationDrawable class in Java 본문

Android

AnimationDrawable class in Java

개발하는 통계학도 켈리 2023. 5. 15. 10:53

https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable

 

AnimationDrawable  |  Android Developers

 

developer.android.com

Drawablae 객체를 연속적으로 정의해서 View 객체의 백그라운드로 사용함으로써 frame-by-frame 애니메이션을 만들 때 쓰이는 객체이다.

가장 간단하게 구현하는 방법은 XML 파일(res/drawable/ folder에 위치함)에 애니메이션을 정의하고 나서 View 객체의 백그라운드로 설정하는 것이다.

애니메이션을 실행할 때는 start() 를 호출하면 된다. 

// MainActivity.java

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    AnimationDrawable ballAnimation;
    ImageView ballImage;
    Button bPlay;
    Button bPause;

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

        ballImage = (ImageView) findViewById(R.id.ballImg);
        ballImage.setBackgroundResource(R.drawable.bounce_ball);
        ballAnimation = (AnimationDrawable) ballImage.getBackground();

        bPlay = (Button) findViewById(R.id.bPlay);
        bPause = (Button) findViewById(R.id.bPause);
        bPlay.setOnClickListener(buttonsOnClickListener);
        bPause.setOnClickListener(buttonsOnClickListener);

        setUpPlayer();
    }
    private View.OnClickListener buttonsOnClickListener = new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.bPlay:
                    ballAnimation.start();
                    break;
                case R.id.bPause:
                    ballAnimation.stop();
                    break;
            }
        }
    };
}
//activity_main.xml 생략
//bounce_ball.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/i1" android:duration="250" />
    <item android:drawable="@drawable/i2" android:duration="250" />
    <item android:drawable="@drawable/i3" android:duration="250" />
    <item android:drawable="@drawable/i4" android:duration="250" />
    <item android:drawable="@drawable/i5" android:duration="250" />
    <item android:drawable="@drawable/i6" android:duration="250" />
    <item android:drawable="@drawable/i7" android:duration="250" />
    <item android:drawable="@drawable/i8" android:duration="250" />
    <item android:drawable="@drawable/i9" android:duration="250" />
    <item android:drawable="@drawable/i10" android:duration="250" />
    <item android:drawable="@drawable/i11" android:duration="250" />
    <item android:drawable="@drawable/i12" android:duration="250" />
    <item android:drawable="@drawable/i13" android:duration="250" />
    <item android:drawable="@drawable/i14" android:duration="250" />
    <item android:drawable="@drawable/i15" android:duration="250" />
    <item android:drawable="@drawable/i16" android:duration="250" />
    <item android:drawable="@drawable/i17" android:duration="250" />
    <item android:drawable="@drawable/i18" android:duration="250" />
    <item android:drawable="@drawable/i19" android:duration="250" />
    <item android:drawable="@drawable/i20" android:duration="250" />
</animation-list>

 

'Android' 카테고리의 다른 글

StringBuffer class in Java  (0) 2023.05.08