Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sample] [Graphics] - Implementation of "Displaying UltraHDR (Glide)" #65

Merged
merged 7 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ androidx-dynamicanimation = "androidx.dynamicanimation:dynamicanimation-ktx:1.0.
androidx-media3-exoplayer = { module = "androidx.media3:media3-exoplayer", version.ref = "media3" }
androidx-media3-ui = { module = "androidx.media3:media3-ui", version.ref = "media3" }

glide = "com.github.bumptech.glide:glide:4.15.1"

mdc = "com.google.android.material:material:1.9.0"

[plugins]
Expand Down
2 changes: 2 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Shows how to create a GATT server and communicate with the GATT client
Demonstrates how to implement data access auditing for your app to identify
- [Displaying UltraHDR](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/DisplayingUltraHDR.kt):
This sample demonstrates displaying an UltraHDR image.
- [Displaying UltraHDR (Glide)](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/DisplayingUltraHDRUsingGlide.kt):
This sample demonstrates using the Glide image loading library to detect the
- [Downloadable Fonts](user-interface/text/src/main/java/com/example/platform/ui/text/DownloadableFonts.kt):
Download fonts instead of bundling them in the app resources.
- [Drag and Drop](user-interface/draganddrop/src/main/java/com/example/platform/ui/draganddrop/DragAndDrop.kt):
Expand Down
3 changes: 2 additions & 1 deletion samples/graphics/ultrahdr/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ android {
}

dependencies {
// Add samples specific dependencies
// Glide
implementation(libs.glide)
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ColorModeControls : LinearLayout, WindowObserver {
* Android ViewBinding.
*/
private var _binding: ColorModeControlsBinding? = null
private val binding get() = _binding!!
val binding get() = _binding!!

/**
* Reference to [Window]. This should come from the currently active activity.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.platform.graphics.ultrahdr.display

import android.content.pm.ActivityInfo
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build.VERSION_CODES
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.CustomViewTarget
import com.bumptech.glide.request.transition.Transition
import com.example.platform.graphics.ultrahdr.databinding.DisplayingUltrahdrUsingGlideBinding
import com.google.android.catalog.framework.annotations.Sample

@Sample(
name = "Displaying UltraHDR (Glide)",
description = "This sample demonstrates using the Glide image loading library to detect the" +
" presence of a gainmap to enable HDR mode when displaying an image using this library.",
documentation = "https://github.com/bumptech/glide",
tags = ["UltraHDR"],
)
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
class DisplayingUltraHDRUsingGlide : Fragment() {
/**
* Android ViewBinding.
*/
private var _binding: DisplayingUltrahdrUsingGlideBinding? = null
private val binding get() = _binding!!

/**
* Using [Glide]s [CustomTarget] class, we can access the given [Bitmap] to check for the
* presence of a gainmap, indicating that we should enable the HDR color mode.
*
* The same could be done with [CustomViewTarget] as well.
*/
private val target: CustomTarget<Bitmap> = object : CustomTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
binding.imageContainer.setImageBitmap(resource)

// Set color mode of the activity to the correct color mode.
requireActivity().window.colorMode = when (resource.hasGainmap()) {
true -> ActivityInfo.COLOR_MODE_HDR
else -> ActivityInfo.COLOR_MODE_DEFAULT
}
}

override fun onLoadCleared(placeholder: Drawable?) {
// clear resources if need be.
}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
_binding = DisplayingUltrahdrUsingGlideBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.colorModeControls.setWindow(requireActivity().window)
loadImageWithGlide(SDR_IMAGE)

// Disable color mode controls to demonstrate glide enabling hdr mode when a gainmap is
// is detected.
binding.colorModeControls.binding.ultrahdrColorModeSdr.isEnabled = false
binding.colorModeControls.binding.ultrahdrColorModeHdr.isEnabled = false

binding.optionSdrImage.setOnClickListener { loadImageWithGlide(SDR_IMAGE) }
binding.optionUltrahdrImage.setOnClickListener { loadImageWithGlide(ULTRAHDR_IMAGE) }
}

/**
* Load an image using [Glide].
*/
private fun loadImageWithGlide(path: String) =
Glide.with(this)
.asBitmap()
.load(Uri.parse(path))
.into(target)

override fun onDetach() {
super.onDetach()
binding.colorModeControls.detach()
}

companion object {
/**
* Sample UltraHDR images paths
*/
private const val SDR_IMAGE = "file:///android_asset/sdr/night_highrise.jpg"
private const val ULTRAHDR_IMAGE = "file:///android_asset/gainmaps/night_highrise.jpg"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2023 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<com.example.platform.graphics.ultrahdr.common.ColorModeControls
android:id="@+id/color_mode_controls"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/ultrahdr_color_mode_current_mode_padding">

<Button
android:id="@+id/option_sdr_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/ultrahdr_color_mode_current_mode_padding"
android:layout_marginEnd="@dimen/ultrahdr_color_mode_current_mode_padding"
android:layout_weight="1"
android:text="@string/visualizing_ultrahdr_gainmap_mode_title_jpeg"
tools:ignore="ButtonStyle" />

<Button
android:id="@+id/option_ultrahdr_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/ultrahdr_color_mode_current_mode_padding"
android:layout_marginEnd="@dimen/ultrahdr_color_mode_current_mode_padding"
android:layout_weight="1"
android:text="@string/visualizing_ultrahdr_gainmap_mode_title_ultrahdr"
tools:ignore="ButtonStyle" />

</LinearLayout>


<ImageView
android:id="@+id/image_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:contentDescription="@null" />

</LinearLayout>