亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用戶按下的按鈕在按下時縮?。?/h1>

如何使用戶按下的按鈕在按下時縮???

瀟湘沐 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 關注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號