代码之家  ›  专栏  ›  技术社区  ›  MarGin

通过ViewPropertyImator缩放视图后,如何将视图恢复到其原始大小?

  •  7
  • MarGin  · 技术社区  · 7 年前

    这是我的密码。在这里,动画后图像大小减小。当我单击 ImageView 再说一遍,我只想 图片框 原始尺寸。我是初学者,所以我需要一些帮助。我尝试过以下方法:

    football.animate().scaleX(1f).scaleY(1f).setDuration(1000).start();
    

    在开始 setonclicklistener 但这行不通。

    提前感谢

    football.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
    
                                ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
                                // values from 0 to 1
                                animator.setDuration(1000); // 5 seconds duration from 0 to 1
                                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
                                {
                                    @Override
                                    public void onAnimationUpdate(ValueAnimator animation) {
                                        float value = ((Float) (animation.getAnimatedValue()))
                                                .floatValue();
                                        // Set translation of view here. Position can be calculated
                                        // out of value. This code should move the view in a half circle.
                                        football.setTranslationX((float)(100.0 * Math.sin(value*Math.PI)));
                                        football.setTranslationY((float)(400.0 * Math.cos(value*Math.PI)));
                                    }
                                });
    
                                animator.start();
    football.setScaleType(ImageView.ScaleType.CENTER);
    
                                //here the scaling is performed
            football.animate().scaleX(0.4f).scaleY(0.4f).setDuration(1000).start();
                    }
                        });
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   earthw0rmjim Abhinav Das    7 年前

    您可以检查 View (或者 scaleX scaleY ,在这种情况下无关紧要,因为您可以对两者进行相等的缩放),并根据该值增加/减少大小。

    例如:

    // if the current scale is lesser than 1.0f, increase it to 1.0f
    // otherwise decrease it to 0.4f
    float scaleValue = football.getScaleX() < 1.0f ? 1.0f : 0.4f;
    football.animate().scaleX(scaleValue).scaleY(scaleValue).setDuration(1000).start();
    

    编辑 (在下面发表评论):如果您希望 看法 要在每次单击时缩小其原始大小,只需在每次动画之前“重置”即可:

    // resetting the scale to its original value
    football.setScaleX(1.0f);
    football.setScaleY(1.0f);
    
    // shrinking
    football.animate().scaleX(0.4f).scaleY(0.4f).setDuration(1000).start();