forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MultiSourceHelper.java -> MultiSourceHelper.kt (facebook#43776)
Summary: Pull Request resolved: facebook#43776 ## Changelog: [Internal] - As in the title. Reviewed By: alanleedev Differential Revision: D55640126
- Loading branch information
1 parent
b52c7aa
commit 7c3ac9a
Showing
4 changed files
with
99 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 0 additions & 105 deletions
105
...ve/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/MultiSourceHelper.java
This file was deleted.
Oops, something went wrong.
89 changes: 89 additions & 0 deletions
89
...tive/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/MultiSourceHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.views.imagehelper | ||
|
||
import com.facebook.imagepipeline.core.ImagePipelineFactory | ||
|
||
/** Helper class for dealing with multisource images. */ | ||
public object MultiSourceHelper { | ||
@JvmStatic | ||
public fun getBestSourceForSize( | ||
width: Int, | ||
height: Int, | ||
sources: List<ImageSource> | ||
): MultiSourceResult = getBestSourceForSize(width, height, sources, 1.0) | ||
|
||
/** | ||
* Chooses the image source with the size closest to the target image size. | ||
* | ||
* @param width the width of the view that will be used to display this image | ||
* @param height the height of the view that will be used to display this image | ||
* @param sources the list of potential image sources to choose from | ||
* @param multiplier the area of the view will be multiplied by this number before calculating the | ||
* best source; this is useful if the image will be displayed bigger than the view (e.g. zoomed) | ||
*/ | ||
@JvmStatic | ||
public fun getBestSourceForSize( | ||
width: Int, | ||
height: Int, | ||
sources: List<ImageSource>, | ||
multiplier: Double | ||
): MultiSourceResult { | ||
// no sources | ||
if (sources.isEmpty()) { | ||
return MultiSourceResult(null, null) | ||
} | ||
|
||
// single source | ||
if (sources.size == 1) { | ||
return MultiSourceResult(sources[0], null) | ||
} | ||
|
||
// For multiple sources, we first need the view's size in order to determine the best source to | ||
// load. If we haven't been measured yet, return null and wait for onSizeChanged. | ||
if (width <= 0 || height <= 0) { | ||
return MultiSourceResult(null, null) | ||
} | ||
val imagePipeline = ImagePipelineFactory.getInstance().imagePipeline | ||
var best: ImageSource? = null | ||
var bestCached: ImageSource? = null | ||
val viewArea = width * height * multiplier | ||
var bestPrecision = Double.MAX_VALUE | ||
var bestCachePrecision = Double.MAX_VALUE | ||
for (source in sources) { | ||
val precision = Math.abs(1.0 - source.size / viewArea) | ||
if (precision < bestPrecision) { | ||
bestPrecision = precision | ||
best = source | ||
} | ||
if (precision < bestCachePrecision && | ||
(imagePipeline.isInBitmapMemoryCache(source.uri) || | ||
imagePipeline.isInDiskCacheSync(source.uri))) { | ||
bestCachePrecision = precision | ||
bestCached = source | ||
} | ||
} | ||
if (bestCached != null && best != null && bestCached.source == best.source) { | ||
bestCached = null | ||
} | ||
return MultiSourceResult(best, bestCached) | ||
} | ||
|
||
public class MultiSourceResult( | ||
/** | ||
* Get the best result overall (closest in size to the view's size). Can be null if there were | ||
* no sources to choose from, or if there were more than 1 sources but width/height were 0. | ||
*/ | ||
@JvmField public val bestResult: ImageSource?, | ||
/** | ||
* Get the best result (closest in size to the view's size) that is also in cache. If this | ||
* would be the same as the source from [.getBestResult], this will return `null` instead. | ||
*/ | ||
@JvmField public val bestResultInCache: ImageSource? | ||
) | ||
} |