summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Icon.java
blob: 2ee17c227d264c08e6412bf6fedc1eb6d844b30b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.mapbox.mapboxsdk.annotations;

import android.graphics.Bitmap;
import android.util.DisplayMetrics;

import java.nio.ByteBuffer;

/**
 * Icon is the visual representation of a Marker on a MapView.
 *
 * @see Marker
 * @see IconFactory
 */
public class Icon {

  private Bitmap mBitmap;
  private String mId;

  Icon(String id, Bitmap bitmap) {
    mId = id;
    mBitmap = bitmap;
  }

  /**
   * String identifier for this icon.
   *
   * @return String identifier for this icon.
   */
  public String getId() {
    return mId;
  }

  /**
   * Get the bitmap being used for this icon.
   *
   * @return The bitmap being used for the icon.
   */
  public Bitmap getBitmap() {
    if (mBitmap.getConfig() != Bitmap.Config.ARGB_8888) {
      mBitmap = mBitmap.copy(Bitmap.Config.ARGB_8888, false);
    }
    return mBitmap;
  }

  /**
   * Get the icon bitmap scale.
   * <p>
   * Requires the bitmap to be set before calling this method.
   * </p>
   *
   * @return the scale of the bitmap
   */
  public float getScale() {
    if (mBitmap == null) {
      throw new IllegalStateException("Required to set a Icon before calling getScale");
    }
    float density = mBitmap.getDensity();
    if (density == Bitmap.DENSITY_NONE) {
      density = DisplayMetrics.DENSITY_DEFAULT;
    }
    return density / DisplayMetrics.DENSITY_DEFAULT;
  }

  /**
   * Get the icon bitmap bytes.
   * <p>
   * Requires the bitmap to be set before calling this method.
   * </p>
   *
   * @return the bytes of the bitmap
   */
  public byte[] toBytes() {
    if (mBitmap == null) {
      throw new IllegalStateException("Required to set a Icon before calling toBytes");
    }
    ByteBuffer buffer = ByteBuffer.allocate(mBitmap.getRowBytes() * mBitmap.getHeight());
    mBitmap.copyPixelsToBuffer(buffer);
    return buffer.array();
  }

  /**
   * Compares this icon object with another icon and determines if they match.
   *
   * @param object Another iconi to compare with this object.
   * @return True if the icon being passed in matches this icon object. Else, false.
   */
  @Override
  public boolean equals(Object object) {
    if (this == object) {
      return true;
    }
    if (object == null || getClass() != object.getClass()) {
      return false;
    }

    Icon icon = (Icon) object;
    return mBitmap.equals(icon.mBitmap) && mId.equals(icon.mId);
  }

  /**
   * Gives an integer which can be used as the bucket number for storing elements of the set/map.
   * This bucket number is the address of the element inside the set/map. There's no guarantee
   * that this hash value will be consistent between different Java implementations, or even
   * between different execution runs of the same program.
   *
   * @return integer value you can use for storing element.
   */
  @Override
  public int hashCode() {
    int result = 0;
    if (mBitmap != null) {
      result = mBitmap.hashCode();
    }
    if (mId != null) {
      result = 31 * result + mId.hashCode();
    }
    return result;
  }
}