首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法按ViewPropertyAnimator更改视图大小?

无法按ViewPropertyAnimator更改视图大小?
EN

Stack Overflow用户
提问于 2014-11-06 12:23:53
回答 2查看 930关注 0票数 2

我想通过属性动画在ImageButton上实现缩放功能。例如,当我单击按钮时,它将缩小。当我再次点击它时,它会被放大。

下面是我的代码的一部分:

代码语言:javascript
复制
    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更改视图大小?

EN

回答 2

Stack Overflow用户

发布于 2016-11-22 09:53:36

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

ViewPropertyAnimator不会更改视图的宽度/高度或任何可能触发另一次布局遍历的内容。这很简单,因为布局过程很昂贵,因此可能会导致跳帧,这是我们在动画过程中最不希望看到的事情。

如果你需要得到按钮的缩放宽度,你可以做getScaleX() * clickedButton.getWidth()

票数 2
EN

Stack Overflow用户

发布于 2014-11-06 14:14:38

试试ObjectAnimator

代码语言:javascript
复制
    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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26771697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档