Skip to content

Commit

Permalink
Upgrade to latest version and include graphics samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
riggaroo committed Dec 3, 2024
1 parent 4071c5b commit 4e145d2
Show file tree
Hide file tree
Showing 9 changed files with 502 additions and 19 deletions.
32 changes: 16 additions & 16 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ plugins {

android {
namespace 'dev.riggaroo.composeplaytime'
compileSdk 34
compileSdk 35

defaultConfig {
applicationId "dev.riggaroo.composeplaytime"
minSdk 23
targetSdk 34
targetSdk 35
versionCode 1
versionName "1.0"

Expand Down Expand Up @@ -48,31 +48,31 @@ android {
}

dependencies {
implementation 'androidx.navigation:navigation-runtime-ktx:2.7.7'
implementation 'androidx.navigation:navigation-runtime-ktx:2.8.4'

implementation 'androidx.graphics:graphics-shapes:1.0.0-beta01'
def composeBom = platform("androidx.compose:compose-bom:2024.05.00")
implementation 'androidx.graphics:graphics-shapes:1.0.1'
def composeBom = platform("androidx.compose:compose-bom-alpha:2024.11.00")
implementation "androidx.compose.ui:ui-util"
implementation 'androidx.core:core-ktx:1.13.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.8.1'
implementation 'androidx.activity:activity-compose:1.9.0'
implementation 'androidx.core:core-ktx:1.15.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.8.7'
implementation 'androidx.activity:activity-compose:1.9.3'
implementation composeBom

implementation 'androidx.compose.animation:animation-graphics'
implementation 'androidx.compose.ui:ui'
implementation 'androidx.compose.foundation:foundation:1.7.0-beta02'
implementation 'androidx.compose.ui:ui-graphics:1.7.0-beta02'
implementation 'androidx.compose.foundation:foundation'
implementation 'androidx.compose.ui:ui-graphics'
implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.material3:material3'
implementation 'androidx.compose.material:material-icons-extended'
implementation 'io.coil-kt:coil-compose:2.6.0'
implementation "androidx.navigation:navigation-compose:2.8.0-beta02"
implementation 'io.coil-kt:coil-compose:2.7.0'
implementation "androidx.navigation:navigation-compose:2.8.4"
implementation "com.google.accompanist:accompanist-pager-indicators:0.29.1-alpha"
implementation "androidx.graphics:graphics-shapes:1.0.0-beta01"
implementation "androidx.graphics:graphics-shapes:1.0.1"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation platform('androidx.compose:compose-bom:2024.05.00')
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
androidTestImplementation platform('androidx.compose:compose-bom:2024.11.00')
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
debugImplementation 'androidx.compose.ui:ui-tooling'
debugImplementation 'androidx.compose.ui:ui-test-manifest'
Expand Down
160 changes: 160 additions & 0 deletions app/src/main/java/dev/riggaroo/composeplaytime/BlendModes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package dev.riggaroo.composeplaytime

import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.ColorMatrix
import androidx.compose.ui.graphics.CompositingStrategy
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.layer.GraphicsLayer
import androidx.compose.ui.graphics.layer.drawLayer
import androidx.compose.ui.graphics.rememberGraphicsLayer
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import dev.riggaroo.composeplaytime.pager.PagerSampleItem
import kotlin.math.max
import kotlin.math.min

@Preview
@Composable
fun BlendModesExamples() {
Box() {
val startIndex = Int.MAX_VALUE / 2
val pagerState = rememberPagerState(initialPage = startIndex, pageCount = { Int.MAX_VALUE })
val graphicsLayer = rememberGraphicsLayer()
HorizontalPager(
state = pagerState,
contentPadding = PaddingValues(horizontal = 32.dp),
modifier = Modifier
.fillMaxWidth()
.colorFilter(ColorFilter.colorMatrix(ColorMatrix().apply {
setToSaturation(0f)
}))
) { index ->
// We calculate the page from the given index
val page = (index - startIndex).floorMod(pagerState.pageCount)

PagerSampleItem(
page = page,
modifier = Modifier
.fillMaxWidth()
.aspectRatio(1f)
)
}
val textGraphicsLayer = rememberGraphicsLayer()
Text("breaking news",
color = Color.White,
fontSize = 140.sp,
fontWeight = FontWeight.ExtraBold,
textAlign = TextAlign.Center,
modifier = Modifier
.fillMaxSize()
.padding(top = 100.dp)
.align(Alignment.Center)
.blendMode(BlendMode.Difference))
}
}

private fun Int.floorMod(other: Int): Int = when (other) {
0 -> this
else -> this - floorDiv(other) * other
}

fun Modifier.blendMode(blendMode: BlendMode): Modifier {
return this.drawWithCache {
val graphicsLayer = obtainGraphicsLayer()
graphicsLayer.apply {
record {
drawContent()
}
this.blendMode = blendMode
}
onDrawWithContent {
drawLayer(graphicsLayer)
}
}
}

private fun Modifier.colorFilter(colorFilter: ColorFilter): Modifier {
return this.drawWithCache {
val graphicsLayer = obtainGraphicsLayer()
graphicsLayer.apply {
record {
drawContent()
}
this.colorFilter = colorFilter
}
onDrawWithContent {
drawLayer(graphicsLayer)
}
}
}

@Preview
@Composable
fun InvertedColorDemo() {
val infiniteTransition = rememberInfiniteTransition()
val centerX = infiniteTransition.animateFloat(
.1f,
0.9f,
infiniteRepeatable(tween(3000), RepeatMode.Reverse)
)
val graphicsLayerBlue = rememberGraphicsLayer()
Box(modifier = Modifier
.fillMaxSize()
.wrapContentSize(Alignment.Center)
.size(200.dp, 120.dp)
.background(Color.DarkGray, CircleShape)
/* .graphicsLayer(compositingStrategy = CompositingStrategy.Offscreen)*/
) {
Text(
"Inverted Colors",
color = Color.White,
fontSize = 30.sp,
modifier = Modifier
.padding(start = 30.dp)
.align(Alignment.Center)
)

Box(modifier = Modifier.fillMaxSize().drawBehind {
val radius = size.minDimension / 4
drawCircle(
Color.Cyan,
radius = radius,
center = Offset(
max(radius, min(size.width * centerX.value, size.width - radius)),
size.height / 2
),
)
}.blendMode( blendMode = BlendMode.Xor))
}
}
Loading

0 comments on commit 4e145d2

Please sign in to comment.