-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestSurfaceView.java
108 lines (87 loc) · 3 KB
/
TestSurfaceView.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
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
package com.example.surfaceapp;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
class TestSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
public class DrawThread extends Thread {
private volatile boolean running = true;
private SurfaceHolder surfaceHolder;
private Paint backgroundPaint = new Paint();
private Paint circlePaint = new Paint();
private int towardPointX;
private int towardPointY;
{
backgroundPaint.setColor(Color.BLUE);
circlePaint.setColor(Color.YELLOW);
}
public void setTowardPoint(int x, int y) {
towardPointX = x;
towardPointY = y;
}
public DrawThread(Context context, SurfaceHolder surfaceHolder) {
this.surfaceHolder = surfaceHolder;
}
public void requestStop() {
running = false;
}
private int radius = 0;
public void setRadius(int rad){
radius = rad;
}
@Override
public void run() {
while (running) {
Canvas canvas = surfaceHolder.lockCanvas();
if (canvas != null) {
try {
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), backgroundPaint);
canvas.drawCircle(towardPointX, towardPointY, radius, circlePaint);
radius+=5;
DrawThread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
private DrawThread drawThread;
public TestSurfaceView(Context context) {
super(context);
getHolder().addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
drawThread = new DrawThread(getContext(), getHolder());
drawThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
drawThread.requestStop();
boolean retry = true;
while (retry) {
try {
drawThread.join();
retry = false;
} catch (InterruptedException e) {
//
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
drawThread.setTowardPoint((int)event.getX(),(int)event.getY());
drawThread.setRadius(0);
return false;
}
}