-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathReceiverView.java
86 lines (72 loc) · 2.13 KB
/
ReceiverView.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
package su.levenetc.android.interactivecanvas;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by Eugene Levenetc.
*/
public class ReceiverView extends View {
private Picture picture;
private int dx;
private int dy;
private Paint textPaint = new Paint();
private Rect bounds = new Rect();
private ITouchEventHandler touchHandler;
private int fullCanvasWidth;
private int fullCanvasHeight;
public ReceiverView(Context context) {
super(context);
init();
}
public ReceiverView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void setTouchHandler(ITouchEventHandler touchHandler) {
this.touchHandler = touchHandler;
}
private void init() {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setColor(Color.BLACK);
textPaint.setAntiAlias(true);
textPaint.setTextSize(Utils.getSp(getContext(), 24));
setBackgroundColor(Color.GRAY);
setWillNotDraw(false);
}
public void setPicture(PictureWrapper wrapper) {
this.picture = wrapper.picture;
this.dx = wrapper.dx;
this.dy = wrapper.dy;
bounds.set(0, 0, fullCanvasWidth, fullCanvasHeight);
invalidate();
}
@Override public boolean onTouchEvent(MotionEvent event) {
if (touchHandler != null) {
event.setLocation(event.getX() - dx, event.getY() - dy);
touchHandler.handleTouchEvent(event);
return true;
} else {
return super.onTouchEvent(event);
}
}
@Override protected void onDraw(Canvas canvas) {
if (picture != null) {
canvas.translate(dx, dy);
canvas.drawPicture(picture, bounds);
} else {
final String waiting = "Waiting...";
canvas.drawText(waiting, canvas.getWidth() / 2 - textPaint.measureText(waiting) / 2, canvas.getHeight() / 2, textPaint);
}
}
public void config(int fullCanvasWidth, int fullCanvasHeight) {
this.fullCanvasWidth = fullCanvasWidth;
this.fullCanvasHeight = fullCanvasHeight;
}
}