From bd3408beb31294408ddb33d18de7542665788ed6 Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Mon, 1 Feb 2021 22:10:05 +0300 Subject: [PATCH] sample, imageSizeResolver usage --- app-sample/build.gradle | 4 -- app-sample/samples.json | 12 ++++ .../samples/image/ImageSizeResolverSample.kt | 64 +++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 app-sample/src/main/java/io/noties/markwon/app/samples/image/ImageSizeResolverSample.kt diff --git a/app-sample/build.gradle b/app-sample/build.gradle index 9019be25..bfd97997 100644 --- a/app-sample/build.gradle +++ b/app-sample/build.gradle @@ -99,10 +99,6 @@ android { signingConfig signingConfigs.config } } - } else { - // it seems to be a bug in NDK handling, github fail to build the project with the: - // `com.android.builder.errors.EvalIssueException: No version of NDK matched the requested version 21.0.6113669. Versions available locally: 21.3.6528147` - ndkVersion '22.0.7026061' } } diff --git a/app-sample/samples.json b/app-sample/samples.json index b3bfbcca..5d665a9a 100644 --- a/app-sample/samples.json +++ b/app-sample/samples.json @@ -1,4 +1,16 @@ [ + { + "javaClassName": "io.noties.markwon.app.samples.image.ImageSizeResolverSample", + "id": "20210201165512", + "title": "ImageSizeResolver", + "description": "Custom `ImageSizeResolver` that treats dimension values as density-based (like `dp`, `dip` in resources)", + "artifacts": [ + "CORE" + ], + "tags": [ + "image" + ] + }, { "javaClassName": "io.noties.markwon.app.samples.html.InspectHtmlTextSample", "id": "20210201140501", diff --git a/app-sample/src/main/java/io/noties/markwon/app/samples/image/ImageSizeResolverSample.kt b/app-sample/src/main/java/io/noties/markwon/app/samples/image/ImageSizeResolverSample.kt new file mode 100644 index 00000000..63e6172a --- /dev/null +++ b/app-sample/src/main/java/io/noties/markwon/app/samples/image/ImageSizeResolverSample.kt @@ -0,0 +1,64 @@ +package io.noties.markwon.app.samples.image + +import android.content.res.Resources +import io.noties.markwon.AbstractMarkwonPlugin +import io.noties.markwon.Markwon +import io.noties.markwon.MarkwonConfiguration +import io.noties.markwon.app.sample.Tags +import io.noties.markwon.app.sample.ui.MarkwonTextViewSample +import io.noties.markwon.html.HtmlPlugin +import io.noties.markwon.image.ImageSize +import io.noties.markwon.image.ImageSizeResolverDef +import io.noties.markwon.image.ImagesPlugin +import io.noties.markwon.sample.annotations.MarkwonArtifact +import io.noties.markwon.sample.annotations.MarkwonSampleInfo + +@MarkwonSampleInfo( + id = "20210201165512", + title = "ImageSizeResolver", + description = "Custom `ImageSizeResolver` that treats dimension values " + + "as density-based (like `dp`, `dip` in resources)", + artifacts = [MarkwonArtifact.CORE], + tags = [Tags.image] +) +class ImageSizeResolverSample : MarkwonTextViewSample() { + override fun render() { + val image = "https://github.com/dcurtis/markdown-mark/raw/master/png/208x128-solid.png" + val md = """ + **150px x 150px**: 150px x 150px + + **150 x 150**: 150 x 150 + + **no dimension**: no dimension + + **just width 150**: 150 + """.trimIndent() + + val markwon = Markwon.builder(context) + .usePlugin(object : AbstractMarkwonPlugin() { + override fun configureConfiguration(builder: MarkwonConfiguration.Builder) { + builder.imageSizeResolver(DensityImageSizeResolver()) + } + }) + .usePlugin(ImagesPlugin.create()) + .usePlugin(HtmlPlugin.create()) + .build() + + markwon.setMarkdown(textView, md) + } + + class DensityImageSizeResolver : ImageSizeResolverDef() { + + val density: Float by lazy(LazyThreadSafetyMode.NONE) { + Resources.getSystem().displayMetrics.density + } + + override fun resolveAbsolute(dimension: ImageSize.Dimension, original: Int, textSize: Float): Int { + if (dimension.unit == null) { + // assume density pixels + return (dimension.value * density + 0.5F).toInt() + } + return super.resolveAbsolute(dimension, original, textSize) + } + } +} \ No newline at end of file