Class ZoomableImageView
- java.lang.Object
-
- de.kaffeemitkoffein.tinyweatherforecastgermany.ZoomableImageView
-
public class ZoomableImageView extends java.lang.Object
ZoomableImageView is a simple class that enhances an ImageView with the capability to zoom in and out.- Author:
- Pawel Dube
Requirements and limitations:
- the imageview has to be populated with a bitmap. Other content will not work.
- WIDTH AND HEIGHT OF THE IMAGEVIEW MUST BE SET TO "MATCH_PARENT". When set to "wrap_content", the view will shrink when the bitmap inside the view shrinks due to zooming!
Gestures:
this class catches the usual "zoom in" and "zoom out" gestures:- zoom in = move two pointers (e.g. two fingers) away from each other while touching the screen
- zoom out = move two pointers (e.g. two fingers) closer to each other while touching the screen
moving one pointer (e.g. one finger) while touching the screen moves the scaled bitmap within the imageview
Notes:
- this class always "consumes" the provided motionEvent, because the underlying ScaleGestureDetector does so. Therefore, there are NEVER any "unused" events after this class has been called. However, this is not a limitation, see example how to use.
How to use:
- create an instance of ZoomableImageView providing Context, the imageview and the bitmap,
- and optionally override "onGestureFinished" to catch the results once a gesture is done. If you do not need to further react after a zoom/move gesture, you can skip this.
- Register an OnTouchListerner and handle over the motionEvent to the ZoomableImageView.
That's all.
mapImageView = (ImageView) findViewById(R.id.map); zoomableImageView = new ZoomableImageView(getApplicationContext(),mapImageView,BitmapFactory.decodeResource(getResources(),R.drawable.germany_nc,null)){
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ZoomableImageView.SPRITEFIXPOINT
Static class that holds values how to fix a sprite to the underlying bitmap.
-
Field Summary
Fields Modifier and Type Field Description float
lastPressX
float
lastPressY
float
maxScaleRange
Default min.float
minScaleRange
Default max.float
scaleFactor
The current zoom:
1 = no zoom,
values below 1 mean the bitmap is zoomed in (enlarged)
values above 1 mean the bitmap is zoomed out (shrunk).
Values above 1 are discouraged and may lead to unexpected behaviour of the imageview/bitmap.ScaleGestureDetector
scaleGestureDetector
static java.lang.String
STATE_SCALEFACTOR
static java.lang.String
STATE_XFOCUS
static java.lang.String
STATE_YFOCUS
RectF
temporaryVisibleArea
Holds the borders of the visible area of the bimap.float
xFocus
The current focus (=center) visible.float
yFocus
de.kaffeemitkoffein.tinyweatherforecastgermany.ZoomableImageView.ZoomGestureListener
zoomGestureListener
-
Constructor Summary
Constructors Constructor Description ZoomableImageView(Context context, ImageView imageView, Bitmap bitmap_src)
Initializes this class.ZoomableImageView(Context context, ImageView imageView, Bitmap bitmap_src, boolean fillViewPort)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
addSpite(Bitmap bitmap, float x, float y, int fixpoint, java.lang.Float sf)
Sprite concept: a "sprite" is a small bitmap/icon that is fixed to an absolute position on top of the underlying bitmap.float
getRelativeXFocus()
Returns the current focus as a relative position, returns a value between 0 (=left border of the bitmap) and 1 (=right border of the bitmap).float
getRelativeYFocus()
Returns the current focus as a relative position, returns a value between 0 (=left top of the bitmap) and 1 (=bottom of the bitmap).float
getXFocus()
Returns the absolute focus x coordinate in pixels.float
getYFocus()
Returns the absolute focus y coordinate in pixels.void
onGestureFinished(float scaleFactor, float lastPressX, float lastPressY, float xFocus, float yFocus, float xFocusRelative, float yFocusRelative, RectF currentlyVisibleArea)
Always called when a gesture (zoom or movement) finished.
Override this method to do something once a zoom / movement inside the imageview finished.
The focus is the visible center coordinate.boolean
onTouchEvent(MotionEvent motionEvent)
Use this method to supply all motionEvents from the imageview to this class.void
redrawBitmap()
Redraws the bitmap inside the imageview.void
restoreZoomViewState(Bundle zoomViewState)
Bundle
saveZoomViewState()
void
setScaleRange(float min, float max)
Optional method to set the min and max scale range.
Example: setScaleRange(0.25,1); sets a maximum zoom in of 4x and a maximum zoom out to the original size.void
updateBitmap(Bitmap newbitmap)
Updates the bitmap with a new one.
-
-
-
Field Detail
-
scaleFactor
public float scaleFactor
The current zoom:
1 = no zoom,
values below 1 mean the bitmap is zoomed in (enlarged)
values above 1 mean the bitmap is zoomed out (shrunk).
Values above 1 are discouraged and may lead to unexpected behaviour of the imageview/bitmap.
Notice: this value is only accurate once a scale gesture finished. During a scale gesture, it holds the scale factor from the beginning of the gesture. In other words: it is updated at the end of the gesture, and not while a gesture is being performed.
Override onGestureFinished to get the results of a gesture.
-
xFocus
public float xFocus
The current focus (=center) visible. Both variables refer to the x/y coordinates of the underlying bitmap.
-
yFocus
public float yFocus
-
minScaleRange
public float minScaleRange
Default max. zoom factor, 0.25f = 4x zoom.
-
maxScaleRange
public float maxScaleRange
Default min. zoom factor, values above 1 are discouraged.
-
temporaryVisibleArea
public RectF temporaryVisibleArea
Holds the borders of the visible area of the bimap. May be null before first gestures.
-
lastPressX
public float lastPressX
-
lastPressY
public float lastPressY
-
zoomGestureListener
public de.kaffeemitkoffein.tinyweatherforecastgermany.ZoomableImageView.ZoomGestureListener zoomGestureListener
-
scaleGestureDetector
public ScaleGestureDetector scaleGestureDetector
-
STATE_SCALEFACTOR
public static final java.lang.String STATE_SCALEFACTOR
- See Also:
- Constant Field Values
-
STATE_XFOCUS
public static final java.lang.String STATE_XFOCUS
- See Also:
- Constant Field Values
-
STATE_YFOCUS
public static final java.lang.String STATE_YFOCUS
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ZoomableImageView
public ZoomableImageView(Context context, ImageView imageView, Bitmap bitmap_src)
Initializes this class. Values must not be null.- Parameters:
context
- the application contextimageView
- the imageviewbitmap_src
- the bitmap to be used in the imageview
-
ZoomableImageView
public ZoomableImageView(Context context, ImageView imageView, Bitmap bitmap_src, boolean fillViewPort)
-
-
Method Detail
-
setScaleRange
public void setScaleRange(float min, float max)
Optional method to set the min and max scale range.
Example: setScaleRange(0.25,1); sets a maximum zoom in of 4x and a maximum zoom out to the original size.- Parameters:
min
- the max. zoom, must be lower than max (e.g. 0.25 means a max. zoom of 4x).max
- the min. zoom, typically 1 (original size). Values above 1 are discouraged.
-
updateBitmap
public void updateBitmap(Bitmap newbitmap)
Updates the bitmap with a new one. If the old and new bitmap have the same width and height, the current zoom values and the focus are preserved.- Parameters:
newbitmap
- the new bitmap, must not be null
-
addSpite
public int addSpite(Bitmap bitmap, float x, float y, int fixpoint, java.lang.Float sf)
Sprite concept: a "sprite" is a small bitmap/icon that is fixed to an absolute position on top of the underlying bitmap. When the underlying bitmap gets zoomed in or out, the sprite does NOT zoom but always keeps the same size. It remains at the same (relative) position fixed to the underlying bitmap. Got it? Adds a sprite.- Parameters:
bitmap
- the bitmap holding the sprite. May not be null.x
- x-position (on the underlying bitmap) where to fix the spritey
- y-position (on the underlying bitmap) where to fix the spritefixpoint
- how to fix the sprite, see SPRITEFIXPOINT class- Returns:
- number of the sprite
-
onGestureFinished
public void onGestureFinished(float scaleFactor, float lastPressX, float lastPressY, float xFocus, float yFocus, float xFocusRelative, float yFocusRelative, RectF currentlyVisibleArea)
Always called when a gesture (zoom or movement) finished.
Override this method to do something once a zoom / movement inside the imageview finished.
The focus is the visible center coordinate.- Parameters:
scaleFactor
- the current scale factorlastPressX
- the absolute x of last pointer/touch, reference is the whole bitmaplastPressY
- the absolute y of last pointer/touch, reference is the whole bitmapxFocus
- the absolute x focus in pixels, reference is the whole bitmapyFocus
- the absolute y focus in pixels, reference is the whole bitmapxFocusRelative
- the relative x focus (0 <= xFocusRelative <= 1), reference is the whole bitmapyFocusRelative
- the relative y focus (0 <= yFocusRelative <= 1), reference is the whole bitmapcurrentlyVisibleArea
- rectangle holding the coordinates of the visible area in pixels
-
getRelativeXFocus
public float getRelativeXFocus()
Returns the current focus as a relative position, returns a value between 0 (=left border of the bitmap) and 1 (=right border of the bitmap). The position refers to the bitmap.
The focus is the visible center coordinate.- Returns:
- the relative x coordinate of the focus, 0 <= x <= 1
-
getRelativeYFocus
public float getRelativeYFocus()
Returns the current focus as a relative position, returns a value between 0 (=left top of the bitmap) and 1 (=bottom of the bitmap). The position refers to the bitmap.
The focus is the visible center coordinate.- Returns:
- the relative y coordinate of the focus, 0 <= y <= 1
-
getXFocus
public float getXFocus()
Returns the absolute focus x coordinate in pixels. The coordinate refers to the bitmap size in pixels.
The focus is the visible center coordinate.- Returns:
- absolute x coordinate
-
getYFocus
public float getYFocus()
Returns the absolute focus y coordinate in pixels. The coordinate refers to the bitmap size in pixels.
The focus is the visible center coordinate.- Returns:
- absolute y coordinate
-
onTouchEvent
public boolean onTouchEvent(MotionEvent motionEvent)
Use this method to supply all motionEvents from the imageview to this class. This is typically called from an OnTouchListener that is attached to the imageview. See example above.- Parameters:
motionEvent
-- Returns:
- if the motionEvent was used, always returns TRUE.
-
redrawBitmap
public void redrawBitmap()
Redraws the bitmap inside the imageview. Uses the currently set scale factor.
-
saveZoomViewState
public Bundle saveZoomViewState()
-
restoreZoomViewState
public void restoreZoomViewState(Bundle zoomViewState)
-
-