Skip to content

Commit

Permalink
Borders and markers improvement (moagrius#466)
Browse files Browse the repository at this point in the history
* Added support for extra borders around the tile image making it possible to scroll past the edge while zoomed in.

Added support for anchoring markers with both relative and absolute anchor positions.

* Added support for extra borders around the tile image making it possible to scroll past the edge while zoomed in.

Added support for anchoring markers with both relative and absolute anchor positions.

* Removed MarkerLayout anchor behaviour flags and replaced them with a system containing both relative and absolute anchors for each marker.

Renamed the setExtraBorder function in ZoomPanLayout to setImagePadding and also renamed the variables to fit. Image padding is now pre-calculated instead of having to calculate it during use.

TileView was updated to work with the MarkerLayout changes.

* Removed unused flags.
  • Loading branch information
Derpalus authored and moagrius committed Feb 19, 2018
1 parent b603c62 commit ace4ad0
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 10 deletions.
22 changes: 22 additions & 0 deletions tileview/src/main/java/com/qozix/tileview/TileView.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,28 @@ public View addMarker( View view, double x, double y, Float anchorX, Float ancho
);
}

/**
* Add a marker to the the TileView. The marker can be any View.
* No LayoutParams are required; the View will be laid out using WRAP_CONTENT for both width and height, and positioned based on the parameters.
*
* @param view View instance to be added to the TileView.
* @param x Relative x position the View instance should be positioned at.
* @param y Relative y position the View instance should be positioned at.
* @param relativeAnchorX The x-axis position of a marker will be offset by a number equal to the width of the marker multiplied by this value.
* @param relativeAnchorY The y-axis position of a marker will be offset by a number equal to the height of the marker multiplied by this value.
* @param absoluteAnchorX The x-axis position of a marker will be offset by this value.
* @param absoluteAnchorY The y-axis position of a marker will be offset by this value.
* @return The View instance added to the TileView.
*/
public View addMarker( View view, double x, double y, Float relativeAnchorX, Float relativeAnchorY, float absoluteAnchorX, float absoluteAnchorY ) {
return mMarkerLayout.addMarker( view,
mCoordinateTranslater.translateX( x ),
mCoordinateTranslater.translateY( y ),
relativeAnchorX, relativeAnchorY,
absoluteAnchorX, absoluteAnchorY
);
}

/**
* Removes a marker View from the TileView's view tree.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ public View addMarker( View view, int x, int y, Float aX, Float aY ) {
return addMarker( view, markerLayoutParams );
}

public View addMarker( View view, int x, int y, Float relativeX, Float relativeY, float absoluteX, float absoluteY ) {
ViewGroup.LayoutParams defaultLayoutParams = view.getLayoutParams();
LayoutParams markerLayoutParams = (defaultLayoutParams != null)
? generateLayoutParams(defaultLayoutParams)
: generateDefaultLayoutParams();
markerLayoutParams.x = x;
markerLayoutParams.y = y;
markerLayoutParams.anchorX = relativeX;
markerLayoutParams.anchorY = relativeY;
markerLayoutParams.absoluteAnchorX = absoluteX;
markerLayoutParams.absoluteAnchorY = absoluteY;
return addMarker( view, markerLayoutParams );
}

public View addMarker( View view, LayoutParams params ) {
addView( view, params );
return view;
Expand Down Expand Up @@ -120,15 +134,15 @@ protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec ) {
View child = getChildAt( i );
if( child.getVisibility() != GONE ) {
MarkerLayout.LayoutParams layoutParams = (MarkerLayout.LayoutParams) child.getLayoutParams();
// get anchor offsets
float widthMultiplier = (layoutParams.anchorX == null) ? mAnchorX : layoutParams.anchorX;
float heightMultiplier = (layoutParams.anchorY == null) ? mAnchorY : layoutParams.anchorY;
// actual sizes of children
int actualWidth = child.getMeasuredWidth();
int actualHeight = child.getMeasuredHeight();
// offset dimensions by anchor values
float widthOffset = actualWidth * widthMultiplier;
float heightOffset = actualHeight * heightMultiplier;
// get anchor offsets
float widthMultiplier = (layoutParams.anchorX == null) ? mAnchorX : layoutParams.anchorX;
float heightMultiplier = (layoutParams.anchorY == null) ? mAnchorY : layoutParams.anchorY;
// calculate combined anchor offsets
float widthOffset = actualWidth * widthMultiplier + layoutParams.absoluteAnchorX;
float heightOffset = actualHeight * heightMultiplier + layoutParams.absoluteAnchorY;
// get offset position
int scaledX = FloatMathHelper.scale( layoutParams.x, mScale );
int scaledY = FloatMathHelper.scale( layoutParams.y, mScale );
Expand Down Expand Up @@ -199,6 +213,13 @@ public static class LayoutParams extends ViewGroup.LayoutParams {
*/
public Float anchorY = null;

