Translations: 简体中文
Sketch provides extended functions for view and Compose to display download progress, as follows:
Three styles are provided to choose from, as follows:
Tip
They can also adjust color, size and behavior
First install component
${LAST_VERSION}
: (Not included 'v')
implementation("io.github.panpf.sketch4:sketch-extensions-compose:${LAST_VERSION}")
Then use the progressIndicator() function to add a progress indicator
// val progressPainter = rememberMaskProgressPainter()
// val progressPainter = rememberSectorProgressPainter()
val progressPainter = rememberRingProgressPainter()
val state = rememberAsyncImageState()
AsyncImage(
uri = "https://example.com/image.jpg",
modifier = Modifier
.size(200.dp)
.progressIndicator(state, progressPainter),
state = state,
contentDescription = "",
)
Tip
Compose version function is implemented by ProgressIndicatorModifier
You can inherit AbsProgressPainter to implement your own progress indicator, as follows:
class MyProgressPainter(
private val maskColor: Color = Color(PROGRESS_INDICATOR_MASK_COLOR),
hiddenWhenIndeterminate: Boolean = PROGRESS_INDICATOR_HIDDEN_WHEN_INDETERMINATE,
hiddenWhenCompleted: Boolean = PROGRESS_INDICATOR_HIDDEN_WHEN_COMPLETED,
stepAnimationDuration: Int = PROGRESS_INDICATOR_STEP_ANIMATION_DURATION,
) : AbsProgressPainter(
hiddenWhenIndeterminate = hiddenWhenIndeterminate,
hiddenWhenCompleted = hiddenWhenCompleted,
stepAnimationDuration = stepAnimationDuration
), SketchPainter {
override val intrinsicSize: Size = Size(200.0, 200.0)
override fun DrawScope.drawProgress(drawProgress: Float) {
// Draw your indicator
}
}
Then use your own indicator like this:
val progressPainter = remember { MyProgressPainter() }
val state = rememberAsyncImageState()
AsyncImage(
uri = "https://example.com/image.jpg",
modifier = Modifier
.size(200.dp)
.progressIndicator(state, progressPainter),
state = state,
contentDescription = "",
)
First install component
${LAST_VERSION}
: (Not included 'v')
implementation("io.github.panpf.sketch4:sketch-extensions-view:${LAST_VERSION}")
Then use the show*ProgressIndicator() function with SketchImageView to add a progress indicator
val sketchImageView = SketchImageView(context)
sketchImageView.showMaskProgressIndicator()
// or
sketchImageView.showSectorProgressIndicator()
// or
sketchImageView.showRingProgressIndicator()
Tip
View version functionality is implemented by ProgressIndicatorAbility
You can extends AbsProgressDrawable to implement your own progress indicator, as follows:
class MyProgressDrawable(
hiddenWhenIndeterminate: Boolean = PROGRESS_INDICATOR_HIDDEN_WHEN_INDETERMINATE,
hiddenWhenCompleted: Boolean = PROGRESS_INDICATOR_HIDDEN_WHEN_COMPLETED,
stepAnimationDuration: Int = PROGRESS_INDICATOR_DEFAULT_STEP_ANIMATION_DURATION,
) : AbsProgressDrawable(
hiddenWhenIndeterminate = hiddenWhenIndeterminate,
hiddenWhenCompleted = hiddenWhenCompleted,
stepAnimationDuration = stepAnimationDuration
) {
private val paint = Paint().apply {
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
alpha = this@RingProgressDrawable.alpha
}
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
colorFilter = this@RingProgressDrawable.colorFilter
}
}
override fun drawProgress(canvas: Canvas, drawProgress: Float) {
val bounds = bounds.takeIf { !it.isEmpty } ?: return
canvas.withSave {
// Draw your indicator
}
}
override fun setAlpha(alpha: Int) {
paint.alpha = alpha
}
override fun setColorFilter(colorFilter: ColorFilter?) {
paint.colorFilter = colorFilter
}
@Deprecated(
"Deprecated in Java. This method is no longer used in graphics optimizations",
ReplaceWith("")
)
override fun getOpacity(): Int = PixelFormat.TRANSLUCENT
override fun getIntrinsicWidth(): Int = 200
override fun getIntrinsicHeight(): Int = 200
override fun mutate(): ProgressDrawable {
return this
}
}
Then use your own indicator like this:
val sketchImageView = SketchImageView(context)
sketchImageView.showProgressIndicator(MyProgressDrawable())