Skip to content

Commit

Permalink
Merge pull request #536 from yashx/master
Browse files Browse the repository at this point in the history
Added Scaling Marker Plugin #409
  • Loading branch information
moagrius authored Dec 15, 2019
2 parents f7d150a + 0a51b3f commit 906f9ba
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
10 changes: 9 additions & 1 deletion demo/src/main/java/com/moagrius/TileViewDemoAdvanced.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.moagrius.tileview.plugins.LowFidelityBackgroundPlugin;
import com.moagrius.tileview.plugins.MarkerPlugin;
import com.moagrius.tileview.plugins.PathPlugin;
import com.moagrius.tileview.plugins.ScalingMarkerPlugin;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -65,6 +66,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
.defineZoomLevel(1, "tiles/phi-500000-%1$d_%2$d.jpg")
.defineZoomLevel(2, "tiles/phi-250000-%1$d_%2$d.jpg")
.installPlugin(new MarkerPlugin(this))
.installPlugin(new ScalingMarkerPlugin(this))
.installPlugin(new InfoWindowPlugin(getInfoView()))
.installPlugin(new CoordinatePlugin(WEST, NORTH, EAST, SOUTH))
.installPlugin(new HotSpotPlugin())
Expand Down Expand Up @@ -99,6 +101,7 @@ private void onReady(TileView tileView) {
InfoWindowPlugin infoWindowPlugin = tileView.getPlugin(InfoWindowPlugin.class);
HotSpotPlugin hotSpotPlugin = tileView.getPlugin(HotSpotPlugin.class);
MarkerPlugin markerPlugin = tileView.getPlugin(MarkerPlugin.class);
ScalingMarkerPlugin scalingMarkerPlugin = tileView.getPlugin(ScalingMarkerPlugin.class);

// drop some markers, with info window expansions
String template = "Clicked marker at:\n%1$f\n%2$f";
Expand All @@ -113,14 +116,19 @@ private void onReady(TileView tileView) {
infoWindowPlugin.show(x, y, -0.5f, -1f);
};

int count = 0;
for (double[] coordinate : sites) {
int x = coordinatePlugin.longitudeToUnscaledX(coordinate[1]);
int y = coordinatePlugin.latitudeToUnscaledY(coordinate[0]);
ImageView marker = new ImageView(this);
marker.setTag(coordinate);
marker.setImageResource(R.drawable.marker);
marker.setOnClickListener(markerClickListener);
markerPlugin.addMarker(marker, x, y, -0.5f, -1f, 0, 0);
if(count%2==0)
markerPlugin.addMarker(marker, x, y, -0.5f, -1f, 0, 0);
else
scalingMarkerPlugin.addMarker(marker, x, y, -0.5f, -1f, 0, 0);
count++;
}
markerPlugin.refreshPositions();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// TODO: recycling
public class MarkerPlugin extends ViewGroup implements TileView.Plugin, TileView.Listener {

private float mScale = 1;
protected float mScale = 1;

public MarkerPlugin(@NonNull Context context) {
super(context);
Expand Down Expand Up @@ -47,7 +47,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}

private LayoutParams populateLayoutParams(View child) {
protected LayoutParams populateLayoutParams(View child) {
MarkerPlugin.LayoutParams layoutParams = (MarkerPlugin.LayoutParams) child.getLayoutParams();
if (child.getVisibility() != View.GONE) {
// actual sizes of children
Expand Down Expand Up @@ -124,10 +124,10 @@ public static class LayoutParams extends ViewGroup.LayoutParams {
public float absoluteAnchorX;
public float absoluteAnchorY;

private int mTop;
private int mLeft;
private int mBottom;
private int mRight;
protected int mTop;
protected int mLeft;
protected int mBottom;
protected int mRight;

public LayoutParams(int width, int height, int left, int top, float relativeAnchorLeft, float relativeAnchorTop, float absoluteAnchorLeft, float absoluteAnchorTop) {
super(width, height);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.moagrius.tileview.plugins;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;

public class ScalingMarkerPlugin extends MarkerPlugin {
private float mOriginalAtScale;

public ScalingMarkerPlugin(@NonNull Context context) {
this(context, 1f);
}

public ScalingMarkerPlugin(@NonNull Context context, float originalAtScale) {
super(context);
mOriginalAtScale = originalAtScale;
}

@Override
protected LayoutParams populateLayoutParams(View child) {
MarkerPlugin.LayoutParams layoutParams = (MarkerPlugin.LayoutParams) child.getLayoutParams();
if (child.getVisibility() != View.GONE) {
// actual sizes of children
int measuredWidth = (int) (child.getMeasuredWidth() / mOriginalAtScale * mScale);
int measuredHeight = (int) (child.getMeasuredHeight() / mOriginalAtScale * mScale);
// calculate combined anchor offsets
float widthOffset = measuredWidth * layoutParams.relativeAnchorX + layoutParams.absoluteAnchorX;
float heightOffset = measuredHeight * layoutParams.relativeAnchorY + layoutParams.absoluteAnchorY;
// get offset position
int scaledX = (int) (layoutParams.x * mScale);
int scaledY = (int) (layoutParams.y * mScale);
// save computed values
layoutParams.mLeft = (int) (scaledX + widthOffset);
layoutParams.mTop = (int) (scaledY + heightOffset);
layoutParams.mRight = layoutParams.mLeft + measuredWidth;
layoutParams.mBottom = layoutParams.mTop + measuredHeight;
}
return layoutParams;
}

@Override
public void onScaleChanged(float scale, float previous) {
super.onScaleChanged(scale, previous);
requestLayout();
}
}

0 comments on commit 906f9ba

Please sign in to comment.