缩放动画不会改变 ImageView 的大小
Scaling animation doesn't change the ImageView size
我正在创建一个应用程序,它从资源中加载可绘制对象并将其显示在占位符 ImageView 中。
当用户单击按钮加载下一张图片时,我创建了一个动画,它会移动并 缩放 旧图像到新位置并将新的可绘制对象加载到占位符。
30 张图片后,我 运行 OOM,这是有道理的。
我想做的是在动画结束后对每个图像重新采样,因为我从 500X500 占位符开始,以 1/3 结束。
问题是在我完成动画后,ImageView 的宽度和高度保持不变。
缩放动画不应该改变ImageView的宽度和高度吗?
这是动画代码:
//create the animation
imageView.setPivotX(0);
imageView.setPivotY(0);
imageView.animate()
.scaleX(scaleX) //about 0.3
.scaleY(scaleY) //about 0.3
.x(finalX) //location x of the image
.y(finalY) //location y of the image
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
int reqWidth = imageView.getWidth(); //result 500 as expected
int reqHeight = imageView.getHeight();//result 500 as expected
Drawable image = imageView.getDrawable();
}
@Override
public void onAnimationEnd(Animator animation) {
int reqWidth = imageView.getWidth();//result 500 - why?
int reqHeight = imageView.getHeight();//result 500 - why?
Drawable image = imageView.getDrawable();
}
@Override
public void onAnimationCancel(Animator animation) {}
@Override
public void onAnimationRepeat(Animator animation) {}
});
scaleX()
使 View 的 scaleX 属性 动画到指定值 (reference). It doesn't directly change the layout width/height values. (reference)
您可以通过乘以 scaleX/Y 值来获得预期的 width/height 值。
//create the animation
imageView.setPivotX(0);
imageView.setPivotY(0);
imageView.animate()
.scaleX(scaleX) //about 0.3
.scaleY(scaleY) //about 0.3
.x(finalX) //location x of the image
.y(finalY) //location y of the image
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
int reqWidth = imageView.getWidth(); //result 500 as expected
int reqHeight = imageView.getHeight();//result 500 as expected
Drawable image = imageView.getDrawable();
}
@Override
public void onAnimationEnd(Animator animation) {
int reqWidth = imageView.getWidth() * imageView.getScaleX;
int reqHeight = imageView.getHeight() * imageView.getScaleY;
Drawable image = imageView.getDrawable();
}
@Override
public void onAnimationCancel(Animator animation) {}
@Override
public void onAnimationRepeat(Animator animation) {}
});
我正在创建一个应用程序,它从资源中加载可绘制对象并将其显示在占位符 ImageView 中。 当用户单击按钮加载下一张图片时,我创建了一个动画,它会移动并 缩放 旧图像到新位置并将新的可绘制对象加载到占位符。
30 张图片后,我 运行 OOM,这是有道理的。
我想做的是在动画结束后对每个图像重新采样,因为我从 500X500 占位符开始,以 1/3 结束。
问题是在我完成动画后,ImageView 的宽度和高度保持不变。 缩放动画不应该改变ImageView的宽度和高度吗?
这是动画代码:
//create the animation
imageView.setPivotX(0);
imageView.setPivotY(0);
imageView.animate()
.scaleX(scaleX) //about 0.3
.scaleY(scaleY) //about 0.3
.x(finalX) //location x of the image
.y(finalY) //location y of the image
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
int reqWidth = imageView.getWidth(); //result 500 as expected
int reqHeight = imageView.getHeight();//result 500 as expected
Drawable image = imageView.getDrawable();
}
@Override
public void onAnimationEnd(Animator animation) {
int reqWidth = imageView.getWidth();//result 500 - why?
int reqHeight = imageView.getHeight();//result 500 - why?
Drawable image = imageView.getDrawable();
}
@Override
public void onAnimationCancel(Animator animation) {}
@Override
public void onAnimationRepeat(Animator animation) {}
});
scaleX()
使 View 的 scaleX 属性 动画到指定值 (reference). It doesn't directly change the layout width/height values. (reference)
您可以通过乘以 scaleX/Y 值来获得预期的 width/height 值。
//create the animation
imageView.setPivotX(0);
imageView.setPivotY(0);
imageView.animate()
.scaleX(scaleX) //about 0.3
.scaleY(scaleY) //about 0.3
.x(finalX) //location x of the image
.y(finalY) //location y of the image
.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
int reqWidth = imageView.getWidth(); //result 500 as expected
int reqHeight = imageView.getHeight();//result 500 as expected
Drawable image = imageView.getDrawable();
}
@Override
public void onAnimationEnd(Animator animation) {
int reqWidth = imageView.getWidth() * imageView.getScaleX;
int reqHeight = imageView.getHeight() * imageView.getScaleY;
Drawable image = imageView.getDrawable();
}
@Override
public void onAnimationCancel(Animator animation) {}
@Override
public void onAnimationRepeat(Animator animation) {}
});