Wear CWF Allow custom image in textView background
This commit is contained in:
parent
752202a280
commit
130391eb99
|
@ -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) {
|
||||||
|
|
|
@ -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 -> {
|
||||||
|
|
Loading…
Reference in a new issue