diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index b97126ce..b95714f8 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -131,6 +131,7 @@ androidx-dynamicanimation = "androidx.dynamicanimation:dynamicanimation-ktx:1.0. androidx-media3-common = { module = "androidx.media3:media3-common", version.ref = "media3" } androidx-media3-exoplayer = { module = "androidx.media3:media3-exoplayer", version.ref = "media3" } androidx-media3-ui = { module = "androidx.media3:media3-ui", version.ref = "media3" } +androidx-palette = { module = "androidx.palette:palette", version = "1.0.0" } glide = "com.github.bumptech.glide:glide:4.15.1" diff --git a/samples/README.md b/samples/README.md index dfc7edab..39b3d580 100644 --- a/samples/README.md +++ b/samples/README.md @@ -26,12 +26,12 @@ Demonstrates how to implement data access auditing for your app to identify 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 -- [Editing UltraHDR](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/edit/EditingUltraHDR.kt): -This sample demonstrates editing an UltraHDR image. Spatial edit operations like crop, rotate, scale are supported. - [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): Demonstrates basic Drag and Drop functionality. +- [Editing UltraHDR](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/edit/EditingUltraHDR.kt): +This sample demonstrates editing an UltraHDR image and the resulting gainmap as well. Spatial edit operations like crop, rotate, scale are supported - [Find devices sample](connectivity/bluetooth/ble/src/main/java/com/example/platform/connectivity/bluetooth/ble/FindBLEDevicesSample.kt): This example will demonstrate how to scanning for Low Energy Devices - [Haptics - 1. Vibration effects](user-interface/haptics/src/main/java/com/example/platform/ui/haptics/Haptics.kt): @@ -68,6 +68,8 @@ Basic motion example using referenced ConstraintLayout files Shows the recommended flow to request multiple RELATED runtime permissions - [Package Visibility](privacy/data/src/main/java/com/example/platform/privacy/data/PackageVisibility.kt): A sample that showcase how the package visibility queries affects the available packages +- [Palette API](graphics/pallete/src/main/java/com/example/platform/graphics/pallete/PalleteSampleScreen.kt): +This sample demonstrates using an Palette API. - [PdfRenderer](graphics/pdf/src/main/java/com/example/platform/graphics/pdf/PdfRenderer.kt): Demonstrates how to use PdfRenderer to display PDF documents on the screen. - [Permissionless](privacy/permissions/src/main/java/com/example/platform/privacy/permissions/Permissionless.kt): diff --git a/samples/graphics/pallete/.gitignore b/samples/graphics/pallete/.gitignore new file mode 100644 index 00000000..42afabfd --- /dev/null +++ b/samples/graphics/pallete/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/samples/graphics/pallete/build.gradle.kts b/samples/graphics/pallete/build.gradle.kts new file mode 100644 index 00000000..9504b1d5 --- /dev/null +++ b/samples/graphics/pallete/build.gradle.kts @@ -0,0 +1,27 @@ +/* + * Copyright 2022 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. + */ + +plugins { + id("com.example.platform.sample") +} + +android { + namespace = "com.example.platform.pallete" +} + +dependencies { + implementation(libs.androidx.palette) +} diff --git a/samples/graphics/pallete/consumer-rules.pro b/samples/graphics/pallete/consumer-rules.pro new file mode 100644 index 00000000..e69de29b diff --git a/samples/graphics/pallete/proguard-rules.pro b/samples/graphics/pallete/proguard-rules.pro new file mode 100644 index 00000000..481bb434 --- /dev/null +++ b/samples/graphics/pallete/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/samples/graphics/pallete/src/main/AndroidManifest.xml b/samples/graphics/pallete/src/main/AndroidManifest.xml new file mode 100644 index 00000000..34cf5700 --- /dev/null +++ b/samples/graphics/pallete/src/main/AndroidManifest.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/samples/graphics/pallete/src/main/java/com/example/platform/graphics/pallete/PalleteSampleScreen.kt b/samples/graphics/pallete/src/main/java/com/example/platform/graphics/pallete/PalleteSampleScreen.kt new file mode 100644 index 00000000..b7573f84 --- /dev/null +++ b/samples/graphics/pallete/src/main/java/com/example/platform/graphics/pallete/PalleteSampleScreen.kt @@ -0,0 +1,197 @@ +/* + * 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.pallete + +import android.graphics.BitmapFactory +import androidx.annotation.DrawableRes +import androidx.compose.foundation.Image +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.unit.dp +import androidx.palette.graphics.Palette +import com.example.platform.pallete.R +import com.google.android.catalog.framework.annotations.Sample + +@Sample( + name = "Palette API", + description = "This sample demonstrates using an Palette API.", + documentation = "https://developer.android.com/develop/ui/views/graphics/palette-colors#kts", + tags = ["Graphics"], +) +@Composable +fun PaletteSampleScreen() { + val scrollState = rememberScrollState() + + Column( + modifier = Modifier + .fillMaxSize() + .verticalScroll(scrollState), + ) { + PaletteViewer(imageId = R.drawable.img_carles_rabada_unsplash) + PaletteViewer(imageId = R.drawable.img_adam_birkett_unsplash) + } +} + +@Composable +fun PaletteViewer( + @DrawableRes imageId: Int, +) { + var profileState: Palette? by remember { mutableStateOf(null) } + + BitmapFactory.decodeResource( + LocalContext.current.resources, + imageId, + ).let { bitmap -> + Palette.from(bitmap).generate { palette -> + profileState = palette + } + } + + Column { + profileState?.let { palette -> + Text( + text = "This text and background color is from vibrant swatch.", + style = MaterialTheme.typography.titleMedium.copy( + color = Color(palette.vibrantSwatch?.titleTextColor ?: 0), + ), + modifier = Modifier + .clickable { + } + .fillMaxWidth() + .background( + color = Color(palette.vibrantSwatch?.rgb ?: 0), + ), + ) + Row { + Image( + painter = painterResource(id = imageId), + contentDescription = null, + modifier = Modifier.fillMaxWidth(0.5f), + ) + Column { + palette.lightVibrantSwatch?.let { swatch -> + Text( + text = "Light Vibrant", + style = MaterialTheme.typography.titleMedium.copy( + color = Color(swatch.titleTextColor), + ), + modifier = Modifier + .fillMaxWidth() + .background( + color = Color(swatch.rgb), + ) + .padding(5.dp), + ) + } + + palette.vibrantSwatch?.let { swatch -> + Text( + text = "Vibrant", + style = MaterialTheme.typography.titleMedium.copy( + color = Color(swatch.titleTextColor), + ), + modifier = Modifier + .fillMaxWidth() + .background( + color = Color(swatch.rgb), + ) + .padding(5.dp), + ) + } + + palette.darkVibrantSwatch?.let { swatch -> + Text( + text = "Dark Vibrant", + style = MaterialTheme.typography.titleMedium.copy( + color = Color(swatch.titleTextColor), + ), + modifier = Modifier + .fillMaxWidth() + .background( + color = Color(swatch.rgb), + ) + .padding(5.dp), + ) + } + + palette.lightMutedSwatch?.let { swatch -> + Text( + text = "Light Muted", + style = MaterialTheme.typography.titleMedium.copy( + color = Color(swatch.titleTextColor), + ), + modifier = Modifier + .fillMaxWidth() + .background( + color = Color(swatch.rgb), + ) + .padding(5.dp), + ) + } + + palette.mutedSwatch?.let { swatch -> + Text( + text = "Muted", + style = MaterialTheme.typography.titleMedium.copy( + color = Color(swatch.titleTextColor), + ), + modifier = Modifier + .fillMaxWidth() + .background( + color = Color(swatch.rgb), + ) + .padding(5.dp), + ) + } + + palette.darkMutedSwatch?.let { swatch -> + Text( + text = "Dark Muted", + style = MaterialTheme.typography.titleMedium.copy( + color = Color(swatch.titleTextColor), + ), + modifier = Modifier + .fillMaxWidth() + .background( + color = Color(swatch.rgb), + ) + .padding(5.dp), + ) + } + } + } + } + } +} diff --git a/samples/graphics/pallete/src/main/res/drawable/img_adam_birkett_unsplash.png b/samples/graphics/pallete/src/main/res/drawable/img_adam_birkett_unsplash.png new file mode 100644 index 00000000..3589bc25 Binary files /dev/null and b/samples/graphics/pallete/src/main/res/drawable/img_adam_birkett_unsplash.png differ diff --git a/samples/graphics/pallete/src/main/res/drawable/img_carles_rabada_unsplash.png b/samples/graphics/pallete/src/main/res/drawable/img_carles_rabada_unsplash.png new file mode 100644 index 00000000..ae562941 Binary files /dev/null and b/samples/graphics/pallete/src/main/res/drawable/img_carles_rabada_unsplash.png differ