Java Code Examples for androidx.collection.LongSparseArray#put()

The following examples show how to use androidx.collection.LongSparseArray#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SkinCompatDrawableManager.java    From Android-skin-support with MIT License 6 votes vote down vote up
private boolean addDrawableToCache(@NonNull final Context context, final long key,
                                   @NonNull final Drawable drawable) {
    final ConstantState cs = drawable.getConstantState();
    if (cs != null) {
        synchronized (mDrawableCacheLock) {
            LongSparseArray<WeakReference<ConstantState>> cache = mDrawableCaches.get(context);
            if (cache == null) {
                cache = new LongSparseArray<>();
                mDrawableCaches.put(context, cache);
            }
            cache.put(key, new WeakReference<ConstantState>(cs));
        }
        return true;
    }
    return false;
}
 
Example 2
Source File: LottieCompositionMoshiParser.java    From lottie-android with Apache License 2.0 6 votes vote down vote up
private static void parseLayers(JsonReader reader, LottieComposition composition,
                                List<Layer> layers, LongSparseArray<Layer> layerMap) throws IOException {
  int imageCount = 0;
  reader.beginArray();
  while (reader.hasNext()) {
    Layer layer = LayerParser.parse(reader, composition);
    if (layer.getLayerType() == Layer.LayerType.IMAGE) {
      imageCount++;
    }
    layers.add(layer);
    layerMap.put(layer.getId(), layer);

    if (imageCount > 4) {
      Logger.warning("You have " + imageCount + " images. Lottie should primarily be " +
          "used with shapes. If you are using Adobe Illustrator, convert the Illustrator layers" +
          " to shape layers.");
    }
  }
  reader.endArray();
}
 
Example 3
Source File: LottieCompositionParser.java    From lottie-android with Apache License 2.0 6 votes vote down vote up
private static void parseLayers(JsonReader reader, LottieComposition composition,
                                List<Layer> layers, LongSparseArray<Layer> layerMap) throws IOException {
  int imageCount = 0;
  reader.beginArray();
  while (reader.hasNext()) {
    Layer layer = LayerParser.parse(reader, composition);
    if (layer.getLayerType() == Layer.LayerType.IMAGE) {
      imageCount++;
    }
    layers.add(layer);
    layerMap.put(layer.getId(), layer);

    if (imageCount > 4) {
      Logger.warning("You have " + imageCount + " images. Lottie should primarily be " +
          "used with shapes. If you are using Adobe Illustrator, convert the Illustrator layers" +
          " to shape layers.");
    }
  }
  reader.endArray();
}
 
Example 4
Source File: GridBasedAlgorithm.java    From android-maps-utils with Apache License 2.0 6 votes vote down vote up
@Override
public Set<? extends Cluster<T>> getClusters(float zoom) {
    long numCells = (long) Math.ceil(256 * Math.pow(2, zoom) / mGridSize);
    SphericalMercatorProjection proj = new SphericalMercatorProjection(numCells);

    HashSet<Cluster<T>> clusters = new HashSet<Cluster<T>>();
    LongSparseArray<StaticCluster<T>> sparseArray = new LongSparseArray<StaticCluster<T>>();

    synchronized (mItems) {
        for (T item : mItems) {
            Point p = proj.toPoint(item.getPosition());

            long coord = getCoord(numCells, p.x, p.y);

            StaticCluster<T> cluster = sparseArray.get(coord);
            if (cluster == null) {
                cluster = new StaticCluster<T>(proj.toLatLng(new Point(Math.floor(p.x) + .5, Math.floor(p.y) + .5)));
                sparseArray.put(coord, cluster);
                clusters.add(cluster);
            }
            cluster.add(item);
        }
    }

    return clusters;
}
 
Example 5
Source File: LollipopDrawablesCompat.java    From Carbon with Apache License 2.0 5 votes vote down vote up
private static void cacheDrawable(TypedValue value, Resources resources, Resources.Theme theme, boolean isColorDrawable, long key, Drawable drawable, LongSparseArray<WeakReference<Drawable.ConstantState>> caches) {

        Drawable.ConstantState cs = drawable.getConstantState();
        if (cs == null) {
            return;
        }

        synchronized (mAccessLock) {
            caches.put(key, new WeakReference<>(cs));
        }
    }
 
Example 6
Source File: LayoutState.java    From litho with Apache License 2.0 4 votes vote down vote up
private static void addLayoutOutputIdToPositionsMap(
    LongSparseArray outputsIdToPositionMap, LayoutOutput layoutOutput, int position) {
  if (outputsIdToPositionMap != null) {
    outputsIdToPositionMap.put(layoutOutput.getId(), position);
  }
}
 
