-
Notifications
You must be signed in to change notification settings - Fork 2
/
BezierView.java
223 lines (186 loc) · 6.64 KB
/
BezierView.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.com.mr_wrong.Bezier;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.example.mr_wrong.androidstudioproject.R;
/**
* Created by Mr_Wrong on 2015/5/1.
*/
public class BezierView extends FrameLayout {
private static final float DEFAULT_RADIUS = 50;
private float startX = 100;//起始点的坐标
private float startY = 100;
private float anchorX;//锚点的坐标
private float anchorY;
private float x;//手势的坐标
private float y;
private Path path;
private Paint paint;
private float radius;
private float speed = 15;//变瘦的速度,越大变化越慢
private float explored_radius = 9;
private ImageView exploredImageView;
private ImageView tipImageView;
private boolean istouch;
// 判断动画是否开始
private boolean isAnimStart;
public BezierView(Context context) {
this(context, null);
}
public BezierView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BezierView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
/**
* 初始化
*/
private void init() {
path = new Path();
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.RED);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
exploredImageView = new ImageView(getContext());
exploredImageView.setLayoutParams(params);
tipImageView = new ImageView(getContext());
tipImageView.setLayoutParams(params);
//tipImageView.setImageResource(R.drawable.skin_tips_new);
addView(exploredImageView);
addView(tipImageView);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
exploredImageView.setX(startX - exploredImageView.getWidth() / 2);
exploredImageView.setY(startX - exploredImageView.getHeight() / 2);
tipImageView.setX(startX - tipImageView.getWidth() / 2);
tipImageView.setY(startY - tipImageView.getHeight() / 2);
super.onLayout(changed, left, top, right, bottom);
}
/**
* 计算位置
*/
private void calculate() {
float distance = (float) Math.sqrt(Math.pow(y - startY, 2) + Math.pow(x - startX, 2));
radius = DEFAULT_RADIUS - distance / speed;
if (radius < explored_radius) {
isAnimStart = true;
//explore
exploredImageView.setVisibility(View.VISIBLE);
exploredImageView.setImageResource(R.drawable.tip_anim);
((AnimationDrawable) exploredImageView.getDrawable()).stop();
((AnimationDrawable) exploredImageView.getDrawable()).start();
tipImageView.setVisibility(View.GONE);
}
//计算四个点的位置
float offsetX = (float) (radius * Math.sin(Math.atan((y - startY) / (x - startX))));
float offsetY = (float) (radius * Math.cos(Math.atan((y - startY) / (x - startX))));
float x1 = startX - offsetX;
float y1 = startY + offsetY;
float x2 = x - offsetX;
float y2 = y + offsetY;
float x3 = x + offsetX;
float y3 = y - offsetY;
float x4 = startX + offsetX;
float y4 = startY - offsetY;
path.reset();
path.moveTo(x1, y1);
path.quadTo(anchorX, anchorY, x2, y2);
path.lineTo(x3, y3);
path.quadTo(anchorX, anchorY, x4, y4);
path.lineTo(x1, y1);
tipImageView.setX(x - tipImageView.getWidth() / 2);
tipImageView.setY(y - tipImageView.getHeight() / 2);
}
@Override
protected void onDraw(Canvas canvas) {
if (isAnimStart || !istouch) {
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.OVERLAY);
} else {
calculate();
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.OVERLAY);
canvas.drawPath(path, paint);
canvas.drawCircle(startX, startY, radius, paint);
canvas.drawCircle(x, y, radius, paint);
}
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
/* if (event.getAction() == MotionEvent.ACTION_DOWN) {
//判断是否触摸tipimageview
Rect rect = new Rect();
int[] location = new int[2];
tipImageView.getDrawingRect(rect);
tipImageView.getLocationOnScreen(location);
rect.left = location[0];
rect.top = location[1];
rect.right = location[0] + rect.right;
rect.bottom = rect.bottom + location[1];
if (rect.contains((int) event.getRawX(), (int) event.getRawY())) {
istouch = true;
}
} else if (event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP) {
istouch = false;
tipImageView.setX(startX - tipImageView.getWidth() / 2);
tipImageView.setY(startX - tipImageView.getHeight() / 2);
}*/
istouch = true;
invalidate();
anchorX = (event.getX() + startX) / 2;
anchorY = (event.getY() + startY) / 2;
x = event.getX();
y = event.getY();
return true;
}
public float getStartX() {
return startX;
}
public void setStartX(float startX) {
this.startX = startX;
}
public float getStartY() {
return startY;
}
public void setStartY(float startY) {
this.startY = startY;
}
public Paint getPaint() {
return paint;
}
public void setPaint(Paint paint) {
this.paint = paint;
}
public float getSpeed() {
return speed;
}
public void setSpeed(float speed) {
this.speed = speed;
}
public float getRadius() {
return radius;
}
public void setRadius(float radius) {
this.radius = radius;
}
public float getExplored_radius() {
return explored_radius;
}
public void setExplored_radius(float explored_radius) {
this.explored_radius = explored_radius;
}
}