-
Notifications
You must be signed in to change notification settings - Fork 344
[Sample] [Graphics] [Compose] UltraHDR image rendering in Compose. #89
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8c33283
Render an UltraHDR image in Compose. Added two views (ImageView (Regu…
MayuriKhinvasara b653bcb
Merge branch 'main' of https://github.com/android/platform-samples in…
MayuriKhinvasara faa2c01
(1) Loading asset and image decoding on a background thread (2) Add a…
MayuriKhinvasara aba8a85
Removed dependencies which are not required
MayuriKhinvasara 51a109b
Merge branch 'main' into ultrahdr_compose_display_view
MayuriKhinvasara 11c18c4
Update DisplayUltraHDRScreen logic to be Compose friendly (#116)
yrezgui a38e743
Merge branch 'main' into ultrahdr_compose_display_view
yrezgui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
139 changes: 139 additions & 0 deletions
139
...src/main/java/com/example/platform/graphics/ultrahdr/display/DisplayingUltraHDRCompose.kt
This file contains hidden or 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,139 @@ | ||
/* | ||
* 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.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import android.os.Build | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ImageView | ||
import androidx.activity.ComponentActivity | ||
import androidx.annotation.RequiresApi | ||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.foundation.layout.wrapContentHeight | ||
import androidx.compose.material3.Divider | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.asImageBitmap | ||
import androidx.compose.ui.platform.ComposeView | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import androidx.core.content.ContextCompat | ||
import androidx.core.graphics.drawable.toBitmap | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.lifecycleScope | ||
import com.example.platform.graphics.ultrahdr.R | ||
import com.example.platform.graphics.ultrahdr.common.ColorModeControls | ||
import com.example.platform.graphics.ultrahdr.databinding.DisplayingUltrahdrBinding | ||
import com.google.android.catalog.framework.annotations.Sample | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import androidx.compose.ui.viewinterop.AndroidView as AndroidView | ||
|
||
@RequiresApi(34) | ||
@Sample( | ||
name = "Displaying UltraHDR (Compose)", | ||
description = "This sample demonstrates displaying an UltraHDR image in a Compose View and an Android View", | ||
documentation = "https://developer.android.com/guide/topics/media/hdr-image-format", | ||
tags = ["UltraHDR", "Compose"], | ||
) | ||
|
||
@Composable | ||
fun RenderComposeView() { | ||
|
||
val tempBitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.RGB_565) | ||
var bitmap by remember { mutableStateOf(tempBitmap) } | ||
val context = LocalContext.current | ||
|
||
// Load asset and bitmap on background thread | ||
LaunchedEffect(Unit) { | ||
|
||
// Same bitmap is used to load image in an image view and image (Compose) | ||
bitmap = withContext(Dispatchers.IO) { | ||
val ultraHdrImage = "gainmaps/night_highrise.jpg" | ||
val stream = context.assets.open(ultraHdrImage) | ||
BitmapFactory.decodeStream(stream) | ||
} | ||
} | ||
|
||
Column( | ||
modifier = Modifier.fillMaxWidth(), | ||
verticalArrangement = Arrangement.SpaceEvenly, | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
) { | ||
|
||
// Add SDR/HDR Color mode controls | ||
AndroidView( | ||
modifier = Modifier.fillMaxWidth(), | ||
factory = { | ||
ColorModeControls(it) | ||
.apply { | ||
setWindow((it as ComponentActivity).window) | ||
} | ||
}, | ||
) | ||
|
||
// Render UltraHDR in a Compose Image | ||
Text(text = "Image (Compose)") | ||
Image( | ||
bitmap = bitmap.asImageBitmap(), | ||
contentDescription = null, | ||
modifier = Modifier.weight(1f), | ||
) | ||
|
||
Divider( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(vertical = 4.dp), | ||
thickness = 1.dp, | ||
color = MaterialTheme.colorScheme.primary, | ||
) | ||
|
||
// Render UltraHDR in View (ImageView) | ||
Text(text = "ImageView (Android View)") | ||
AndroidView( | ||
modifier = Modifier.weight(1f), | ||
factory = { | ||
ImageView(it).apply { | ||
setImageBitmap(bitmap) | ||
} | ||
}, | ||
update = { | ||
it.setImageBitmap(bitmap) | ||
}, | ||
) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following convention across other Compose samples