Example 7
Source File: LottieCompositionMoshiParser.java    From lottie-android with Apache License 2.0 4 votes vote down vote up
private static void parseAssets(JsonReader reader, LottieComposition composition,
    Map<String, List<Layer>> precomps, Map<String, LottieImageAsset> images) throws IOException {
  reader.beginArray();
  while (reader.hasNext()) {
    String id = null;
    // For precomps
    List<Layer> layers = new ArrayList<>();
    LongSparseArray<Layer> layerMap = new LongSparseArray<>();
    // For images
    int width = 0;
    int height = 0;
    String imageFileName = null;
    String relativeFolder = null;
    reader.beginObject();
    while (reader.hasNext()) {
      switch (reader.selectName(ASSETS_NAMES)) {
        case 0:
          id = reader.nextString();
          break;
        case 1:
          reader.beginArray();
          while (reader.hasNext()) {
            Layer layer = LayerParser.parse(reader, composition);
            layerMap.put(layer.getId(), layer);
            layers.add(layer);
          }
          reader.endArray();
          break;
        case 2:
          width = reader.nextInt();
          break;
        case 3:
          height = reader.nextInt();
          break;
        case 4:
          imageFileName = reader.nextString();
          break;
        case 5:
          relativeFolder = reader.nextString();
          break;
        default:
          reader.skipName();
          reader.skipValue();
      }
    }
    reader.endObject();
    if (imageFileName != null) {
      LottieImageAsset image =
          new LottieImageAsset(width, height, id, imageFileName, relativeFolder);
      images.put(image.getId(), image);
    } else {
      precomps.put(id, layers);
    }
  }
  reader.endArray();
}
 
Example 8
Source File: CompositionLayer.java    From lottie-android with Apache License 2.0 4 votes vote down vote up
public CompositionLayer(LottieDrawable lottieDrawable, Layer layerModel, List<Layer> layerModels,
    LottieComposition composition) {
  super(lottieDrawable, layerModel);

  AnimatableFloatValue timeRemapping = layerModel.getTimeRemapping();
  if (timeRemapping != null) {
    this.timeRemapping = timeRemapping.createAnimation();
    addAnimation(this.timeRemapping);
    //noinspection ConstantConditions
    this.timeRemapping.addUpdateListener(this);
  } else {
    this.timeRemapping = null;
  }

  LongSparseArray<BaseLayer> layerMap =
      new LongSparseArray<>(composition.getLayers().size());

  BaseLayer mattedLayer = null;
  for (int i = layerModels.size() - 1; i >= 0; i--) {
    Layer lm = layerModels.get(i);
    BaseLayer layer = BaseLayer.forModel(lm, lottieDrawable, composition);
    if (layer == null) {
      continue;
    }
    layerMap.put(layer.getLayerModel().getId(), layer);
    if (mattedLayer != null) {
      mattedLayer.setMatteLayer(layer);
      mattedLayer = null;
    } else {
      layers.add(0, layer);
      switch (lm.getMatteType()) {
        case ADD:
        case INVERT:
          mattedLayer = layer;
          break;
      }
    }
  }

  for (int i = 0; i < layerMap.size(); i++) {
    long key = layerMap.keyAt(i);
    BaseLayer layerView = layerMap.get(key);
    // This shouldn't happen but it appears as if sometimes on pre-lollipop devices when
    // compiled with d8, layerView is null sometimes.
    // https://github.com/airbnb/lottie-android/issues/524
    if (layerView == null) {
      continue;
    }
    BaseLayer parentLayer = layerMap.get(layerView.getLayerModel().getParentId());
    if (parentLayer != null) {
      layerView.setParentLayer(parentLayer);
    }
  }
}
 
Example 9
Source File: HeatmapTileProvider.java    From android-maps-utils with Apache License 2.0 4 votes vote down vote up
/**
 * Calculate a reasonable maximum intensity value to map to maximum color intensity
 *
 * @param points    Collection of LatLngs to put into buckets
 * @param bounds    Bucket boundaries
 * @param radius    radius of convolution
 * @param screenDim larger dimension of screen in pixels (for scale)
 * @return Approximate max value
 */
static double getMaxValue(Collection<WeightedLatLng> points, Bounds bounds, int radius,
                          int screenDim) {
    // Approximate scale as if entire heatmap is on the screen
    // ie scale dimensions to larger of width or height (screenDim)
    double minX = bounds.minX;
    double maxX = bounds.maxX;
    double minY = bounds.minY;
    double maxY = bounds.maxY;
    double boundsDim = (maxX - minX > maxY - minY) ? maxX - minX : maxY - minY;

    // Number of buckets: have diameter sized buckets
    int nBuckets = (int) (screenDim / (2 * radius) + 0.5);
    // Scaling factor to convert width in terms of point distance, to which bucket
    double scale = nBuckets / boundsDim;

    // Make buckets
    // Use a sparse array - use LongSparseArray just in case
    LongSparseArray<LongSparseArray<Double>> buckets = new LongSparseArray<LongSparseArray<Double>>();
    //double[][] buckets = new double[nBuckets][nBuckets];

    // Assign into buckets + find max value as we go along
    double x, y;
    double max = 0;
    for (WeightedLatLng l : points) {
        x = l.getPoint().x;
        y = l.getPoint().y;

        int xBucket = (int) ((x - minX) * scale);
        int yBucket = (int) ((y - minY) * scale);

        // Check if x bucket exists, if not make it
        LongSparseArray<Double> column = buckets.get(xBucket);
        if (column == null) {
            column = new LongSparseArray<Double>();
            buckets.put(xBucket, column);
        }
        // Check if there is already a y value there
        Double value = column.get(yBucket);
        if (value == null) {
            value = 0.0;
        }
        value += l.getIntensity();
        // Yes, do need to update it, despite it being a Double.
        column.put(yBucket, value);

        if (value > max) max = value;
    }

    return max;
}