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

我正在开发一款手机游戏,在游戏中,用户在按钮缩小之前按下按钮,为了让游戏变得“有趣”,按钮缩小的速度越来越快,所以当用户按下一个按钮时,另一个按钮就会弹出向上收缩得更快。到目前为止,我有这段代码可以处理用户输入和缩小按钮,但是,一旦按下,就没有动画,它会立即捕捉到“缩小”的尺寸,我不知道如何解决这个问题。


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 在开发移动应用程序方面我还是个新手。


潇湘沐
浏览 71回答 1
1回答

慕莱坞森

编辑忘记我在原来的帖子中写的内容了。请尝试下面的代码,让我知道这是否对您有帮助。final ValueAnimator valueAnimator = ValueAnimator.ofFloat(1.0f, 0.0f); //start and end value&nbsp; &nbsp; &nbsp; &nbsp; valueAnimator.setDuration(2000); //you can replace 2000 with a variable you can change dynamically&nbsp; &nbsp; &nbsp; &nbsp; valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onAnimationUpdate(ValueAnimator animation) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float animatedValue = (float) animation.getAnimatedValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; button.setScaleX(animatedValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; button.setScaleY(animatedValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; valueAnimator.start();&nbsp; &nbsp; &nbsp; &nbsp; button.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; valueAnimator.pause();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });原答案我会遵循 0X0nosugar 的建议。在 Android 文件树中的 res 目录下,添加 Android 资源目录(右键单击 res 文件夹 > 新建)并将其命名为“anim”。如果您使用该名称,Android Studio 可能会自动将其视为保存动画的文件夹。再次右键单击动画文件夹 > 新建 > 动画资源文件。将其命名为您想要的名称。在我的示例中,我将其命名为“button_animator”。您的文件树将如下所示:您的button_animator.xml 可能如下所示:<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">&nbsp; &nbsp; <scale&nbsp; &nbsp; &nbsp; &nbsp; android:interpolator="@android:anim/accelerate_decelerate_interpolator"&nbsp; &nbsp; &nbsp; &nbsp; android:fromXScale="1.0"&nbsp; &nbsp; &nbsp; &nbsp; android:toXScale="0.1"&nbsp; &nbsp; &nbsp; &nbsp; android:fromYScale="1.0"&nbsp; &nbsp; &nbsp; &nbsp; android:toYScale="0.1"&nbsp; &nbsp; &nbsp; &nbsp; android:pivotX="50%"&nbsp; &nbsp; &nbsp; &nbsp; android:pivotY="50%"&nbsp; &nbsp; &nbsp; &nbsp; android:fillAfter="false"&nbsp; &nbsp; &nbsp; &nbsp; android:duration="500" /></set>您可以使用以下几行定制按钮的最终比例:android:toXScale="0.1"和android:toYScale="0.1"在代码中,您可以动态定制动画:button.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Animation shrinkButton = AnimationUtils.loadAnimation(MainActivity.this, R.anim.button_animator); //reference the animator&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shrinkButton.setDuration(5000); //dynamically set the duration of your animation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; button.startAnimation(shrinkButton); //start the animation. Since it is inside an onclicklistener, the animation start on a button click event&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shrinkButton.setAnimationListener(new Animation.AnimationListener() { //you could use an AnimationListener to do something on certain event, like at the end of the animation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onAnimationStart(Animation animation) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onAnimationEnd(Animation animation) { //you probably want to something onAnimationEnd, otherwise the button will snap back into its original size.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onAnimationRepeat(Animation animation) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });在 onAnimationEnd 中,您必须决定在动画结束时要执行的操作。只是几个想法:@Overridepublic void onAnimationEnd(Animation animation) { //you probably want to something onAnimationEnd, otherwise the button will snap back into its original size.&nbsp; &nbsp; button.setScaleX(0.1f); //same size as in toScale size in the animator xml&nbsp; &nbsp; button.setScaleY(0.1f);}或者,如果您希望按钮变得不可见:@Overridepublic void onAnimationEnd(Animation animation) {&nbsp; &nbsp; button.setVisibility(View.INVISIBLE);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java