问题
I have translated a view using onTouchEvent
, And then I have rotated that view with input value like 90 degree. Now I wanted to scale that view without affecting previous translation and rotation, by using following code I retained rotation, but not translation.
val scaleValue = 1.2f
val px = rectf.centerX()
val py = rectf.centerY()
matrix.reset()
matrix.setScale(scaleValue, scaleValue, px, py)
matrix.postRotate(rotateAngle, px, py)
Update 1
val scaleFactor = 0.02f
val scaleValue = value * scaleFactor //value is 0...100
val px = bounds.centerX()
val py = bounds.centerY()
val rectF = RectF()
matrix.mapRect(rectF, bounds)
matrix.reset()
matrix.postTranslate(rectF.centerX(), rectF.centerY())
matrix.postScale(scaleValue, scaleValue, px, py)
matrix.postRotate(rotateAngle, px, py)
回答1:
Now I wanted to scale that view without affecting previous translation and rotation
Then, you should not reset()
the matrix
.
val scaleValue = 1.2f
val px = rectf.centerX()
val py = rectf.centerY()
//matrix.reset()
matrix.setScale(scaleValue, scaleValue, px, py)
matrix.postRotate(rotateAngle, px, py)
If you reset it, all of previous manipulations for the matrix including translations are lost. (As you only show quite few lines of your code, not being reproduce-able, I can't tell why the rotation is retained though.)
来源:https://stackoverflow.com/questions/66119167/retain-translated-position-in-android-matrix