Retain translated position in Android Matrix

梦想的初衷 提交于 2021-02-11 12:28:25

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!