我試圖在縮小 ImageView 后移動它,但由于某種原因,該位置與目標位置不匹配。首先,我計算比例因子和新舊位置之間的差異。然后我開始了影片,但是 ImageView 向左和底部移動了很遠,我不知道為什么。
float scaleX = (destWidth * 1.0f) / imageView.getMeasuredWidth();
float scaleY = (destHeight * 1.0f) / imageView.getMeasuredHeight();
float moveX = -1 * (imageView.getLeft() - destX); // move image to the left
float moveY = destY - imageView.getTop(); // move image to the bottom
oa = ObjectAnimator.ofPropertyValuesHolder(imageView
, PropertyValuesHolder.ofFloat("scaleX", scaleX)
, PropertyValuesHolder.ofFloat("scaleY", scaleY)
, PropertyValuesHolder.ofFloat("translationX", moveX)
, PropertyValuesHolder.ofFloat("translationY", moveY));
oa.setDuration(3000);
oa.setRepeatMode(ValueAnimator.REVERSE);
oa.setRepeatCount(ValueAnimator.INFINITE);
oa.start();
uj5u.com熱心網友回復:
我自己想通了,但我會將更正的代碼留給其他有類似問題的人。
基本上,ImageView是先根據平移移動,然后按比例縮小。縮放將頂部、底部、左側和右側移動到中心,因此 ImageView 是偏移的。
要修復舊 ImageView 大小和新 ImageView 大小之間的這一半差異,需要從之前的 moveX/moveY 中減去。
float moveX = -1 * (imageView.getLeft() - destX)
- ((imageView.getMeasuredWidth() - (imageView.getMeasuredWidth() * scaleX)) / 2);
// move image to the bottom
float moveY = destY - imageView.getTop()
- ((imageView.getMeasuredHeight() - (imageView.getMeasuredHeight() * scaleY)) / 2);
最后,完成的代碼應該是這樣的。
float scaleX = (destWidth * 1.0f) / imageView.getMeasuredWidth();
float scaleY = (destHeight * 1.0f) / imageView.getMeasuredHeight();
// move image to the left
float moveX = -1 * (imageView.getLeft() - destX)
- ((imageView.getMeasuredWidth() - (imageView.getMeasuredWidth() * scaleX)) / 2);
// move image to the bottom
float moveY = destY - imageView.getTop()
- ((imageView.getMeasuredHeight() - (imageView.getMeasuredHeight() * scaleY)) / 2);
oa = ObjectAnimator.ofPropertyValuesHolder(imageView
, PropertyValuesHolder.ofFloat("scaleX", scaleX)
, PropertyValuesHolder.ofFloat("scaleY", scaleY)
, PropertyValuesHolder.ofFloat("translationX", moveX)
, PropertyValuesHolder.ofFloat("translationY", moveY));
oa.setDuration(3000);
oa.setRepeatMode(ValueAnimator.REVERSE);
oa.setRepeatCount(ValueAnimator.INFINITE);
oa.start();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/480655.html
