-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement BlurTransformationPlugin (#959)
* Implement BlurTransformationPlugin * Update gitignore and add missing aar file * Update to compileOnly
- Loading branch information
Showing
8 changed files
with
247 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/build | ||
/build | ||
!/libs/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...tlin/io/getstream/video/android/compose/ui/components/plugins/BlurTransformationPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (c) 2014-2023 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-video-android/blob/main/LICENSE | ||
* | ||
* 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 io.getstream.video.android.compose.ui.components.plugins | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.Immutable | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import com.skydoves.landscapist.plugins.ImagePlugin | ||
|
||
/** | ||
* Originated from [Landscapist](https://github.com/skydoves/landscapist). | ||
* | ||
* BlurTransformationPlugin adds blur transformation effect while rendering an image. | ||
* An image plugin that extends [ImagePlugin.PainterPlugin] to be executed while rendering painters. | ||
* | ||
* @property radius The radius of the pixels used to blur, a value from 0 to infinite. Default is 10. | ||
*/ | ||
@Immutable | ||
internal data class BlurTransformationPlugin( | ||
val radius: Int = 10, | ||
) : ImagePlugin.PainterPlugin { | ||
|
||
/** | ||
* Compose circular reveal painter with an [imageBitmap] to the given [painter]. | ||
* | ||
* @param imageBitmap A target [ImageBitmap] to be drawn on the painter. | ||
* @param painter A given painter to be executed circular reveal animation. | ||
*/ | ||
@Composable | ||
override fun compose(imageBitmap: ImageBitmap, painter: Painter): Painter { | ||
return painter.rememberBlurPainter( | ||
imageBitmap = imageBitmap, | ||
radius = radius, | ||
) | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...in/kotlin/io/getstream/video/android/compose/ui/components/plugins/RememberBlurPainter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2014-2023 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-video-android/blob/main/LICENSE | ||
* | ||
* 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 io.getstream.video.android.compose.ui.components.plugins | ||
|
||
import android.graphics.Bitmap | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.asAndroidBitmap | ||
import androidx.compose.ui.graphics.asImageBitmap | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import com.google.android.renderscript.Toolkit | ||
|
||
/** | ||
* Originated from [Landscapist](https://github.com/skydoves/landscapist). | ||
* | ||
* This is an extension of the [Painter] for giving blur transformation effect to the given [imageBitmap]. | ||
* | ||
* @param imageBitmap an image bitmap for loading the content. | ||
* @property radius The radius of the pixels used to blur, a value from 1 to 25. Default is 10. | ||
*/ | ||
@Composable | ||
internal fun Painter.rememberBlurPainter( | ||
imageBitmap: ImageBitmap, | ||
radius: Int, | ||
): Painter { | ||
var androidBitmap = imageBitmap.asAndroidBitmap() | ||
|
||
if (!( | ||
androidBitmap.config == Bitmap.Config.ARGB_8888 || | ||
androidBitmap.config == Bitmap.Config.ALPHA_8 | ||
) | ||
) { | ||
androidBitmap = androidBitmap.copy(Bitmap.Config.ARGB_8888, false) | ||
} | ||
|
||
val blurredBitmap = remember(imageBitmap, radius) { | ||
iterativeBlur(androidBitmap, radius) | ||
} | ||
return remember(this) { | ||
TransformationPainter( | ||
imageBitmap = blurredBitmap.asImageBitmap(), | ||
painter = this, | ||
) | ||
} | ||
} | ||
|
||
private fun iterativeBlur( | ||
androidBitmap: Bitmap, | ||
radius: Int, | ||
): Bitmap { | ||
val iterate = (radius + 1) / 25 | ||
var bitmap: Bitmap = Toolkit.blur( | ||
inputBitmap = androidBitmap, | ||
radius = (radius + 1) % 25, | ||
) | ||
|
||
for (i in 0 until iterate) { | ||
bitmap = Toolkit.blur( | ||
inputBitmap = bitmap, | ||
radius = 25, | ||
) | ||
} | ||
|
||
return bitmap | ||
} |
102 changes: 102 additions & 0 deletions
102
.../kotlin/io/getstream/video/android/compose/ui/components/plugins/TransformationPainter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright (c) 2014-2023 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-video-android/blob/main/LICENSE | ||
* | ||
* 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 io.getstream.video.android.compose.ui.components.plugins | ||
|
||
import android.graphics.Matrix | ||
import android.graphics.RectF | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.geometry.toRect | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.ImageShader | ||
import androidx.compose.ui.graphics.Paint | ||
import androidx.compose.ui.graphics.ShaderBrush | ||
import androidx.compose.ui.graphics.TileMode | ||
import androidx.compose.ui.graphics.asAndroidBitmap | ||
import androidx.compose.ui.graphics.drawscope.DrawScope | ||
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import androidx.core.util.Pools | ||
|
||
/** | ||
* Originated from [Landscapist](https://github.com/skydoves/landscapist). | ||
* | ||
* TransformationPainter is a [Painter] which draws the [imageBitmap] to the given [painter]. | ||
* | ||
* @param imageBitmap an image bitmap for loading for the content. | ||
* @param painter an image painter to draw an [ImageBitmap] into the provided canvas. | ||
*/ | ||
internal class TransformationPainter( | ||
private val imageBitmap: ImageBitmap, | ||
private val painter: Painter, | ||
) : Painter() { | ||
|
||
/** return the dimension size of the [painter]'s intrinsic width and height. */ | ||
override val intrinsicSize: Size get() = painter.intrinsicSize | ||
|
||
override fun DrawScope.onDraw() { | ||
drawIntoCanvas { canvas -> | ||
var dx = 0f | ||
var dy = 0f | ||
val scale: Float | ||
val shaderMatrix = Matrix() | ||
val shader = ImageShader(imageBitmap, TileMode.Clamp) | ||
val brush = ShaderBrush(shader) | ||
val paint = paintPool.acquire() ?: Paint() | ||
paint.asFrameworkPaint().apply { | ||
isAntiAlias = true | ||
isDither = true | ||
isFilterBitmap = true | ||
} | ||
|
||
// cache the paint in the internal stack. | ||
canvas.saveLayer(size.toRect(), paint) | ||
|
||
val mDrawableRect = RectF(0f, 0f, size.width, size.height) | ||
val bitmapWidth: Int = imageBitmap.asAndroidBitmap().width | ||
val bitmapHeight: Int = imageBitmap.asAndroidBitmap().height | ||
|
||
if (bitmapWidth * mDrawableRect.height() > mDrawableRect.width() * bitmapHeight) { | ||
scale = mDrawableRect.height() / bitmapHeight.toFloat() | ||
dx = (mDrawableRect.width() - bitmapWidth * scale) * 0.5f | ||
} else { | ||
scale = mDrawableRect.width() / bitmapWidth.toFloat() | ||
dy = (mDrawableRect.height() - bitmapHeight * scale) * 0.5f | ||
} | ||
|
||
// resize the matrix to scale by sx and sy. | ||
shaderMatrix.setScale(scale, scale) | ||
|
||
// post translate the matrix with the specified translation. | ||
shaderMatrix.postTranslate( | ||
(dx + 0.5f) + mDrawableRect.left, | ||
(dy + 0.5f) + mDrawableRect.top, | ||
) | ||
// apply the scaled matrix to the shader. | ||
shader.setLocalMatrix(shaderMatrix) | ||
// draw an image bitmap as a rect. | ||
drawRect(brush = brush) | ||
// restore canvas. | ||
canvas.restore() | ||
// resets the paint and release to the pool. | ||
paint.asFrameworkPaint().reset() | ||
paintPool.release(paint) | ||
} | ||
} | ||
} | ||
|
||
/** paint pool which caching and reusing [Paint] instances. */ | ||
private val paintPool = Pools.SimplePool<Paint>(2) |