Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for RenderPostProcessor + memory optimizations #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions clustering/src/main/java/com/huawei/clustering/Cluster.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.huawei.clustering;

import com.huawei.hms.maps.model.LatLng;

import androidx.annotation.NonNull;

import java.util.List;
Expand All @@ -17,6 +19,8 @@ public class Cluster<T extends ClusterItem> {
private final double south;
private final double east;

private final LatLng latLng;

Cluster(double latitude, double longitude, @NonNull List<T> items,
double north, double west, double south, double east) {
this.latitude = latitude;
Expand All @@ -26,6 +30,8 @@ public class Cluster<T extends ClusterItem> {
this.west = west;
this.south = south;
this.east = east;

this.latLng = new LatLng(latitude, longitude);
}

/**
Expand All @@ -46,6 +52,15 @@ public double getLongitude() {
return longitude;
}

/**
* The LatLng based on latitude and longitude.
*
* @return the latLng of the cluster
*/
public LatLng getLatLng() {
return latLng;
}

/**
* The items contained in the cluster.
*
Expand Down
10 changes: 10 additions & 0 deletions clustering/src/main/java/com/huawei/clustering/ClusterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public void setIconGenerator(@NonNull IconGenerator<T> iconGenerator) {
mRenderer.setIconGenerator(iconGenerator);
}

/**
* Sets a custom render post processor thus replacing the default one.
*
* @param renderPostProcessor the render post processor that's used for modifying the marker being rendered
*/
public void setRenderPostProcessor(@NonNull RenderPostProcessor<T> renderPostProcessor) {
Preconditions.checkNotNull(renderPostProcessor);
mRenderer.setRenderPostProcessor(renderPostProcessor);
}

/**
* Sets a callback that's invoked when a cluster or a cluster item is clicked.
*
Expand Down
22 changes: 12 additions & 10 deletions clustering/src/main/java/com/huawei/clustering/ClusterRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Map;

class ClusterRenderer<T extends ClusterItem> implements HuaweiMap.OnMarkerClickListener {
private static final LatLngTypeEvaluator latLngTypeEvaluator = new LatLngTypeEvaluator();
private static final FastOutSlowInInterpolator fastOutSlowInInterpolator = new FastOutSlowInInterpolator();

private static final int BACKGROUND_MARKER_Z_INDEX = 0;

Expand Down Expand Up @@ -52,7 +54,7 @@ public boolean onMarkerClick(Marker marker) {
if (markerTag instanceof Cluster) {
//noinspection unchecked
Cluster<T> cluster = (Cluster<T>) marker.getTag();
//noinspection ConstantConditions

List<T> clusterItems = cluster.getItems();

if (mCallbacks != null) {
Expand Down Expand Up @@ -106,8 +108,7 @@ void render(@NonNull List<Cluster<T>> clusters) {
Cluster<T> parentCluster = findParentCluster(mClusters, clusterToRemove.getLatitude(),
clusterToRemove.getLongitude());
if (parentCluster != null) {
animateMarkerToLocation(markerToRemove, new LatLng(parentCluster.getLatitude(),
parentCluster.getLongitude()), true);
animateMarkerToLocation(markerToRemove, parentCluster.getLatLng(), true);
} else {
markerToRemove.remove();
}
Expand All @@ -123,20 +124,19 @@ void render(@NonNull List<Cluster<T>> clusters) {
String markerTitle = getMarkerTitle(clusterToAdd);
String markerSnippet = getMarkerSnippet(clusterToAdd);

Cluster parentCluster = findParentCluster(clustersToRemove, clusterToAdd.getLatitude(),
Cluster<T> parentCluster = findParentCluster(clustersToRemove, clusterToAdd.getLatitude(),
clusterToAdd.getLongitude());
if (parentCluster != null) {
markerToAdd = mHuaweiMap.addMarker(new MarkerOptions()
.position(new LatLng(parentCluster.getLatitude(), parentCluster.getLongitude()))
.position(parentCluster.getLatLng())
.icon(markerIcon)
.title(markerTitle)
.snippet(markerSnippet)
.zIndex(FOREGROUND_MARKER_Z_INDEX));
animateMarkerToLocation(markerToAdd,
new LatLng(clusterToAdd.getLatitude(), clusterToAdd.getLongitude()), false);
animateMarkerToLocation(markerToAdd, clusterToAdd.getLatLng(), false);
} else {
markerToAdd = mHuaweiMap.addMarker(new MarkerOptions()
.position(new LatLng(clusterToAdd.getLatitude(), clusterToAdd.getLongitude()))
.position(clusterToAdd.getLatLng())
.icon(markerIcon)
.title(markerTitle)
.snippet(markerSnippet)
Expand All @@ -147,6 +147,8 @@ void render(@NonNull List<Cluster<T>> clusters) {
markerToAdd.setTag(clusterToAdd);

mMarkers.put(clusterToAdd, new MarkerState(markerToAdd));

mRenderPostProcessor.postProcess(markerToAdd, clusterToAdd);
}
}

Expand Down Expand Up @@ -199,8 +201,8 @@ private Cluster<T> findParentCluster(@NonNull List<Cluster<T>> clusters,
private void animateMarkerToLocation(@NonNull final Marker marker, @NonNull LatLng targetLocation,
final boolean removeAfter) {
ObjectAnimator objectAnimator = ObjectAnimator.ofObject(marker, "position",
new LatLngTypeEvaluator(), targetLocation);
objectAnimator.setInterpolator(new FastOutSlowInInterpolator());
latLngTypeEvaluator, targetLocation);
objectAnimator.setInterpolator(fastOutSlowInInterpolator);
objectAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@

import com.huawei.hms.maps.model.Marker;

public class DefaultRenderPostProcessor<T extends ClusterItem> implements RenderPostProcessor<T> {
import androidx.annotation.NonNull;

/**
* A default render post processor that does nothing.
*
* @param <T> type of the the cluster item
*/
public class DefaultRenderPostProcessor<T extends ClusterItem> implements RenderPostProcessor<T> {
@Override
public boolean postProcess(Marker marker, Cluster<T> cluster) {
return false;
public void postProcess(@NonNull Marker marker, @NonNull Cluster<T> cluster) {
// do nothing by default
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@

import com.huawei.hms.maps.model.Marker;

/**
* Allows for modifying the marker that is being rendered.
*
* @param <T> type of the cluster item
*/
public interface RenderPostProcessor<T extends ClusterItem> {

boolean postProcess(@NonNull Marker marker, @NonNull Cluster<T> cluster);
/**
* Allows for modifying the marker that is being rendered.
*
* @param marker the marker
* @param cluster the cluster
*/
void postProcess(@NonNull Marker marker, @NonNull Cluster<T> cluster);
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
rootProject.name='ClusterDemo'
include ':singleCluster', 'multipleClusters', ':clusterings'
include ':singleCluster', 'multipleClusters'
include ':clustering'