CaptureAnimationOverlay.java
8.26 KB
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
224
225
226
227
228
229
230
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sw.laryngoscope.widget;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
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.RectF;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import com.sw.laryngoscope.common.Constant;
import com.sw.laryngoscope.manager.SpManager;
import com.sw.laryngoscope.utils.Logger;
/**
* This class handles all the animations at capture time. Post capture animations
* will be handled in a separate place.
*/
public class CaptureAnimationOverlay extends View {
private final static String TAG = "CaptureAnimOverlay";
private final static int FLASH_COLOR = Color.WHITE;
private static final float FLASH_MAX_ALPHA = 0.85f;
private static final long FLASH_FULL_DURATION_MS = 65;
private static final long FLASH_DECREASE_DURATION_MS = 150;
private static final float SHORT_FLASH_MAX_ALPHA = 0.25f;
private static final long SHORT_FLASH_FULL_DURATION_MS = 34;
private static final long SHORT_FLASH_DECREASE_DURATION_MS = 100;
private AnimatorSet mFlashAnimation;
private final RectF mPreviewArea = new RectF();
private final Paint mPaint = new Paint();
private final Interpolator mFlashAnimInterpolator;
private final ValueAnimator.AnimatorUpdateListener mFlashAnimUpdateListener;
private final Animator.AnimatorListener mFlashAnimListener;
private Path roundRectPath;
public CaptureAnimationOverlay(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint.setColor(FLASH_COLOR);
mFlashAnimInterpolator = new LinearInterpolator();
mFlashAnimUpdateListener = new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float alpha = 255.0f * (Float) animation.getAnimatedValue();
mPaint.setAlpha((int) alpha);
invalidate();
}
};
mFlashAnimListener = new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
setVisibility(VISIBLE);
}
@Override
public void onAnimationEnd(Animator animation) {
mFlashAnimation = null;
setVisibility(INVISIBLE);
}
@Override
public void onAnimationCancel(Animator animation) {
// End is always called after cancel.
}
@Override
public void onAnimationRepeat(Animator animation) {
}
};
mPaint1 = new Paint();
mPaint1.setAntiAlias(true);
//mPaint1.setColor(Color.BLACK);
mPaint1.setColor(Color.parseColor("#1B2027"));
mPaint1.setStrokeJoin(Paint.Join.ROUND);
mPaint1.setStrokeCap(Paint.Cap.ROUND);
mPaint1.setStrokeWidth(1);
roundRectPath = new Path();
}
/**
* Start flash animation.
*
* @param shortFlash show shortest possible flash instead of regular long version.
*/
public void startFlashAnimation(boolean shortFlash) {
if (mFlashAnimation != null && mFlashAnimation.isRunning()) {
mFlashAnimation.cancel();
}
float maxAlpha;
if (shortFlash) {
maxAlpha = SHORT_FLASH_MAX_ALPHA;
} else {
maxAlpha = FLASH_MAX_ALPHA;
}
ValueAnimator flashAnim1 = ValueAnimator.ofFloat(maxAlpha, maxAlpha);
ValueAnimator flashAnim2 = ValueAnimator.ofFloat(maxAlpha, .0f);
if (shortFlash) {
flashAnim1.setDuration(SHORT_FLASH_FULL_DURATION_MS);
flashAnim2.setDuration(SHORT_FLASH_DECREASE_DURATION_MS);
} else {
flashAnim1.setDuration(FLASH_FULL_DURATION_MS);
flashAnim2.setDuration(FLASH_DECREASE_DURATION_MS);
}
flashAnim1.addUpdateListener(mFlashAnimUpdateListener);
flashAnim2.addUpdateListener(mFlashAnimUpdateListener);
flashAnim1.setInterpolator(mFlashAnimInterpolator);
flashAnim2.setInterpolator(mFlashAnimInterpolator);
mFlashAnimation = new AnimatorSet();
mFlashAnimation.play(flashAnim1).before(flashAnim2);
mFlashAnimation.addListener(mFlashAnimListener);
mFlashAnimation.start();
}
//@Override
public void onPreviewAreaChanged(RectF previewArea) {
mPreviewArea.set(previewArea);
}
@Override
public void onDraw(Canvas canvas) {
if (mFlashAnimation != null && mFlashAnimation.isRunning()) {
// mPaint alpha is animated by the animation.
canvas.drawRect(mPreviewArea, mPaint);
canvas.clipRect(mPreviewArea);
drawTriangle(canvas);
}
}
@Override
public boolean hasOverlappingRendering() {
// The internal draw method will NOT have draw calls that overlap.
return false;
}
private Paint mPaint1;
private float mWidth;
private float mHeight;
private void drawTriangle(Canvas canvas) {
mWidth = getWidth();
mHeight = getHeight();
if ( SpManager.get_border_type() == Constant.BORDER_TYPE_A ) {
roundRectPath.reset();
roundRectPath.addRoundRect(new RectF(0, 0, mWidth, mHeight), 100, 100, Path.Direction.CW);
canvas.clipPath(roundRectPath, Region.Op.DIFFERENCE);
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
//canvas.restore();
//canvas.drawColor(getResources().getColor(R.color.transparent));
canvas.drawColor(Color.parseColor("#1B2027"));
Logger.d("====CaptureAnimationOverlay 1");
}
if ( SpManager.get_border_type() == Constant.BORDER_TYPE_B ) {
int sideLen = (int) (mWidth / 12f * 1.0f);
Path path0 = new Path();
path0.moveTo(0, 0);
path0.lineTo(sideLen, 0);
path0.lineTo(0, sideLen);
Path path1 = new Path();
path1.moveTo(mWidth - sideLen, 0);
path1.lineTo(mWidth, 0);
path1.lineTo(mWidth, sideLen);
Path path2 = new Path();
path2.moveTo(0, mHeight - sideLen);
path2.lineTo(0, mHeight);
path2.lineTo(sideLen, mHeight);
Path path3 = new Path();
path3.moveTo(mWidth - sideLen, mHeight);
path3.lineTo(mWidth, mHeight);
path3.lineTo(mWidth, mHeight - sideLen);
canvas.save();
canvas.drawPath(path0, mPaint1);
canvas.drawPath(path1, mPaint1);
canvas.drawPath(path2, mPaint1);
canvas.drawPath(path3, mPaint1);
canvas.restore();
Logger.d("====CaptureAnimationOverlay 2");
}
if ( SpManager.get_border_type() == Constant.BORDER_TYPE_C ) {
roundRectPath.reset();
roundRectPath.addCircle(mWidth / 2f, mHeight / 2f, mHeight / 2f, Path.Direction.CW);
canvas.clipPath(roundRectPath, Region.Op.DIFFERENCE);
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
//canvas.restore();
//canvas.drawColor(getResources().getColor(R.color.transparent));
canvas.drawColor(Color.parseColor("#1B2027"));
Logger.d("====CaptureAnimationOverlay 3");
}
if ( SpManager.get_border_type() == Constant.BORDER_TYPE_D ) {
Logger.d("====CaptureAnimationOverlay 4");
}
}
}