为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用户按下的按钮在按下时缩小?

如何使用户按下的按钮在按下时缩小?

潇湘沐 2023-11-10 16:17:14
我正在开发一款手机游戏,在游戏中,用户在按钮缩小之前按下按钮,为了让游戏变得“有趣”,按钮缩小的速度越来越快,所以当用户按下一个按钮时,另一个按钮就会弹出向上收缩得更快。到目前为止,我有这段代码可以处理用户输入和缩小按钮,但是,一旦按下,就没有动画,它会立即捕捉到“缩小”的尺寸,我不知道如何解决这个问题。button.setOnClickListener(new Button.OnClickListener(){        @Override        public void onClick(View arg0) {            ViewGroup.LayoutParams params = button.getLayoutParams();            Integer value = 120;            while(value >= 2) {                value = value - 1;                SystemClock.sleep(50);                params.width = value;                params.height = value;                button.setLayoutParams(params);            };        }    });我添加了这条SystemClock.sleep(50)线,因为我认为这就是它捕捉的原因(即它太快了,我只是没有看到动画),但事实并非如此,因为应用程序只是挂起,直到按钮的大小更新。PS 在开发移动应用程序方面我还是个新手。
查看完整描述

1 回答

?
慕莱坞森

TA贡献1810条经验 获得超4个赞

编辑


忘记我在原来的帖子中写的内容了。请尝试下面的代码,让我知道这是否对您有帮助。


final ValueAnimator valueAnimator = ValueAnimator.ofFloat(1.0f, 0.0f); //start and end value

        valueAnimator.setDuration(2000); //you can replace 2000 with a variable you can change dynamically

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override

            public void onAnimationUpdate(ValueAnimator animation) {

                float animatedValue = (float) animation.getAnimatedValue();

                button.setScaleX(animatedValue);

                button.setScaleY(animatedValue);

            }

        });

        valueAnimator.start();


        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                valueAnimator.pause();

            }

        });

原答案


我会遵循 0X0nosugar 的建议。


在 Android 文件树中的 res 目录下,添加 Android 资源目录(右键单击 res 文件夹 > 新建)并将其命名为“anim”。如果您使用该名称,Android Studio 可能会自动将其视为保存动画的文件夹。再次右键单击动画文件夹 > 新建 > 动画资源文件。将其命名为您想要的名称。在我的示例中,我将其命名为“button_animator”。


您的文件树将如下所示:

https://img1.sycdn.imooc.com/654de72b0001955e02070136.jpg

您的button_animator.xml 可能如下所示:


<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">


    <scale

        android:interpolator="@android:anim/accelerate_decelerate_interpolator"

        android:fromXScale="1.0"

        android:toXScale="0.1"

        android:fromYScale="1.0"

        android:toYScale="0.1"

        android:pivotX="50%"

        android:pivotY="50%"

        android:fillAfter="false"

        android:duration="500" />


</set>

您可以使用以下几行定制按钮的最终比例:


android:toXScale="0.1"


android:toYScale="0.1"

在代码中,您可以动态定制动画:


button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {


                Animation shrinkButton = AnimationUtils.loadAnimation(MainActivity.this, R.anim.button_animator); //reference the animator

                shrinkButton.setDuration(5000); //dynamically set the duration of your animation

                button.startAnimation(shrinkButton); //start the animation. Since it is inside an onclicklistener, the animation start on a button click event


                shrinkButton.setAnimationListener(new Animation.AnimationListener() { //you could use an AnimationListener to do something on certain event, like at the end of the animation

                    @Override

                    public void onAnimationStart(Animation animation) {


                    }


                    @Override

                    public void onAnimationEnd(Animation animation) { //you probably want to something onAnimationEnd, otherwise the button will snap back into its original size.


                    }


                    @Override

                    public void onAnimationRepeat(Animation animation) {


                    }

                });


            }

        });

在 onAnimationEnd 中,您必须决定在动画结束时要执行的操作。只是几个想法:


@Override

public void onAnimationEnd(Animation animation) { //you probably want to something onAnimationEnd, otherwise the button will snap back into its original size.

    button.setScaleX(0.1f); //same size as in toScale size in the animator xml

    button.setScaleY(0.1f);

}

或者,如果您希望按钮变得不可见:


@Override

public void onAnimationEnd(Animation animation) {

    button.setVisibility(View.INVISIBLE);

}


查看完整回答
反对 回复 2023-11-10
  • 1 回答
  • 0 关注
  • 64 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信