每当我为 myMatrix 分配一个新的 Matrix 时,myMatrix 的引用都不会改变。结果,没有重新组合。
任何人都知道如何为 myMatrix 分配新的引用,或者以不同的方式解决这个问题?
@Composable
fun MatrixTest(){
var myMatrix by remember { mutableStateOf(Matrix()) }
Box(
Modifier
.graphicsLayer {
translationX = myMatrix.values[Matrix.TranslateX]
translationY = myMatrix.values[Matrix.TranslateY]
}
.border(2.dp, Color.Black)
.size(100.dp)
.pointerInput(Unit, myMatrix) {
detectTransformGestures { centroid, pan, zoom, rotation ->
myMatrix.translate(pan.x, pan.y)
myMatrix = Matrix(myMatrix.values)
}
}
)
}
答案 0 :(得分:0)
对 mutableStateOf
使用不同的平等政策是一种解决方案。在本例中,我使用了 neverEqualPolicy
,它将值的任何设置都视为更改。
参考:
/**
* A policy never treat values of a [MutableState] as equivalent.
*
* Setting [MutableState.value] will always be considered a change. When applying a
* [MutableSnapshot] that changes the state will always conflict with other snapshots that change
* the same state.
*/
@Suppress("UNCHECKED_CAST")
fun <T> neverEqualPolicy(): SnapshotMutationPolicy<T> =
NeverEqualPolicy as SnapshotMutationPolicy<T>
代码:
@Composable
fun TestMatrix(){
var myMatrix by remember { mutableStateOf(Matrix(), neverEqualPolicy()) }
Box(
Modifier
.graphicsLayer {
translationX = myMatrix.values[Matrix.TranslateX]
translationY = myMatrix.values[Matrix.TranslateY]
}
.border(2.dp, Color.Black)
.size(100.dp)
.pointerInput(Unit, myMatrix) {
detectTransformGestures { centroid, pan, zoom, rotation ->
myMatrix.translate(pan.x, pan.y)
myMatrix = Matrix(myMatrix.values)
}
}
)
}