My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
* Copyright (C) 2010 Alfredo Morresi
*
* This file is part of RainbowLibs 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.
*/

/**
* Copyright (C) 2010 Alfredo Morresi
*
* This file is part of RainbowLibs project.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/

package it.rainbowbreeze.libs.ui;

import it.rainbowbreeze.libs.ui.MultiTouchController.MultiTouchObjectCanvas;
import it.rainbowbreeze.libs.ui.MultiTouchController.PointInfo;
import it.rainbowbreeze.libs.ui.MultiTouchController.PositionAndScale;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;

/**
* View that show an image and allow to zoom and pan on it
*
* @author Alfredo "Rainbowbreeze" Morresi
*
*/
public class RainbowZoomableImageView
extends View
implements MultiTouchObjectCanvas<RainbowZoomableImageView.Img>
{

//---------- Private fields
private MultiTouchController<RainbowZoomableImageView.Img> multiTouchController;
private Img mImgtoDisplay;

private static final int UI_MODE_ROTATE = 1;
private static final int UI_MODE_ANISOTROPIC_SCALE = 2;
private int mUIMode = UI_MODE_ROTATE;



//---------- Constructors
public RainbowZoomableImageView(Context context) {
super(context);
initVars(context);
}

/**
* Generally, this is the constructor called
*/
public RainbowZoomableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
initVars(context);
}

public RainbowZoomableImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initVars(context);
}




//---------- Public properties



//---------- Events

/**
* Pass touch events to the MT controller
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
return multiTouchController.onTouchEvent(event);
}


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (null != mImgtoDisplay) mImgtoDisplay.draw(canvas);
}


//---------- Public methods
/**
* Get the image that is under the single-touch point, or return null (canceling the drag op) if none
*/
public Img getDraggableObjectAtPoint(PointInfo touchPoint) {
return mImgtoDisplay;
}

/**
* Get the current position and scale of the selected image.
* Called whenever a drag starts or is reset.
*/
public void getPositionAndScale(Img img, PositionAndScale objPosAndScaleOut) {
objPosAndScaleOut.set(
img.getCenterX(), img.getCenterY(),
(mUIMode & UI_MODE_ANISOTROPIC_SCALE) == 0,
(img.getScaleX() + img.getScaleY()) / 2,
(mUIMode & UI_MODE_ANISOTROPIC_SCALE) != 0,
img.getScaleX(), img.getScaleY(),
(mUIMode & UI_MODE_ROTATE) != 0,
img.getAngle());
}

/**
* Set the position and scale of the dragged/stretched image.รน
*/
public boolean setPositionAndScale(Img obj, PositionAndScale newObjPosAndScale, PointInfo touchPoint) {
boolean ok = false;
if (null != mImgtoDisplay)
ok = mImgtoDisplay.setPos(newObjPosAndScale);
if (ok) invalidate();
return ok;
}

/**
* Select an object for dragging.
* Called whenever an object is found to be under the point (non-null is returned by getDraggableObjectAtPoint())
* and a drag operation is starting.
* Called with null when drag op ends.
*/
public void selectObject(Img obj, PointInfo touchPoint) {
invalidate();
}


/**
* Increment scale factor of the image
* @param increment
*/
public void incrementScale(float increment) {
if (null != mImgtoDisplay) {
mImgtoDisplay.incrementScaleFactor(increment);
invalidate();
}
}


/**
* Assign the image using a resource
* @param res
* @param resId
*/
public void assignImage(Resources res, int resId) {
mImgtoDisplay = new Img(res, resId);
}

public void assignImage(Resources res, Bitmap bitmap) {
mImgtoDisplay = new Img(res, bitmap);
}


/**
* Called by activity's onPause() method to free memory used for loading the images
*/
public void onPause() {
if (null != mImgtoDisplay) mImgtoDisplay.onPause();
}

/**
* Called by activity's onResume() method to load the images
*/
public void onResume(Resources res) {
if (null != mImgtoDisplay) mImgtoDisplay.onResume(res);
}

