旋转后在ImageView中适合图像
问题描述:
我有一个ImageView,我正在旋转,我希望它在旋转完成后重新缩放自身。 (我更喜欢无缝的动画旋转,它可以在旋转时缩小,但这是另一个话题。)我有一种感觉,我需要制作一个可运行的程序来实现这一点,但我无法让它正常工作。图像保持与旋转之前相同的尺寸。旋转后在ImageView中适合图像
private void rotate() {
imageView.animate().rotationBy(90).setDuration(100).setInterpolator(new LinearInterpolator()).start();
imageView.post(new Runnable() {
@Override
public void run() {
fitImageView();
}
});
}
private void fitImageView() {
float imageWidth = imageView.getDrawable().getIntrinsicWidth();
float imageHeight = imageView.getDrawable().getIntrinsicHeight();
RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight);
RectF viewRect = new RectF(0, 0, imageView.getWidth(), imageView.getHeight());
Matrix matrix = new Matrix();
matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
imageView.setImageMatrix(matrix);
imageView.invalidate();
}
fitImageView()工作正常,以适应图像时第一次初始化它,我已经从日志证实fitImageView()被旋转过程中有时称为()。
答
如果有人遇到这种情况,我使用下面的代码,它使用动画和动画侦听器在动画完成后调用方法。我仍然需要弄清楚如何调整图像的大小,因为getheight和getwidth仍然与旋转之前的值相同,但至少在旋转完成后调用该方法。
imageView.animate().rotationBy(90).setDuration(2000).setInterpolator(new LinearInterpolator()).setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
fitImageView();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
}).start();
您的fitimageview()工作正常,但在完成动画之前调用它是正确的吗? – dipali
我这么认为。很难说它是否在日志动画之前被调用。 – Mardymar