我创建了一个画布应用程序。用户可以在画布上绘图。现在我想将其绘制进度保存在房间数据库中。但我收到 androidx.compose.ui.graphics.Path 的序列化/反序列化错误。
这是我的实体序列化/反序列化代码:
@Entity(tableName = "canvas_projects")
data class CanvasProject(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
val name: String,
val ratio: Float,
@TypeConverters(BitmapConverter::class) val canvasBackground: Bitmap?,
@TypeConverters(PathListConverter::class) val paths: List<PathWithProperties>
)
data class PathWithProperties(
@TypeConverters(PathConverter::class) val path: Path,
val pathProperties: PathProperties
)
object PathConverter {
private val gson = Gson()
@TypeConverter
fun fromPath(path: Path) : String {
return gson.toJson(path)
}
@TypeConverter
fun toPath(data: String) : Path {
return gson.fromJson(data, Path::class.java)
}
}
data class PathProperties(
val strokeWidth: Float,
val color: String,
val alphas: Float,
val strokeCap: StrokeCap = StrokeCap.Round,
val strokeJoin: StrokeJoin = StrokeJoin.Round,
val eraseMode: Boolean,
val isBitmap: Boolean,
val bitmap: ImageBitmap? = null,
val bitmapRect: Rect? = null
)
object BitmapConverter {
@TypeConverter
fun fromBitmap(bitmap: Bitmap?): ByteArray? {
if (bitmap != null) {
val outputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
return outputStream.toByteArray()
} else return null
}
@TypeConverter
fun toBitmap(byteArray: ByteArray?): Bitmap? {
return if (byteArray != null) BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size) else null
}
}
object PathListConverter {
private val gson = Gson()
@TypeConverter
fun fromPathList(pathList: List<PathWithProperties>): String {
return gson.toJson(pathList)
}
@TypeConverter
fun toPathList(data: String): List<PathWithProperties> {
val listType = object : TypeToken<List<PathWithProperties>>() {}.type
return gson.fromJson(data, listType)
}
}
但是我收到以下错误:
com.google.gson.JsonIOException:接口无法实例化!为此类型注册 InstanceCreator 或 TypeAdapter。接口名称:androidx.compose.ui.graphics.Path
如何解决这个问题?
您无法序列化
androidx.compose.ui.graphics.Path
,但您可以创建一个类,例如 SaveablePath
,它包含或是一个路径,并在接口或用例中定义诸如 moveTo(x, y)
、lineTo(x, y)
之类的操作
并对它们进行序列化和反序列化,例如
PathDrawEntity(operationType, x, y, other serializable/parcelable properties)
当您使用路径对象从数据库读取数据时,使用此类来提取所有内容
class SaveablePath(
va saveUseCase: SaveUseCase,
):Path {
override fun moveTo(x: Float, y: Float) {
internalPath.moveTo(x, y)
saveUseCase.moveTo(x,y)
}
// Do samething with others
override fun relativeMoveTo(dx: Float, dy: Float) {
internalPath.rMoveTo(dx, dy)
}
override fun lineTo(x: Float, y: Float) {
internalPath.lineTo(x, y)
}
override fun relativeLineTo(dx: Float, dy: Float) {
internalPath.rLineTo(dx, dy)
}
override fun quadraticBezierTo(x1: Float, y1: Float, x2: Float, y2: Float) {
internalPath.quadTo(x1, y1, x2, y2)
}
override fun quadraticTo(x1: Float, y1: Float, x2: Float, y2: Float) {
internalPath.quadTo(x1, y1, x2, y2)
}
override fun relativeQuadraticBezierTo(dx1: Float, dy1: Float, dx2: Float, dy2: Float) {
internalPath.rQuadTo(dx1, dy1, dx2, dy2)
}
}
还有一个函数,用于读取保存的操作,并在读取时将其应用到 Path 以在 Canvas 上绘制。