-
Notifications
You must be signed in to change notification settings - Fork 789
Expand file tree
/
Copy pathImageViewActivity.java
More file actions
282 lines (248 loc) · 7.69 KB
/
Copy pathImageViewActivity.java
File metadata and controls
282 lines (248 loc) · 7.69 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright (C) 2010-2014 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.android.fbreader.image;
import android.app.Activity;
import android.content.Intent;
import android.graphics.*;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.*;
import org.geometerplus.zlibrary.core.image.*;
import org.geometerplus.zlibrary.core.util.ZLColor;
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidLibrary;
import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageData;
import org.geometerplus.zlibrary.ui.android.util.ZLAndroidColorUtil;
import org.geometerplus.android.fbreader.OrientationUtil;
public class ImageViewActivity extends Activity {
public static final String BACKGROUND_COLOR_KEY = "bgColor";
private Bitmap myBitmap;
private ZLColor myBgColor;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
final ZLAndroidLibrary library = (ZLAndroidLibrary)ZLAndroidLibrary.Instance();
final boolean showStatusBar = library.ShowStatusBarOption.getValue();
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
showStatusBar ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN
);
Thread.setDefaultUncaughtExceptionHandler(
new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this)
);
setContentView(new ImageView());
final Intent intent = getIntent();
myBgColor = new ZLColor(
intent.getIntExtra(BACKGROUND_COLOR_KEY, new ZLColor(127, 127, 127).intValue())
);
final Uri uri = intent.getData();
if (ZLFileImage.SCHEME.equals(uri.getScheme())) {
new AsyncTask<Uri, Void, Boolean>() {
protected Boolean doInBackground(Uri... args) {
Uri uri = args[0];
final ZLFileImage image = ZLFileImage.byUrlPath(uri.getPath());
if (image == null) {
return false;
}
try {
final ZLImageData imageData = ZLImageManager.Instance().getImageData(image);
myBitmap = ((ZLAndroidImageData)imageData).getFullSizeBitmap();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
protected void onPostExecute(Boolean result) {
if(!result) {
// TODO: error message (?)
finish();
}
}
}.execute(uri);
} else {
// TODO: error message (?)
finish();
}
}
@Override
protected void onStart() {
super.onStart();
OrientationUtil.setOrientation(this, getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
OrientationUtil.setOrientation(this, intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (myBitmap != null) {
myBitmap.recycle();
}
myBitmap = null;
}
private class ImageView extends View {
private final Paint myPaint = new Paint();
private volatile int myDx = 0;
private volatile int myDy = 0;
private volatile float myZoomFactor = 1.0f;
ImageView() {
super(ImageViewActivity.this);
}
@Override
protected void onDraw(final Canvas canvas) {
myPaint.setColor(ZLAndroidColorUtil.rgb(myBgColor));
final int w = getWidth();
final int h = getHeight();
canvas.drawRect(0, 0, w, h, myPaint);
if (myBitmap == null || myBitmap.isRecycled()) {
return;
}
final int bw = (int)(myBitmap.getWidth() * myZoomFactor);
final int bh = (int)(myBitmap.getHeight() * myZoomFactor);
final Rect src = new Rect(0, 0, (int)(w / myZoomFactor), (int)(h / myZoomFactor));
final Rect dst = new Rect(0, 0, w, h);
if (bw <= w) {
src.left = 0;
src.right = myBitmap.getWidth();
dst.left = (w - bw) / 2;
dst.right = dst.left + bw;
} else {
final int bWidth = myBitmap.getWidth();
final int pWidth = (int)(w / myZoomFactor);
src.left = Math.min(bWidth - pWidth, Math.max((bWidth - pWidth) / 2 - myDx, 0));
src.right += src.left;
}
if (bh <= h) {
src.top = 0;
src.bottom = myBitmap.getHeight();
dst.top = (h - bh) / 2;
dst.bottom = dst.top + bh;
} else {
final int bHeight = myBitmap.getHeight();
final int pHeight = (int)(h / myZoomFactor);
src.top = Math.min(bHeight - pHeight, Math.max((bHeight - pHeight) / 2 - myDy, 0));
src.bottom += src.top;
}
canvas.drawBitmap(myBitmap, src, dst, myPaint);
}
private void shift(int dx, int dy) {
if (myBitmap == null || myBitmap.isRecycled()) {
return;
}
final int w = (int)(getWidth() / myZoomFactor);
final int h = (int)(getHeight() / myZoomFactor);
final int bw = myBitmap.getWidth();
final int bh = myBitmap.getHeight();
final int newDx, newDy;
if (w < bw) {
final int delta = (bw - w) / 2;
newDx = Math.max(-delta, Math.min(delta, myDx + dx));
} else {
newDx = myDx;
}
if (h < bh) {
final int delta = (bh - h) / 2;
newDy = Math.max(-delta, Math.min(delta, myDy + dy));
} else {
newDy = myDy;
}
if (newDx != myDx || newDy != myDy) {
myDx = newDx;
myDy = newDy;
postInvalidate();
}
}
private boolean myMotionControl;
private int mySavedX;
private int mySavedY;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getPointerCount()) {
case 1:
return onSingleTouchEvent(event);
case 2:
return onDoubleTouchEvent(event);
default:
return false;
}
}
private boolean onSingleTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
myMotionControl = false;
break;
case MotionEvent.ACTION_DOWN:
myMotionControl = true;
mySavedX = x;
mySavedY = y;
break;
case MotionEvent.ACTION_MOVE:
if (myMotionControl) {
shift(
(int)((x - mySavedX) / myZoomFactor),
(int)((y - mySavedY) / myZoomFactor)
);
}
myMotionControl = true;
mySavedX = x;
mySavedY = y;
break;
}
return true;
}
private float myStartPinchDistance2 = -1;
private float myStartZoomFactor;
private boolean onDoubleTouchEvent(MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_UP:
myStartPinchDistance2 = -1;
break;
case MotionEvent.ACTION_POINTER_DOWN:
{
final float diffX = event.getX(0) - event.getX(1);
final float diffY = event.getY(0) - event.getY(1);
myStartPinchDistance2 = Math.max(diffX * diffX + diffY * diffY, 10f);
myStartZoomFactor = myZoomFactor;
break;
}
case MotionEvent.ACTION_MOVE:
{
final float diffX = event.getX(0) - event.getX(1);
final float diffY = event.getY(0) - event.getY(1);
final float distance2 = Math.max(diffX * diffX + diffY * diffY, 10f);
if (myStartPinchDistance2 < 0) {
myStartPinchDistance2 = distance2;
myStartZoomFactor = myZoomFactor;
} else {
myZoomFactor = myStartZoomFactor * FloatMath.sqrt(distance2 / myStartPinchDistance2);
postInvalidate();
}
}
break;
}
return true;
}
}
}