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
|
/*
* Copyright (C) 2009 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.android.camera;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
/**
* A (vertical) widget which switches between the {@code Camera} and the
* {@code VideoCamera} activities.
*/
public class Switcher extends ImageView implements View.OnTouchListener {
@SuppressWarnings("unused")
private static final String TAG = "Switcher";
/** A callback to be called when the user wants to switch activity. */
public interface OnSwitchListener {
// Returns true if the listener agrees that the switch can be changed.
public boolean onSwitchChanged(Switcher source, boolean onOff);
}
private static final int ANIMATION_SPEED = 200;
private static final long NO_ANIMATION = -1;
private final int DISABLED_COLOR;
private boolean mSwitch = false;
private int mPosition = 0;
private long mAnimationStartTime = NO_ANIMATION;
private int mAnimationStartPosition;
private OnSwitchListener mListener;
public Switcher(Context context, AttributeSet attrs) {
super(context, attrs);
DISABLED_COLOR = context.getResources().getColor(R.color.icon_disabled_color);
}
public void setSwitch(boolean onOff) {
if (mSwitch == onOff) return;
mSwitch = onOff;
invalidate();
}
// Try to change the switch position. (The client can veto it.)
private void tryToSetSwitch(boolean onOff) {
try {
if (mSwitch == onOff) return;
// Switch may be changed during the callback so set it before the
// callback.
mSwitch = onOff;
if (mListener != null) {
if (!mListener.onSwitchChanged(this, onOff)) {
mSwitch = !onOff;
return;
}
}
} finally {
startParkingAnimation();
}
}
public void setOnSwitchListener(OnSwitchListener listener) {
mListener = listener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) return false;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mAnimationStartTime = NO_ANIMATION;
setPressed(true);
trackTouchEvent(event);
break;
case MotionEvent.ACTION_MOVE:
trackTouchEvent(event);
break;
case MotionEvent.ACTION_UP:
trackTouchEvent(event);
tryToSetSwitch(mPosition >= getAvailableLength() / 2);
setPressed(false);
break;
case MotionEvent.ACTION_CANCEL:
tryToSetSwitch(mSwitch);
setPressed(false);
break;
}
return true;
}
private void startParkingAnimation() {
mAnimationStartTime = AnimationUtils.currentAnimationTimeMillis();
mAnimationStartPosition = mPosition;
}
protected int getAvailableLength() {
return getHeight() - getPaddingTop() - getPaddingBottom()
- getDrawable().getIntrinsicHeight();
}
/** Returns the logical position of this switch. */
protected int trackTouch(MotionEvent event) {
return (int) event.getY() - getPaddingTop()
- (getDrawable().getIntrinsicHeight() / 2);
}
private void trackTouchEvent(MotionEvent event) {
final int available = getAvailableLength();
mPosition = trackTouch(event);
if (mPosition < 0) {
mPosition = 0;
} else if (mPosition > available) {
mPosition = available;
}
invalidate();
}
protected int getOffsetTopToDraw() {
return getPaddingTop() + mPosition;
}
protected int getOffsetLeftToDraw() {
return (getWidth() - getDrawable().getIntrinsicWidth()) / 2;
}
protected int getLogicalPosition() {
return mPosition;
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if ((drawable.getIntrinsicHeight() == 0)
|| (drawable.getIntrinsicWidth() == 0)) {
return; // nothing to draw (empty bounds)
}
final int available = getAvailableLength();
if (mAnimationStartTime != NO_ANIMATION) {
long time = AnimationUtils.currentAnimationTimeMillis();
int deltaTime = (int) (time - mAnimationStartTime);
mPosition = mAnimationStartPosition +
ANIMATION_SPEED * (mSwitch ? deltaTime : -deltaTime) / 1000;
if (mPosition < 0) mPosition = 0;
if (mPosition > available) mPosition = available;
boolean done = (mPosition == (mSwitch ? available : 0));
if (!done) {
invalidate();
} else {
mAnimationStartTime = NO_ANIMATION;
}
} else if (!isPressed()){
mPosition = mSwitch ? available : 0;
}
int saveCount = canvas.getSaveCount();
canvas.save();
canvas.translate(getOffsetLeftToDraw(), getOffsetTopToDraw());
drawable.draw(canvas);
canvas.restoreToCount(saveCount);
}
// Consume the touch events for the specified view.
public void addTouchView(View v) {
v.setOnTouchListener(this);
}
// This implements View.OnTouchListener so we intercept the touch events
// of the parent and pass them to ourselves.
public boolean onTouch(View v, MotionEvent event) {
// Adjust the x and y because they are relative to the parent.
event.offsetLocation(-getLeft(), -getTop());
onTouchEvent(event);
event.offsetLocation(getLeft(), getTop());
return true;
}
}
|