-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDCryptPictureViewer.java
More file actions
315 lines (261 loc) · 11.8 KB
/
DCryptPictureViewer.java
File metadata and controls
315 lines (261 loc) · 11.8 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package com.example.jay_pc.dcrypt;
/**
* Created by Jay-pc on 3/10/2017.
*/
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import static java.lang.StrictMath.sqrt;
public class DCryptPictureViewer extends Activity implements OnTouchListener {
private static final String TAG = "DCrypt - Touch";
@SuppressWarnings("unused")
private static final float MIN_ZOOM = 1f, MAX_ZOOM = 1f;
//private static final android.R.attr R =null ;
// These matrices will be used to scale points of the image
private Matrix matrix = new Matrix();
private Matrix savedMatrix = new Matrix();
//private Matrix originalMatrix = new Matrix();
// The 3 states (events) which the user is trying to perform
private static final int NONE = 0;
private static final int DRAG = 1;
private static final int ZOOM = 2;
private int mode = NONE;
// these PointF objects are used to record the point(s) the user is touching
private PointF start = new PointF();
private PointF mid = new PointF();
private float oldDist = 1f;
String currentPicId, prevPicId, nextPicId;
int picPosition;
private ImageView dCryptImageView = null;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picviewer);
dCryptImageView = (ImageView) findViewById(R.id.dcryptImageView);
Button prevButton = (Button) findViewById(R.id.prevButton);
Button nextButton = (Button) findViewById(R.id.nextButton);
Button returnButton = (Button) findViewById(R.id.returnButton);
Intent i = getIntent();
currentPicId = i.getStringExtra("currentPicId");
prevPicId = i.getStringExtra("prevPicId");
nextPicId = i.getStringExtra("nextPicId");
picPosition = i.getIntExtra("picPosition", -1);
currentPicId = (DCryptImageAdapter.getCurrItemId(picPosition)).getAbsolutePath();
Log.i("DCrypt", "currentPicId" + currentPicId);
Drawable image;
if (currentPicId == null) {
image = getResources().getDrawable(R.drawable.ic_launcher);
} else {
image = Drawable.createFromPath(currentPicId);
}
dCryptImageView.setImageDrawable(image);
dCryptImageView.setOnTouchListener(this);
dCryptImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
scaleProperly(image.getMinimumHeight(), image.getMinimumWidth());
//originalMatrix = dCryptImageView.getImageMatrix();
//dCryptImageView.setScaleType(ImageView.ScaleType.MATRIX);
//dCryptImageView.setImageMatrix(originalMatrix);
returnButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
}
});
prevButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Drawable imagePrev;
currentPicId = prevPicId;
if (prevPicId == null) {
imagePrev = getResources().getDrawable(R.drawable.ic_launcher);
} else {
imagePrev = Drawable.createFromPath(currentPicId);
}
dCryptImageView.setImageDrawable(imagePrev);
dCryptImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//dCryptImageView.setImageMatrix(originalMatrix);
picPosition--;
assert (DCryptImageAdapter.getPrevItemId(picPosition)) != null;
prevPicId = (DCryptImageAdapter.getPrevItemId(picPosition)).getAbsolutePath();
assert (DCryptImageAdapter.getNextItemId(picPosition)) != null;
nextPicId = (DCryptImageAdapter.getNextItemId(picPosition)).getAbsolutePath();
}
});
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Drawable imageNext;
currentPicId = nextPicId;
if (currentPicId == null) {
imageNext = getResources().getDrawable(R.drawable.ic_launcher);
} else {
imageNext = Drawable.createFromPath(currentPicId);
}
dCryptImageView.setImageDrawable(imageNext);
dCryptImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//dCryptImageView.setImageMatrix(originalMatrix);
picPosition++;
nextPicId = (DCryptImageAdapter.getNextItemId(picPosition)).getAbsolutePath();
prevPicId = (DCryptImageAdapter.getPrevItemId(picPosition)).getAbsolutePath();
}
});
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
private void scaleProperly(int height, int width) {
int measuredWidth = 0;
int measuredHeight = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
measuredWidth = size.x;
measuredHeight = size.y;
} else {
Display d = w.getDefaultDisplay();
measuredWidth = d.getWidth();
measuredHeight = d.getHeight();
}
//float scale = measuredWidth / width;
Log.i("DCrypt", "Height=" + measuredHeight + "; Width=" + measuredWidth);
//originalMatrix = new Matrix();
//originalMatrix.postScale(0.25f, 0.25f);
matrix.postScale(0.20f, 0.20f);
}
@SuppressLint("FloatMath")
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) sqrt(x * x + y * y);
}
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
view.setScaleType(ImageView.ScaleType.MATRIX);
float scale;
toLogger(event);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: // first finger down only
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG"); // write to LogCat
mode = DRAG;
break;
case MotionEvent.ACTION_UP: // first finger lifted
case MotionEvent.ACTION_POINTER_UP: // second finger lifted
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_POINTER_DOWN: // first and second finger down
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 5f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
// create the transformation in the matrix of points
} else if (mode == ZOOM) {
// pinch zooming
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 5f) {
matrix.set(savedMatrix);
scale = newDist / oldDist; // setting the scaling of the
// matrix...if scale > 1 means
// zoom in...if scale < 1 means
// zoom out
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix); // display the transformation on screen
return true; // indicate event was handled
}
@SuppressWarnings("deprecation")
private void toLogger(MotionEvent event) {
String names[] = {"DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?"};
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_").append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN ||
actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")");
}
sb.append("[");
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#").append(i);
sb.append("(pid ").append(event.getPointerId(i));
sb.append(")=").append((int) event.getX(i));
sb.append(",").append((int) event.getY(i));
if (i + 1 < event.getPointerCount()) {
sb.append(";");
}
}
sb.append("]");
Log.d("Touch Events ---------", sb.toString());
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW,
"DCryptPictureViewer Page",
Uri.parse("http://host/path"),
Uri.parse("android-app://com.example.jay_pc.dcrypt/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW,
"DCryptPictureViewer Page",
Uri.parse("http://host/path"),
Uri.parse("android-app://com.example.jay_pc.dcrypt/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}