/**
* Similar to anchorX/Y, but for absolute offsets. This value is used in addition to anchorX/Y
* Set to 0 by default which means no absolute offset.
*/
public float absoluteAnchorX;
public float absoluteAnchorY;

private int mTop;
private int mLeft;
private int mBottom;
Expand Down Expand Up @@ -268,6 +289,28 @@ public LayoutParams( int width, int height, int left, int top, Float anchorLeft,
anchorY = anchorTop;
}

/**
* Creates a new set of layout parameters with the specified values.
*
* @param width Information about how wide the view wants to be. This should generally be WRAP_CONTENT or a fixed value.
* @param height Information about how tall the view wants to be. This should generally be WRAP_CONTENT or a fixed value.
* @param left Sets the absolute x value of the view's position in pixels.
* @param top Sets the absolute y value of the view's position in pixels.
* @param relativeAnchorLeft Sets the relative horizontal offset of the view from the left.
* @param relativeAnchorTop Sets the relative vertical offset of the view from the top.
* @param absoluteAnchorLeft Sets the absolute horizontal offset of the view.
* @param absoluteAnchorTop Sets the absolute horizontal offset of the view.
*/
public LayoutParams( int width, int height, int left, int top, Float relativeAnchorLeft, Float relativeAnchorTop, float absoluteAnchorLeft, float absoluteAnchorTop ) {
super( width, height );
x = left;
y = top;
anchorX = relativeAnchorLeft;
anchorY = relativeAnchorTop;
absoluteAnchorX = absoluteAnchorLeft;
absoluteAnchorY = absoluteAnchorTop;
}

}

public interface MarkerTapListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class ZoomPanLayout extends ViewGroup implements
private int mBaseHeight;
private int mScaledWidth;
private int mScaledHeight;
private int mImagePadding;
private int mScaledImagePadding;

private float mScale = 1;

Expand Down Expand Up @@ -196,6 +198,17 @@ public void setSize( int width, int height ) {
requestLayout();
}

/**
* Adds extra padding around the tiled image, making it possible to scroll past the end of
* the border even when zoomed in.
*
* @param padding Additional empty padding around the tiled image.
*/
public void setImagePadding(int padding) {
mImagePadding = padding;
recalculateImagePadding();
}

/**
* Returns the base (not scaled) width of the underlying composite image.
*
Expand Down Expand Up @@ -244,6 +257,7 @@ public void setScale( float scale ) {
mScale = scale;
updateScaledDimensions();
constrainScrollToLimits();
recalculateImagePadding();
onScaleChanged( scale, previous );
invalidate();
}
Expand Down Expand Up @@ -598,19 +612,23 @@ protected int getConstrainedScrollY( int y ) {
}

protected int getScrollLimitX() {
return mScaledWidth - getWidth();
return mScaledWidth - getWidth() + mScaledImagePadding;
}

protected int getScrollLimitY() {
return mScaledHeight - getHeight();
return mScaledHeight - getHeight() + mScaledImagePadding;
}

protected int getScrollMinX(){
return 0;
return -mScaledImagePadding;
}

protected int getScrollMinY(){
return 0;
return -mScaledImagePadding;
}

private void recalculateImagePadding() {
mScaledImagePadding = FloatMathHelper.scale(mImagePadding, mScale);
}

@Override
Expand Down

0 comments on commit ace4ad0

Please sign in to comment.