Skip to content

Commit 91ffad5

Browse files
madebymozartyrezgui
authored andcommitted
[Sample] [Graphics] - Implementation of "Displaying UltraHDR (Glide)" (#65)
* initial draft implementation * [Sample] [Graphic] - Implementation of "Displaying UltraHDR (Glide)" * remove fresco for now * remove fresco for now * cleanup * switch back to office
1 parent e6067dc commit 91ffad5

File tree

8 files changed

+187
-2
lines changed

8 files changed

+187
-2
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ androidx-dynamicanimation = "androidx.dynamicanimation:dynamicanimation-ktx:1.0.
129129
androidx-media3-exoplayer = { module = "androidx.media3:media3-exoplayer", version.ref = "media3" }
130130
androidx-media3-ui = { module = "androidx.media3:media3-ui", version.ref = "media3" }
131131

132+
glide = "com.github.bumptech.glide:glide:4.15.1"
133+
132134
mdc = "com.google.android.material:material:1.9.0"
133135

134136
[plugins]

samples/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Shows how to create a GATT server and communicate with the GATT client
2222
Demonstrates how to implement data access auditing for your app to identify
2323
- [Displaying UltraHDR](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/DisplayingUltraHDR.kt):
2424
This sample demonstrates displaying an UltraHDR image.
25+
- [Displaying UltraHDR (Glide)](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/DisplayingUltraHDRUsingGlide.kt):
26+
This sample demonstrates using the Glide image loading library to detect the
2527
- [Downloadable Fonts](user-interface/text/src/main/java/com/example/platform/ui/text/DownloadableFonts.kt):
2628
Download fonts instead of bundling them in the app resources.
2729
- [Drag and Drop](user-interface/draganddrop/src/main/java/com/example/platform/ui/draganddrop/DragAndDrop.kt):

samples/graphics/ultrahdr/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ android {
2626
}
2727

2828
dependencies {
29-
// Add samples specific dependencies
29+
// Glide
30+
implementation(libs.glide)
3031
}
Loading
Loading

samples/graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/common/ColorModeControls.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ColorModeControls : LinearLayout, WindowObserver {
4343
* Android ViewBinding.
4444
*/
4545
private var _binding: ColorModeControlsBinding? = null
46-
private val binding get() = _binding!!
46+
val binding get() = _binding!!
4747

4848
/**
4949
* Reference to [Window]. This should come from the currently active activity.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.example.platform.graphics.ultrahdr.display
17+
18+
import android.content.pm.ActivityInfo
19+
import android.graphics.Bitmap
20+
import android.graphics.drawable.Drawable
21+
import android.net.Uri
22+
import android.os.Build.VERSION_CODES
23+
import android.os.Bundle
24+
import android.view.LayoutInflater
25+
import android.view.View
26+
import android.view.ViewGroup
27+
import androidx.annotation.RequiresApi
28+
import androidx.fragment.app.Fragment
29+
import com.bumptech.glide.Glide
30+
import com.bumptech.glide.request.target.CustomTarget
31+
import com.bumptech.glide.request.target.CustomViewTarget
32+
import com.bumptech.glide.request.transition.Transition
33+
import com.example.platform.graphics.ultrahdr.databinding.DisplayingUltrahdrUsingGlideBinding
34+
import com.google.android.catalog.framework.annotations.Sample
35+
36+
@Sample(
37+
name = "Displaying UltraHDR (Glide)",
38+
description = "This sample demonstrates using the Glide image loading library to detect the" +
39+
" presence of a gainmap to enable HDR mode when displaying an image using this library.",
40+
documentation = "https://github.com/bumptech/glide",
41+
tags = ["UltraHDR"],
42+
)
43+
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
44+
class DisplayingUltraHDRUsingGlide : Fragment() {
45+
/**
46+
* Android ViewBinding.
47+
*/
48+
private var _binding: DisplayingUltrahdrUsingGlideBinding? = null
49+
private val binding get() = _binding!!
50+
51+
/**
52+
* Using [Glide]s [CustomTarget] class, we can access the given [Bitmap] to check for the
53+
* presence of a gainmap, indicating that we should enable the HDR color mode.
54+
*
55+
* The same could be done with [CustomViewTarget] as well.
56+
*/
57+
private val target: CustomTarget<Bitmap> = object : CustomTarget<Bitmap>() {
58+
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
59+
binding.imageContainer.setImageBitmap(resource)
60+
61+
// Set color mode of the activity to the correct color mode.
62+
requireActivity().window.colorMode = when (resource.hasGainmap()) {
63+
true -> ActivityInfo.COLOR_MODE_HDR
64+
else -> ActivityInfo.COLOR_MODE_DEFAULT
65+
}
66+
}
67+
68+
override fun onLoadCleared(placeholder: Drawable?) {
69+
// clear resources if need be.
70+
}
71+
}
72+
73+
override fun onCreateView(
74+
inflater: LayoutInflater,
75+
container: ViewGroup?,
76+
savedInstanceState: Bundle?,
77+
): View {
78+
_binding = DisplayingUltrahdrUsingGlideBinding.inflate(inflater, container, false)
79+
return binding.root
80+
}
81+
82+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
83+
super.onViewCreated(view, savedInstanceState)
84+
binding.colorModeControls.setWindow(requireActivity().window)
85+
loadImageWithGlide(SDR_IMAGE)
86+
87+
// Disable color mode controls to demonstrate glide enabling hdr mode when a gainmap is
88+
// is detected.
89+
binding.colorModeControls.binding.ultrahdrColorModeSdr.isEnabled = false
90+
binding.colorModeControls.binding.ultrahdrColorModeHdr.isEnabled = false
91+
92+
binding.optionSdrImage.setOnClickListener { loadImageWithGlide(SDR_IMAGE) }
93+
binding.optionUltrahdrImage.setOnClickListener { loadImageWithGlide(ULTRAHDR_IMAGE) }
94+
}
95+
96+
/**
97+
* Load an image using [Glide].
98+
*/
99+
private fun loadImageWithGlide(path: String) =
100+
Glide.with(this)
101+
.asBitmap()
102+
.load(Uri.parse(path))
103+
.into(target)
104+
105+
override fun onDetach() {
106+
super.onDetach()
107+
binding.colorModeControls.detach()
108+
}
109+
110+
companion object {
111+
/**
112+
* Sample UltraHDR images paths
113+
*/
114+
private const val SDR_IMAGE = "file:///android_asset/sdr/night_highrise.jpg"
115+
private const val ULTRAHDR_IMAGE = "file:///android_asset/gainmaps/night_highrise.jpg"
116+
}
117+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
~ Copyright 2023 The Android Open Source Project
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ https://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
17+
xmlns:tools="http://schemas.android.com/tools"
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content"
20+
android:orientation="vertical">
21+
22+
<com.example.platform.graphics.ultrahdr.common.ColorModeControls
23+
android:id="@+id/color_mode_controls"
24+
android:layout_width="match_parent"
25+
android:layout_height="wrap_content" />
26+
27+
<LinearLayout
28+
android:layout_width="match_parent"
29+
android:layout_height="wrap_content"
30+
android:orientation="horizontal"
31+
android:padding="@dimen/ultrahdr_color_mode_current_mode_padding">
32+
33+
<Button
34+
android:id="@+id/option_sdr_image"
35+
android:layout_width="0dp"
36+
android:layout_height="wrap_content"
37+
android:layout_marginStart="@dimen/ultrahdr_color_mode_current_mode_padding"
38+
android:layout_marginEnd="@dimen/ultrahdr_color_mode_current_mode_padding"
39+
android:layout_weight="1"
40+
android:text="@string/visualizing_ultrahdr_gainmap_mode_title_jpeg"
41+
tools:ignore="ButtonStyle" />
42+
43+
<Button
44+
android:id="@+id/option_ultrahdr_image"
45+
android:layout_width="0dp"
46+
android:layout_height="wrap_content"
47+
android:layout_marginStart="@dimen/ultrahdr_color_mode_current_mode_padding"
48+
android:layout_marginEnd="@dimen/ultrahdr_color_mode_current_mode_padding"
49+
android:layout_weight="1"
50+
android:text="@string/visualizing_ultrahdr_gainmap_mode_title_ultrahdr"
51+
tools:ignore="ButtonStyle" />
52+
53+
</LinearLayout>
54+
55+
56+
<ImageView
57+
android:id="@+id/image_container"
58+
android:layout_width="wrap_content"
59+
android:layout_height="match_parent"
60+
android:layout_gravity="center_horizontal"
61+
android:contentDescription="@null" />
62+
63+
</LinearLayout>

0 commit comments

Comments
 (0)