我想通过属性动画在ImageButton上实现缩放功能。例如,当我单击按钮时,它将缩小。当我再次点击它时,它会被放大。
下面是我的代码的一部分:
OnClickListener clickPlayButtonHandler = new OnClickListener() {
@Override
public void onClick(View v) {
final ImageButton clickedButton = (ImageButton) v;
if((Boolean) v.getTag()) {
// zoom out
clickedButton.animate().setInterpolator(new AnticipateInterpolator()).setDuration(500).scaleXBy(-0.4f).scaleYBy(-0.4f).setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
clickedButton.setImageResource(R.drawable.bg_pause);
System.out.println(clickedButton.getWidth()); // output the width of the button for checking
}
@Override
public void onAnimationEnd(Animator animation) {
clickedButton.setTag(false);
int d = clickedButton.getWidth();
System.out.println(clickedButton.getWidth());// output the width of the button for checking
}
@Override
public void onAnimationCancel(Animator animation) {}
@Override
public void onAnimationRepeat(Animator animation) { }
});
} else {
// process zoom in
}
}
};在动画开始和动画结束之前,我打印了按钮的宽度。我想当缩小动画结束时,按钮的宽度应该比以前小一些。但事实并非如此。
无法按ViewPropertyAnimator更改视图大小?
发布于 2016-11-22 09:53:36
clickedButton.getWidth()中不会有变化,因为视图的宽度不受缩放的影响。您可以将getWidth()看作是获取视图未缩放宽度的一种方法。要更改视图的宽度,需要新的测量/布局通道。
ViewPropertyAnimator不会更改视图的宽度/高度或任何可能触发另一次布局遍历的内容。这很简单,因为布局过程很昂贵,因此可能会导致跳帧,这是我们在动画过程中最不希望看到的事情。
如果你需要得到按钮的缩放宽度,你可以做getScaleX() * clickedButton.getWidth()
发布于 2014-11-06 14:14:38
试试ObjectAnimator
ObjectAnimator xAnimator =
ObjectAnimator.ofFloat(clickedButton, "scaleX", 1.0f, -0.4f);
ObjectAnimator yAnimator =
ObjectAnimator.ofFloat(clickedButton, "scaleY", 1.0f, -0.4f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(500);
animatorSet.playTogether(xAnimator, yAnimator);
animatorSet.setInterpolator(new AnticipateInterpolator());
animatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
clickedButton.setImageResource(R.drawable.bg_pause);
System.out.println(clickedButton.getWidth());
}
@Override
public void onAnimationEnd(Animator animation) {
clickedButton.setTag(false);
int d = clickedButton.getWidth();
System.out.println(clickedButton.getWidth());
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animatorSet.start();https://stackoverflow.com/questions/26771697
复制相似问题