Skip to content

Commit

Permalink
Update to Compose SNAPSHOT #6492492
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Banes committed May 14, 2020
1 parent 5768829 commit bba1bf6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ plugins {
subprojects {
repositories {
google()
maven { url 'https://androidx.dev/snapshots/builds/6492492/artifacts/ui/repository' }
jcenter()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object Libs {

object UI {
const val kotlinCompilerVersion = "1.3.70-dev-withExperimentalGoogleExtensions-20200424"
const val version = "0.1.0-dev10"
const val version = "0.1.0-SNAPSHOT"

const val composeRuntime = "androidx.compose:compose-runtime:$version"
const val framework = "androidx.ui:ui-framework:$version"
Expand Down
45 changes: 26 additions & 19 deletions coil/src/main/java/dev/chrisbanes/accompanist/coil/Coil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dev.chrisbanes.accompanist.coil

import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.drawable.Drawable
import androidx.animation.FloatPropKey
Expand All @@ -27,6 +28,7 @@ import androidx.compose.remember
import androidx.compose.setValue
import androidx.compose.stateFor
import androidx.core.graphics.drawable.toBitmap
import androidx.core.util.Pools
import androidx.ui.animation.Transition
import androidx.ui.core.Alignment
import androidx.ui.core.Constraints
Expand All @@ -40,13 +42,15 @@ import androidx.ui.core.hasFixedHeight
import androidx.ui.core.hasFixedWidth
import androidx.ui.foundation.Image
import androidx.ui.geometry.Offset
import androidx.ui.graphics.Canvas
import androidx.ui.geometry.Size
import androidx.ui.graphics.ColorFilter
import androidx.ui.graphics.ImageAsset
import androidx.ui.graphics.Paint
import androidx.ui.graphics.asImageAsset
import androidx.ui.graphics.painter.CanvasScope
import androidx.ui.graphics.painter.ImagePainter
import androidx.ui.graphics.painter.Painter
import androidx.ui.graphics.painter.drawCanvas
import androidx.ui.unit.IntPx
import androidx.ui.unit.PxSize
import coil.Coil
Expand Down Expand Up @@ -133,8 +137,10 @@ fun CoilImageWithCrossfade(
crossfadeDuration: Int = defaultTransitionDuration,
modifier: Modifier = Modifier
) {
WithConstraints(modifier) { constraints, _ ->
var imgLoadState by stateFor(request) { ImageLoadState.Empty }
WithConstraints(modifier) {
// We key off the data, for the same reasons as executeAsComposable()
// below
var imgLoadState by stateFor(request.data) { ImageLoadState.Empty }

val transitionDef = remember(crossfadeDuration) {
transitionDefinition {
Expand Down Expand Up @@ -190,11 +196,8 @@ fun CoilImageWithCrossfade(
matrix.alphaFraction = transitionState[alpha]
matrix.brightnessFraction = transitionState[brightness]

// Unfortunately ColorMatrixColorFilter is not mutable so we have to create a new
// instance every time
val cf = ColorMatrixColorFilter(matrix)
Image(
painter = AndroidColorMatrixImagePainter(image, cf),
painter = ColorMatrixImagePainter(image, colorMatrix = matrix),
contentScale = contentScale,
alignment = alignment,
modifier = modifier
Expand Down Expand Up @@ -255,7 +258,7 @@ fun CoilImage(
colorFilter: ColorFilter? = null,
modifier: Modifier = Modifier
) {
WithConstraints(modifier) { constraints, _ ->
WithConstraints(modifier) {
// Execute the request using executeAsComposable(), which guards the actual execution
// so that the request is only run if the request changes.
val result = if (request.sizeResolver != null) {
Expand Down Expand Up @@ -284,25 +287,29 @@ fun CoilImage(
}
}

private val paintPool = Pools.SimplePool<Paint>(2)

/**
* An [ImagePainter] which draws the image with the given Android framework
* [android.graphics.ColorFilter].
* [android.graphics.ColorMatrix].
*/
private class AndroidColorMatrixImagePainter(
private class ColorMatrixImagePainter(
private val image: ImageAsset,
colorFilter: android.graphics.ColorFilter
private val srcOffset: Offset = Offset.zero,
private val srcSize: Size = Size(image.width.toFloat(), image.height.toFloat()),
private val colorMatrix: ColorMatrix? = null
) : Painter() {
private val paint = Paint()
private val size = PxSize(IntPx(image.width), IntPx(image.height))

init {
paint.asFrameworkPaint().colorFilter = colorFilter
}
override fun CanvasScope.onDraw() {
val paint = paintPool.acquire() ?: Paint()
paint.asFrameworkPaint().colorFilter = colorMatrix?.let(::ColorMatrixColorFilter)

drawCanvas { canvas, _ ->
canvas.drawImageRect(image, srcOffset, srcSize, Offset.zero, size, paint)
}

override fun onDraw(canvas: Canvas, bounds: PxSize) {
// Always draw the image in the top left as we expect it to be translated and scaled
// in the appropriate position
canvas.drawImage(image, Offset.zero, paint)
paintPool.release(paint)
}

/**
Expand Down

0 comments on commit bba1bf6

Please sign in to comment.