/**
* Initialize local variables
* @param context
*/
private void initVars(Context context) {
//initialize only once
if (null == multiTouchController) {
multiTouchController = new MultiTouchController<RainbowZoomableImageView.Img>(this);
//default black background
setBackgroundColor(Color.BLACK);
}
}


//---------- Private methods




//---------- Private classe

//Class from some of Luke Hutchison's sample code
class Img {
//---------- Private fields
private static final float SCREEN_MARGIN = 100;

private int resId;
private Drawable drawable;
private boolean firstLoad;
private int width, height, displayWidth, displayHeight;
private float centerX, centerY, scaleX, scaleY, angle;
private float minX, maxX, minY, maxY;
final boolean mCanDiscardResource;



//---------- Constructors
public Img(Resources res, int resId) {
this(res);
this.resId = resId;
}

public Img(Resources res, Bitmap bitmap) {
this(res);
drawable = new BitmapDrawable(bitmap);
}

private Img(Resources res) {
this.firstLoad = true;
getMetrics(res);
//cannot discard the resource when the app is in pause
mCanDiscardResource = false;
}


//---------- Public properties
public Drawable getDrawable() {
return drawable;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public float getCenterX() {
return centerX;
}

public float getCenterY() {
return centerY;
}

public float getScaleX() {
return scaleX;
}

public float getScaleY() {
return scaleY;
}

public float getAngle() {
return angle;
}

// FIXME: these need to be updated for rotation
public float getMinX() {
return minX;
}

public float getMaxX() {
return maxX;
}

public float getMinY() {
return minY;
}

public float getMaxY() {
return maxY;
}



//---------- Public methods

/** Called by activity's onResume() method to load the images */
public void onResume(Resources res) {
getMetrics(res);
if (mCanDiscardResource) {
this.drawable = res.getDrawable(resId);
}
this.width = drawable.getIntrinsicWidth();
this.height = drawable.getIntrinsicHeight();
float centerX, centerY, scaleX, scaleY;
if (firstLoad) {
// cx = SCREEN_MARGIN + (float) (Math.random() * (displayWidth - 2 * SCREEN_MARGIN));
// cy = SCREEN_MARGIN + (float) (Math.random() * (displayHeight - 2 * SCREEN_MARGIN));
// float sc = (float) (Math.max(displayWidth, displayHeight) / (float) Math.max(width, height) * Math.random() * 0.3 + 0.2);
//float sc = 1;
//center the image with max size available
centerX = displayWidth / 2.0f;
centerY = displayHeight / 2.0f;
float scale = Math.min(displayWidth / (float) width, displayHeight / (float) height);
scaleX = scaleY = scale;
firstLoad = false;
} else {
// Reuse position and scale information if it is available
// FIXME this doesn't actually work because the whole activity is torn down and re-created on rotate
centerX = this.centerX;
centerY = this.centerY;
scaleX = this.scaleX;
scaleY = this.scaleY;
// Make sure the image is not off the screen after a screen rotation
if (this.maxX < SCREEN_MARGIN)
centerX = SCREEN_MARGIN;
else if (this.minX > displayWidth - SCREEN_MARGIN)
centerX = displayWidth - SCREEN_MARGIN;
if (this.maxY > SCREEN_MARGIN)
centerY = SCREEN_MARGIN;
else if (this.minY > displayHeight - SCREEN_MARGIN)
centerY = displayHeight - SCREEN_MARGIN;
}
setPos(centerX, centerY, scaleX, scaleY, 0.0f);
}

/** Called by activity's onPause() method to free memory used for loading the images */
public void onPause() {
if (mCanDiscardResource) {
this.drawable = null;
}
}

/** Set the position and scale of an image in screen coordinates */
public boolean setPos(PositionAndScale newImgPosAndScale) {
return setPos(
newImgPosAndScale.getXOff(),
newImgPosAndScale.getYOff(),
(mUIMode & UI_MODE_ANISOTROPIC_SCALE) != 0
? newImgPosAndScale.getScaleX()
: newImgPosAndScale.getScale(),
(mUIMode & UI_MODE_ANISOTROPIC_SCALE) != 0
? newImgPosAndScale.getScaleY()
: newImgPosAndScale.getScale(),
newImgPosAndScale.getAngle()
);
// FIXME: anisotropic scaling jumps when axis-snapping
// FIXME: affine-ize
// return setPos(newImgPosAndScale.getXOff(), newImgPosAndScale.getYOff(), newImgPosAndScale.getScaleAnisotropicX(),
// newImgPosAndScale.getScaleAnisotropicY(), 0.0f);
}

/** Return whether or not the given screen coords are inside this image */
public boolean containsPoint(float scrnX, float scrnY) {
// FIXME: need to correctly account for image rotation
return (scrnX >= minX && scrnX <= maxX && scrnY >= minY && scrnY <= maxY);
}

/**
* Draw the image
* @param canvas
*/
public void draw(Canvas canvas) {
canvas.save();
float dx = (maxX + minX) / 2;
float dy = (maxY + minY) / 2;
drawable.setBounds((int) minX, (int) minY, (int) maxX, (int) maxY);
canvas.translate(dx, dy);
//fix image rotation
//canvas.rotate(angle * 180.0f / (float) Math.PI);
canvas.translate(-dx, -dy);
drawable.draw(canvas);
canvas.restore();
}

/**
* Add an increment to the scale scale factor of the image.
* If the increment is negative, the image will be decreased
* @param increment
*/
public void incrementScaleFactor(float increment) {
float newScaleX = this.scaleX + increment > 0 ? this.scaleX + increment : this.scaleX;
float newScaleY = this.scaleY + increment > 0 ? this.scaleY + increment : this.scaleY;
setPos(this.centerX, this.centerY, newScaleX, newScaleY, this.angle);
}


//---------- Private methods

/** Set the position and scale of an image in screen coordinates */
private boolean setPos(float centerX, float centerY, float scaleX, float scaleY, float angle) {
float ws = (width / 2) * scaleX;
float hs = (height / 2) * scaleY;
float newMinX = centerX - ws;
float newMinY = centerY - hs;
float newMaxX = centerX + ws;
float newMaxY = centerY + hs;
if (newMinX > displayWidth - SCREEN_MARGIN
|| newMaxX < SCREEN_MARGIN
|| newMinY > displayHeight - SCREEN_MARGIN
|| newMaxY < SCREEN_MARGIN)
return false;
this.centerX = centerX;
this.centerY = centerY;
this.scaleX = scaleX;
this.scaleY = scaleY;
this.angle = angle;
this.minX = newMinX;
this.minY = newMinY;
this.maxX = newMaxX;
this.maxY = newMaxY;
return true;
}

private void getMetrics(Resources res) {
DisplayMetrics metrics = res.getDisplayMetrics();
// The DisplayMetrics don't seem to always be updated on screen rotate, so we hard code a portrait
// screen orientation for the non-rotated screen here...
// this.displayWidth = metrics.widthPixels;
// this.displayHeight = metrics.heightPixels;
this.displayWidth =
res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
? Math.max(metrics.widthPixels, metrics.heightPixels)
: Math.min(metrics.widthPixels, metrics.heightPixels);
this.displayHeight =
res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
? Math.min(metrics.widthPixels, metrics.heightPixels)
: Math.max(metrics.widthPixels, metrics.heightPixels);
}
}

}

Change log

r136 by alfredo.morresi on Dec 17, 2011   Diff
small bugfixes
Go to: 
Project members, sign in to write a code review

Older revisions

r39 by alfredo.morresi on Oct 7, 2010   Diff
fixed some bugs
r37 by alfredo.morresi on Oct 6, 2010   Diff
set control on zoomimage
r36 by alfredo.morresi on Oct 6, 2010   Diff
changed name of libraries
All revisions of this file

File info

Size: 13336 bytes, 460 lines

File properties

svn:mime-type
text/plain
Powered by Google Project Hosting