feat: implement opacity to control visibility of subtitles (#3583)
* feat: implement opacity to control visibility of subtitles implemented per discussion on https://github.com/react-native-video/react-native-video/issues/3579 updated docs and linked onTextTrackDataChanged to the subtitle style to clarify intent on how to control visibility. * chore: update type so that we use a union of 0 1 vs any number * chore: run ktlint to get rid of white spaces * feat: add ability to have range of numbers for opacity; while, 0 will still not render the subtitles. added util function for safeGetFloat updated types * feat: add ability to suppress subtitles with opacity 0 add data structure for subtitle styles for extensibility * chore: run yarn check-ios * chore: update documentation to clarify differences between android and ios * Update android/src/main/java/com/brentvatne/exoplayer/ExoPlayerView.java Co-authored-by: Olivier Bouillet <62574056+freeboub@users.noreply.github.com> --------- Co-authored-by: Olivier Bouillet <62574056+freeboub@users.noreply.github.com>
This commit is contained in:
@@ -17,6 +17,8 @@ class SubtitleStyle private constructor() {
|
||||
private set
|
||||
var paddingBottom = 0
|
||||
private set
|
||||
var opacity = 1f
|
||||
private set
|
||||
|
||||
companion object {
|
||||
private const val PROP_FONT_SIZE_TRACK = "fontSize"
|
||||
@@ -24,6 +26,7 @@ class SubtitleStyle private constructor() {
|
||||
private const val PROP_PADDING_TOP = "paddingTop"
|
||||
private const val PROP_PADDING_LEFT = "paddingLeft"
|
||||
private const val PROP_PADDING_RIGHT = "paddingRight"
|
||||
private const val PROP_OPACITY = "opacity"
|
||||
|
||||
@JvmStatic
|
||||
fun parse(src: ReadableMap?): SubtitleStyle {
|
||||
@@ -33,6 +36,7 @@ class SubtitleStyle private constructor() {
|
||||
subtitleStyle.paddingTop = ReactBridgeUtils.safeGetInt(src, PROP_PADDING_TOP, 0)
|
||||
subtitleStyle.paddingLeft = ReactBridgeUtils.safeGetInt(src, PROP_PADDING_LEFT, 0)
|
||||
subtitleStyle.paddingRight = ReactBridgeUtils.safeGetInt(src, PROP_PADDING_RIGHT, 0)
|
||||
subtitleStyle.opacity = ReactBridgeUtils.safeGetFloat(src, PROP_OPACITY, 1f)
|
||||
return subtitleStyle
|
||||
}
|
||||
}
|
||||
|
@@ -66,6 +66,14 @@ object ReactBridgeUtils {
|
||||
return safeGetDouble(map, key, 0.0)
|
||||
}
|
||||
|
||||
@JvmStatic fun safeGetFloat(map: ReadableMap?, key: String?, fallback: Float): Float {
|
||||
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDouble(key).toFloat() else fallback
|
||||
}
|
||||
|
||||
@JvmStatic fun safeGetFloat(map: ReadableMap?, key: String?): Float {
|
||||
return safeGetFloat(map, key, 0.0f)
|
||||
}
|
||||
|
||||
/**
|
||||
* toStringMap converts a [ReadableMap] into a HashMap.
|
||||
*
|
||||
|
@@ -117,6 +117,13 @@ public final class ExoPlayerView extends FrameLayout implements AdViewProvider {
|
||||
subtitleLayout.setFixedTextSize(TypedValue.COMPLEX_UNIT_SP, style.getFontSize());
|
||||
}
|
||||
subtitleLayout.setPadding(style.getPaddingLeft(), style.getPaddingTop(), style.getPaddingRight(), style.getPaddingBottom());
|
||||
if (style.getOpacity() != 0) {
|
||||
subtitleLayout.setAlpha(style.getOpacity());
|
||||
subtitleLayout.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
subtitleLayout.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setShutterColor(Integer color) {
|
||||
|
Reference in New Issue
Block a user