2

我想通过 Property Animation 在 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 更改视图大小?

4

2 回答 2

2

不会有变化clickedButton.getWidth(),因为视图的宽度不受缩放的影响。您可以将 getWidth() 视为一种获取未缩放视图宽度的方法。要更改视图的宽度,需要新的测量/布局通道。

ViewPropertyAnimator不会更改 View 的宽度/高度或任何可能触发另一个布局传递的东西。这仅仅是因为布局通道很昂贵,因此可能导致跳帧,这是我们在动画期间最不想看到的。

如果您需要获得按钮的缩放宽度,您可以这样做getScaleX() * clickedButton.getWidth()

于 2016-11-22T01:53:35.630 回答
0

尝试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();
于 2014-11-06T06:14:37.587 回答