Wear CWF Allow custom image in textView background

This commit is contained in:
Philoul 2023-09-14 21:32:10 +02:00
parent 752202a280
commit 130391eb99
2 changed files with 7 additions and 4 deletions

View file

@ -12,7 +12,6 @@ import info.nightscout.shared.R
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import org.json.JSONObject import org.json.JSONObject
import java.io.BufferedOutputStream import java.io.BufferedOutputStream
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
@ -77,7 +76,7 @@ enum class ResFormat(val extension: String) {
@Serializable @Serializable
data class ResData(val value: ByteArray, val format: ResFormat) { data class ResData(val value: ByteArray, val format: ResFormat) {
fun toDrawable(resources: Resources): Drawable? { fun toDrawable(resources: Resources, width: Int? = null, height: Int? = null): Drawable? {
try { try {
return when (format) { return when (format) {
ResFormat.PNG, ResFormat.JPG -> { ResFormat.PNG, ResFormat.JPG -> {
@ -86,7 +85,9 @@ data class ResData(val value: ByteArray, val format: ResFormat) {
} }
ResFormat.SVG -> { ResFormat.SVG -> {
val svg = SVG.getFromInputStream(ByteArrayInputStream(value)) val svg = SVG.getFromString(String(value))
svg.documentWidth = width?.toFloat() ?: svg.documentWidth
svg.documentHeight = height?.toFloat() ?: svg.documentHeight
val picture = svg.renderToPicture() val picture = svg.renderToPicture()
PictureDrawable(picture).apply { PictureDrawable(picture).apply {
setBounds(0, 0, svg.documentWidth.toInt(), svg.documentHeight.toInt()) setBounds(0, 0, svg.documentWidth.toInt(), svg.documentHeight.toInt())
@ -233,7 +234,8 @@ enum class JsonKeys(val key: String) {
COLOR("color"), COLOR("color"),
ALLCAPS("allCaps"), ALLCAPS("allCaps"),
DAYNAMEFORMAT("dayNameFormat"), DAYNAMEFORMAT("dayNameFormat"),
MONTHFORMAT("monthFormat") MONTHFORMAT("monthFormat"),
BACKGROUND("background")
} }
enum class JsonKeyValues(val key: String) { enum class JsonKeyValues(val key: String) {

View file

@ -196,6 +196,7 @@ class CustomWatchface : BaseWatchFace() {
view.isAllCaps = viewJson.optBoolean(ALLCAPS.key) view.isAllCaps = viewJson.optBoolean(ALLCAPS.key)
if (viewJson.has(TEXTVALUE.key)) if (viewJson.has(TEXTVALUE.key))
view.text = viewJson.optString(TEXTVALUE.key) view.text = viewJson.optString(TEXTVALUE.key)
view.background = resDataMap[viewJson.optString(BACKGROUND.key)]?.toDrawable(resources, width, height)
} }
is ImageView -> { is ImageView -> {