Skip to content

Commit

Permalink
Merge pull request #8 from pawelsa/master
Browse files Browse the repository at this point in the history
Parameters for image
  • Loading branch information
mvarnagiris authored Nov 7, 2020
2 parents 29f0da5 + 1fb6d99 commit 91cb5d0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
16 changes: 16 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion compose-glide-image/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
}

dependencies {
def compose_version = '1.0.0-alpha05'
def compose_version = '1.0.0-alpha06'
def coroutines_version = "1.3.9"

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.FrameManager
import androidx.compose.runtime.onCommit
import androidx.compose.runtime.stateFor
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.WithConstraints
import androidx.compose.ui.graphics.ImageAsset
import androidx.compose.ui.graphics.asImageAsset
import androidx.compose.ui.graphics.drawscope.drawCanvas
import androidx.compose.ui.graphics.*
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.ContextAmbient
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
Expand All @@ -29,12 +28,17 @@ import kotlinx.coroutines.launch
@Composable
fun GlideImage(
model: Any,
modifier: Modifier = Modifier,
contentScale: ContentScale = ContentScale.Fit,
alignment: Alignment = Alignment.Center,
alpha: Float = DefaultAlpha,
colorFilter: ColorFilter? = null,
onImageReady: (() -> Unit)? = null,
customize: RequestBuilder<Bitmap>.() -> RequestBuilder<Bitmap> = { this }
) {
WithConstraints {
val image = stateFor <ImageAsset?> (null) { null }
val drawable = stateFor<Drawable?> (null) { null }
val image = stateFor<ImageAsset?>(null) { null }
val drawable = stateFor<Drawable?>(null) { null }
val context = ContextAmbient.current

onCommit(model) {
Expand All @@ -47,7 +51,10 @@ fun GlideImage(
drawable.value = placeholder
}

override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
override fun onResourceReady(
resource: Bitmap,
transition: Transition<in Bitmap>?
) {
FrameManager.ensureStarted()
image.value = resource.asImageAsset()
onImageReady?.invoke()
Expand Down Expand Up @@ -87,9 +94,16 @@ fun GlideImage(
val theImage = image.value
val theDrawable = drawable.value
if (theImage != null) {
Image(asset = theImage)
Image(
asset = theImage,
modifier = modifier,
contentScale = contentScale,
alignment = alignment,
alpha = alpha,
colorFilter = colorFilter
)
} else if (theDrawable != null) {
Canvas(modifier = Modifier.fillMaxSize()) {
Canvas(modifier = Modifier.fillMaxSize().then(modifier)) {
drawIntoCanvas { theDrawable.draw(it.nativeCanvas) }
}
}
Expand Down

0 comments on commit 91cb5d0

Please sign in to comment.