Skip to content

Commit 5e070da

Browse files
update demos
1 parent 5781ee2 commit 5e070da

File tree

2 files changed

+53
-211
lines changed

2 files changed

+53
-211
lines changed

app/src/main/java/com/smarttoolfactory/composeimage/demo/ImageScaleDemo.kt

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.smarttoolfactory.composeimage.demo
22

33
import android.graphics.Bitmap
4+
import androidx.compose.animation.core.*
45
import androidx.compose.foundation.*
56
import androidx.compose.foundation.layout.*
67
import androidx.compose.foundation.shape.RoundedCornerShape
@@ -11,6 +12,7 @@ import androidx.compose.ui.draw.clipToBounds
1112
import androidx.compose.ui.geometry.Offset
1213
import androidx.compose.ui.graphics.*
1314
import androidx.compose.ui.graphics.drawscope.Stroke
15+
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
1416
import androidx.compose.ui.input.pointer.PointerInputChange
1517
import androidx.compose.ui.layout.ContentScale
1618
import androidx.compose.ui.platform.LocalContext
@@ -319,20 +321,50 @@ private fun Drawing(modifier: Modifier) {
319321
// This is previous motion event before next touch is saved into this current position
320322
var previousPosition by remember { mutableStateOf(Offset.Unspecified) }
321323

322-
val brush = remember {
323-
Brush.verticalGradient(
324-
colors = listOf(
325-
Color.Red,
326-
Color.Green,
327-
Color.Yellow,
328-
Color.Blue,
329-
Color.Cyan,
330-
Color.Magenta,
331-
Color.Red,
332-
)
324+
325+
val transition: InfiniteTransition = rememberInfiniteTransition()
326+
327+
// Infinite phase animation for PathEffect
328+
val phase by transition.animateFloat(
329+
initialValue = .9f,
330+
targetValue = .3f,
331+
animationSpec = infiniteRepeatable(
332+
animation = tween(
333+
durationMillis = 1000,
334+
easing = FastOutSlowInEasing
335+
),
336+
repeatMode = RepeatMode.Reverse
333337
)
338+
)
339+
340+
val color = Color.Green
341+
342+
val paint = remember {
343+
Paint().apply {
344+
style = PaintingStyle.Stroke
345+
strokeWidth = 15f
346+
strokeCap = StrokeCap.Round
347+
348+
349+
this.asFrameworkPaint().apply {
350+
val transparent = color
351+
.copy(alpha = 0f)
352+
.toArgb()
353+
354+
this.color = transparent
355+
}
356+
}
334357
}
335358

359+
paint.asFrameworkPaint().setShadowLayer(
360+
30f * phase,
361+
0f,
362+
0f,
363+
color
364+
.copy(alpha = phase)
365+
.toArgb()
366+
)
367+
336368
// Path is what is used for drawing line on Canvas
337369
val path = remember(modifier) { Path() }
338370

@@ -385,10 +417,15 @@ private fun Drawing(modifier: Modifier) {
385417
else -> Unit
386418
}
387419

388-
drawPath(
389-
brush = brush,
390-
path = path,
391-
style = Stroke(width = 4.dp.toPx(), cap = StrokeCap.Round, join = StrokeJoin.Round)
392-
)
420+
this.drawIntoCanvas {
421+
422+
it.drawPath(path, paint)
423+
424+
drawPath(
425+
color = Color.White.copy((0.4f + phase).coerceAtMost(1f)),
426+
path = path,
427+
style = Stroke(width = 4.dp.toPx(), cap = StrokeCap.Round, join = StrokeJoin.Round)
428+
)
429+
}
393430
}
394431
}

app/src/main/java/com/smarttoolfactory/composeimage/demo/ThumbnailDemo.kt

Lines changed: 0 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,8 @@ private fun ThumbnailDemoSamples(imageBitmap: ImageBitmap) {
8181
ThumbnailCallbackSample(modifier, imageBitmap, contentScale)
8282
ThumbnailPositionChangeSample(modifier)
8383
ThumbnailUIPropertiesSample(modifier)
84-
ThumbnailScaleModeSample()
8584
}
8685

87-
8886
@Composable
8987
private fun ThumbnailScaleModeCustomImageSample(
9088
modifier: Modifier,
@@ -309,199 +307,6 @@ private fun ThumbnailUIPropertiesSample(
309307
),
310308
contentDescription = null
311309
)
312-
313-
}
314-
}
315-
316-
@Composable
317-
private fun ThumbnailScaleModeSample() {
318-
val modifier = Modifier
319-
.background(Color.LightGray)
320-
.fillMaxWidth()
321-
.aspectRatio(4 / 3f)
322-
323-
val bitmap1 = ImageBitmap.imageResource(
324-
LocalContext.current.resources,
325-
R.drawable.landscape1
326-
)
327-
328-
val bitmap2 = ImageBitmap.imageResource(
329-
LocalContext.current.resources,
330-
R.drawable.landscape2
331-
)
332-
333-
val bitmap3 = ImageBitmap.imageResource(
334-
LocalContext.current.resources,
335-
R.drawable.landscape3
336-
)
337-
338-
ExpandableColumnWithTitle(
339-
title = "Content Scale",
340-
color = MaterialTheme.colorScheme.primary,
341-
initialExpandState = false
342-
) {
343-
344-
Text(
345-
"Demonstrates correct positions are returned even if image is " +
346-
"scaled with ContentScale modes"
347-
)
348-
349-
// Bitmap1 1920x1280
350-
Text(text = "ContentScale.None")
351-
ImageWithThumbnail(
352-
imageBitmap = bitmap1,
353-
modifier = modifier,
354-
contentScale = ContentScale.None,
355-
contentDescription = null
356-
)
357-
358-
Spacer(modifier = Modifier.height(30.dp))
359-
Text(text = "ContentScale.Fit")
360-
ImageWithThumbnail(
361-
imageBitmap = bitmap1,
362-
modifier = modifier,
363-
contentScale = ContentScale.Fit,
364-
contentDescription = null
365-
)
366-
367-
Spacer(modifier = Modifier.height(30.dp))
368-
Text(text = "ContentScale.Crop")
369-
ImageWithThumbnail(
370-
imageBitmap = bitmap1,
371-
modifier = modifier,
372-
contentScale = ContentScale.Crop,
373-
contentDescription = null
374-
)
375-
376-
Spacer(modifier = Modifier.height(30.dp))
377-
Text(text = "ContentScale.FillBounds")
378-
ImageWithThumbnail(
379-
imageBitmap = bitmap1,
380-
modifier = modifier,
381-
contentScale = ContentScale.FillBounds,
382-
contentDescription = null
383-
)
384-
385-
Spacer(modifier = Modifier.height(30.dp))
386-
Text(text = "ContentScale.FillWidth")
387-
ImageWithThumbnail(
388-
imageBitmap = bitmap1,
389-
modifier = modifier,
390-
contentScale = ContentScale.FillWidth,
391-
contentDescription = null
392-
)
393-
394-
Spacer(modifier = Modifier.height(30.dp))
395-
Text(text = "ContentScale.FillHeight")
396-
ImageWithThumbnail(
397-
imageBitmap = bitmap1,
398-
modifier = modifier,
399-
contentScale = ContentScale.FillHeight,
400-
contentDescription = null
401-
)
402-
403-
Spacer(modifier = Modifier.height(30.dp))
404-
Text(text = "ContentScale.Inside")
405-
ImageWithThumbnail(
406-
imageBitmap = bitmap1,
407-
modifier = modifier,
408-
contentScale = ContentScale.Inside,
409-
contentDescription = null
410-
)
411-
412-
// Bitmap2 480x270
413-
414-
Spacer(modifier = Modifier.height(30.dp))
415-
Text(text = "ContentScale.None")
416-
ImageWithThumbnail(
417-
imageBitmap = bitmap2,
418-
modifier = modifier,
419-
contentScale = ContentScale.None,
420-
contentDescription = null
421-
)
422-
423-
Spacer(modifier = Modifier.height(30.dp))
424-
Text(text = "ContentScale.Fit")
425-
ImageWithThumbnail(
426-
imageBitmap = bitmap2,
427-
modifier = modifier,
428-
contentScale = ContentScale.Fit,
429-
contentDescription = null
430-
)
431-
432-
Spacer(modifier = Modifier.height(30.dp))
433-
Text(text = "ContentScale.Crop")
434-
ImageWithThumbnail(
435-
imageBitmap = bitmap2,
436-
modifier = modifier,
437-
contentScale = ContentScale.Crop,
438-
contentDescription = null
439-
)
440-
441-
Spacer(modifier = Modifier.height(30.dp))
442-
Text(text = "ContentScale.FillBounds")
443-
ImageWithThumbnail(
444-
imageBitmap = bitmap2,
445-
modifier = modifier,
446-
contentScale = ContentScale.FillBounds,
447-
contentDescription = null
448-
)
449-
450-
Spacer(modifier = Modifier.height(30.dp))
451-
Text(text = "ContentScale.FillWidth")
452-
ImageWithThumbnail(
453-
imageBitmap = bitmap2,
454-
modifier = modifier,
455-
contentScale = ContentScale.FillWidth,
456-
contentDescription = null
457-
)
458-
459-
Spacer(modifier = Modifier.height(30.dp))
460-
Text(text = "ContentScale.FillHeight")
461-
ImageWithThumbnail(
462-
imageBitmap = bitmap2,
463-
modifier = modifier,
464-
contentScale = ContentScale.FillHeight,
465-
contentDescription = null
466-
)
467-
468-
Spacer(modifier = Modifier.height(30.dp))
469-
Text(text = "ContentScale.Inside")
470-
ImageWithThumbnail(
471-
imageBitmap = bitmap2,
472-
modifier = modifier,
473-
contentScale = ContentScale.Inside,
474-
contentDescription = null
475-
)
476-
477-
// Bitmap3 1000x1000
478-
479-
Spacer(modifier = Modifier.height(30.dp))
480-
Text(text = "ContentScale.FillBounds")
481-
ImageWithThumbnail(
482-
imageBitmap = bitmap3,
483-
modifier = modifier,
484-
contentScale = ContentScale.FillBounds,
485-
contentDescription = null
486-
)
487-
488-
Spacer(modifier = Modifier.height(30.dp))
489-
Text(text = "ContentScale.Fit")
490-
ImageWithThumbnail(
491-
imageBitmap = bitmap3,
492-
modifier = modifier,
493-
contentScale = ContentScale.Fit,
494-
contentDescription = null
495-
)
496-
497-
Spacer(modifier = Modifier.height(30.dp))
498-
Text(text = "ContentScale.Crop")
499-
ImageWithThumbnail(
500-
imageBitmap = bitmap3,
501-
modifier = modifier,
502-
contentScale = ContentScale.Crop,
503-
contentDescription = null
504-
)
505310
}
506311
}
507312

0 commit comments

Comments
 (0)