Source: https://www.edwith.org/boostcourse-android/lecture/17111/



1. 실행 결과 화면

    



2. activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context="com.tistory.qlyh8.practice.MainActivity">
 
    <Button
        android:id="@+id/button"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="시작"/>
 
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
 
</LinearLayout>
 
cs



3. MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.tistory.qlyh8.practice;
 
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
 
import java.util.ArrayList;
 
 
public class MainActivity extends AppCompatActivity {
 
    private ImageView imageView;
    ArrayList<Drawable> drawableList = new ArrayList<>();
    Handler handler;
 
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        imageView = findViewById(R.id.image_view);
        handler = new Handler();
 
        Resources res = getResources();
        drawableList.add(res.getDrawable(R.drawable.image22));
        drawableList.add(res.getDrawable(R.drawable.image33));
        drawableList.add(res.getDrawable(R.drawable.image44));
        drawableList.add(res.getDrawable(R.drawable.image55));
        drawableList.add(res.getDrawable(R.drawable.image66));
 
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    AnimThread thread = new AnimThread();
                    thread.start();
            }
        });
    }
 
    class AnimThread extends Thread {
        // 일정 시간 간격으로 이미지뷰의 이미지를 다른 이미지로 설정
        public void run() {
            int index = 0;
            for (int i = 0; i < 100; i++) {
                final Drawable drawable = drawableList.get(index);
                index += 1;
                if (index > 4) {index = 0;}
 
                handler.post(new Runnable() {
                    public void run() {
                        imageView.setImageDrawable(drawable);
                    }
                });
 
                try {
                    Thread.sleep(500);
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
cs



4. 참고

- Animations and Transitions: https://developer.android.com/training/animation/

'Android' 카테고리의 다른 글

슬라이딩 애니메이션 (Sliding Animation)  (0) 2018.08.24
트윈 애니메이션 (Tween Animation)  (0) 2018.08.24
RecyclerView (리사이클러뷰)  (0) 2018.08.23
음성 녹음하기  (0) 2018.08.23
동영상 재생하기  (0) 2018.08.23

+ Recent posts