Android Open Source - TileView Marker Manager






From Project

Back to project page TileView.

License

The source code is released under:

MIT License

If you think the Android project TileView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.qozix.tileview.markers;
/*from  w  ww.j ava 2s  . c om*/
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.View;

import com.qozix.layouts.TranslationLayout;
import com.qozix.tileview.detail.DetailLevelEventListener;
import com.qozix.tileview.detail.DetailManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

/*
 * TODO: need to consolidate positioning logic - works as is, but does too many unnecessary and possibly messy calculations
 * should work with adding at runtime, in response to user event, sliding, etc. 
 */


public class MarkerManager extends TranslationLayout implements DetailLevelEventListener {

  private DetailManager detailManager;
  private HashMap<View, Rect> markerMap = new HashMap<View, Rect>();
  private ArrayList<MarkerEventListener> listeners = new ArrayList<MarkerEventListener>();

  public MarkerManager( Context context, DetailManager zm ) {
    super( context );
    detailManager = zm;
    detailManager.addDetailLevelEventListener( this );
  }

  public View addMarker( View v, int x, int y ){
    LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, x, y );
    return addMarker( v, lp );
  }

  public View addMarker( View v, int x, int y, float aX, float aY ) {
    LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, x, y, aX, aY );
    return addMarker( v, lp );
  }

  public View addMarker( View v, LayoutParams params ) {
    addView( v, params );
    markerMap.put( v, new Rect() );
    requestLayout();
    return v;
  }

  public void moveMarker( View v, int x, int y ) {
    LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, x, y );
    moveMarker( v, lp );
  }

  public void moveMarker( View v, int x, int y, float aX, float aY ) {
    LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, x, y, aX, aY );
    moveMarker( v, lp );
  }

  public void moveMarker( View v, LayoutParams params ) {
    if (markerMap.containsKey( v )) {
      v.setLayoutParams( params );
      requestLayout();
    }
  }

  public void removeMarker( View v ) {
    removeView( v );
    markerMap.remove( v );
  }

  public void addMarkerEventListener( MarkerEventListener listener ) {
    listeners.add( listener );
  }

  public void removeMarkerEventListener( MarkerEventListener listener ) {
    listeners.remove( listener );
  }

  private View getViewFromTap( int x, int y ) {
    Iterator<Entry<View, Rect>> iterator = markerMap.entrySet().iterator();
    while (iterator.hasNext()) {
          Entry<View, Rect> pairs = iterator.next();
          Rect rect = (Rect) pairs.getValue();
          if(rect.contains( x, y )){
            View view = (View) pairs.getKey();
            return view;
          }
    }
    return null;
  }

  public void processHit ( Point point ) {
    // fast-fail if no listeners
    if( listeners.isEmpty() ){
      return;
    }
    View view = getViewFromTap( point.x, point.y );
    if( view != null ) {
      for( MarkerEventListener listener : listeners ) {
        listener.onMarkerTap( view, point.x, point.y );
      }
    }
  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout( changed, l, t, r, b );
      for (int i = getChildCount() - 1; i >= 0; i--) {
        View child = getChildAt(i);
          if (child.getVisibility() != GONE) {
              LayoutParams lp = (LayoutParams) child.getLayoutParams();
              // get sizes
              int w = child.getMeasuredWidth();
              int h = child.getMeasuredHeight();
              // get offset position
              int scaledX = (int) (0.5 + (lp.x * scale));
              int scaledY = (int) (0.5 + (lp.y * scale));
              // user child's layout params anchor position if set, otherwise default to anchor position of layout
              float aX = (lp.anchorX == null) ? anchorX : lp.anchorX;
              float aY = (lp.anchorY == null) ? anchorY : lp.anchorY;
              // apply anchor offset to position
              int x = scaledX + (int) (w * aX);
              int y = scaledY + (int) (h * aY);
              // get and set the rect for the child
              Rect rect = markerMap.get( child );
              if( rect != null ) {
                rect.set( x, y, x + w, y + h );
              }
          }
      }
  }

  @Override
  public void onDetailLevelChanged() {

  }

  @Override
  public void onDetailScaleChanged( double scale ) {
    setScale( scale );
  }

}




Java Source Code List

com.qozix.animation.AnimationListener.java
com.qozix.animation.Animator.java
com.qozix.animation.TweenHandler.java
com.qozix.animation.TweenListener.java
com.qozix.animation.Tween.java
com.qozix.animation.easing.EasingEquation.java
com.qozix.animation.easing.Linear.java
com.qozix.animation.easing.Strong.java
com.qozix.layouts.AnchorLayout.java
com.qozix.layouts.FixedLayout.java
com.qozix.layouts.ScalingLayout.java
com.qozix.layouts.StaticLayout.java
com.qozix.layouts.TranslationLayout.java
com.qozix.layouts.ZoomPanLayout.java
com.qozix.os.AsyncTask.java
com.qozix.tileview.TileView.java
com.qozix.tileview.detail.DetailLevelEventListener.java
com.qozix.tileview.detail.DetailLevelPatternParserDefault.java
com.qozix.tileview.detail.DetailLevelPatternParser.java
com.qozix.tileview.detail.DetailLevelSet.java
com.qozix.tileview.detail.DetailLevelSetupListener.java
com.qozix.tileview.detail.DetailLevel.java
com.qozix.tileview.detail.DetailManager.java
com.qozix.tileview.geom.PositionManager.java
com.qozix.tileview.graphics.BitmapDecoderAssets.java
com.qozix.tileview.graphics.BitmapDecoderHttp.java
com.qozix.tileview.graphics.BitmapDecoder.java
com.qozix.tileview.hotspots.HotSpotEventListener.java
com.qozix.tileview.hotspots.HotSpotManager.java
com.qozix.tileview.hotspots.HotSpot.java
com.qozix.tileview.markers.CalloutManager.java
com.qozix.tileview.markers.MarkerEventListener.java
com.qozix.tileview.markers.MarkerManager.java
com.qozix.tileview.paths.DrawablePath.java
com.qozix.tileview.paths.PathHelper.java
com.qozix.tileview.paths.PathManager.java
com.qozix.tileview.samples.SampleManager.java
com.qozix.tileview.tiles.TileCache.java
com.qozix.tileview.tiles.TileManager.java
com.qozix.tileview.tiles.TileRenderHandler.java
com.qozix.tileview.tiles.TileRenderListener.java
com.qozix.tileview.tiles.TileRenderTask.java
com.qozix.tileview.tiles.TileTransitionListener.java
com.qozix.tileview.tiles.Tile.java
com.qozix.tileview.tiles.selector.TileSetSelectorByRange.java
com.qozix.tileview.tiles.selector.TileSetSelectorClosest.java
com.qozix.tileview.tiles.selector.TileSetSelectorMinimalUpScale.java
com.qozix.tileview.tiles.selector.TileSetSelector.java
com.qozix.utils.ViewCurator.java
com.qozix.widgets.Scroller.java