본문 바로가기

미래(2015-2016)/자습

토끼 속도 줄이기

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.lastviolet.myview;
 
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
 
public class MainActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 제목 표시줄 제거
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 상태 표시줄 제거
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        MyView view = new MyView(this);
            
        setContentView(view);
    }
        
    // Custom View 클래스를 설계
    class MyView extends View
    {
        int width, height;
        float bx, by;
        int bw, bh;
        float dx, dy;        // 이동 변화량
        int index = 0;    // 이미지 배열의 번호
        int loop = 0;
        int x1, y1;        // 터치다운한 위치 저장
        
        Bitmap[] bm = new Bitmap[2];
        Handler handler = new Handler(){
            
            public void handleMessage(android.os.Message msg) 
            {
                // 화면을 지우고 onDraw 메소드를 호출
                invalidate();    // 화면 갱신
                
                handler.sendEmptyMessageDelayed(010);
            }
        };
        
        public MyView(Context context) 
        {
            super(context);
//            getWindowManager();    // MainActivity가 가지고 있는 메소드. 이너클래스일 경우에만 호출 가능
            
            Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
                    
            width = display.getWidth();
            height = display.getHeight();
            
            bm[0= BitmapFactory.decodeResource(context.getResources(), R.drawable.rabbit_1);
            bm[0= Bitmap.createScaledBitmap(bm[0], 100150true);
            
            bm[1= BitmapFactory.decodeResource(context.getResources(), R.drawable.rabbit_2);
            bm[1= Bitmap.createScaledBitmap(bm[1], 100150true);
            
            bx = width / 2;
            by = height / 2;
            
            bw = bm[0].getWidth() / 2;
            bh = bm[0].getHeight() / 2;
            
            dx = 3;
            dy = 3;
                
            handler.sendEmptyMessageDelayed(010);
        }
                    
        // 생성자 함수가 종료된 후 한 번 자동으로 호출되는 콜백 메소드
        // MyView가 화면에 무엇인가를 보이게 하려면 이 메소드 안에 코딩해야 한다.
        @Override
        protected void onDraw(Canvas canvas) 
        {                        
            bx += dx;
            by += dy;
            
            // 왼쪽 벽에 닿았는가
            if (bx <= bw)
            {
                dx = -dx;
                bx = bw;        // bx가 음수가 되는 것을 방지해, 그림이 잘리는 것을 막는다.
            }
            // 오른쪽 벽에 닿았는가
            if (bx >= width - bw)
            {
                dx = -dx;
                bx = width - bw;
            }
            // 위쪽 벽에 닿았는가
            if (by <= bh)
            {
                dy = -dy;
                by = bh;
            }
            
            // 아래쪽 벽에 닿았는가
            if (by >= height - bh)
            {
                dy = -dy;
                by = height - bh;
            }
            
            if (loop % 15 == 0)
            {
                index = 1 - index;
                
                dx *= 0.9;
                dy *= 0.9;
            }
            
            loop++;
                                                        
            canvas.drawBitmap(bm[index], bx - bw, by - bh, null);
        } // end of onDraw
        
        @Override
        public boolean onTouchEvent(MotionEvent event) 
        {
            int action = event.getAction();
            int x = (int)event.getX();
            int y = (int)event.getY();
                        
            switch(action)
            {
            case MotionEvent.ACTION_DOWN:
                
                x1 = x;
                y1 = y;
                
                break;
                
            case MotionEvent.ACTION_UP:
                                
                dx = (x - x1) / 10;
                dy = (y - y1) / 10;
                                
                bx += dx;
                by += dy;            
                
                break;
                
            case MotionEvent.ACTION_MOVE:
                
//                dx = x - x1;
//                dy = y - y1;
//                
//                x1 = x;
//                y1 = y;
//                
//                bx += dx;
//                by += dy;
//                
//                if (bx < bw)
//                    bx = bw;
//                
//                if (bx > width - bw) 
//                    bx = width - bw;
//                
//                if (by < bh)
//                    by = bh;
//                
//                if (by > height - bh)
//                    by = height - bh;
                
                break;
            }
            
            return true;
        }
                        
    } // end of MyView
// end of MainActivity
 
cs


 MyView.zip



'미래(2015-2016) > 자습' 카테고리의 다른 글

오늘의 과제  (0) 2015.12.14
JTabbedPane  (0) 2015.11.03
룩앤필 사용하기  (0) 2015.11.03
JToolBar  (0) 2015.11.02
JTable, DefaultTableModel  (0) 2015.11.02