第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用戶按下的按鈕在按下時(shí)縮???

我正在開發(fā)一款手機(jī)游戲,在游戲中,用戶在按鈕縮小之前按下按鈕,為了讓游戲變得“有趣”,按鈕縮小的速度越來越快,所以當(dāng)用戶按下一個(gè)按鈕時(shí),另一個(gè)按鈕就會(huì)彈出向上收縮得更快。到目前為止,我有這段代碼可以處理用戶輸入和縮小按鈕,但是,一旦按下,就沒有動(dòng)畫,它會(huì)立即捕捉到“縮小”的尺寸,我不知道如何解決這個(gè)問題。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)線,因?yàn)槲艺J(rèn)為這就是它捕捉的原因(即它太快了,我只是沒有看到動(dòng)畫),但事實(shí)并非如此,因?yàn)閼?yīng)用程序只是掛起,直到按鈕的大小更新。PS 在開發(fā)移動(dòng)應(yīng)用程序方面我還是個(gè)新手。
查看完整描述

1 回答

?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

編輯


忘記我在原來的帖子中寫的內(nèi)容了。請(qǐng)嘗試下面的代碼,讓我知道這是否對(duì)您有幫助。


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();

            }

        });

原答案


我會(huì)遵循 0X0nosugar 的建議。


在 Android 文件樹中的 res 目錄下,添加 Android 資源目錄(右鍵單擊 res 文件夾 > 新建)并將其命名為“anim”。如果您使用該名稱,Android Studio 可能會(huì)自動(dòng)將其視為保存動(dòng)畫的文件夾。再次右鍵單擊動(dòng)畫文件夾 > 新建 > 動(dòng)畫資源文件。將其命名為您想要的名稱。在我的示例中,我將其命名為“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"

在代碼中,您可以動(dòng)態(tài)定制動(dòng)畫:


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 中,您必須決定在動(dòng)畫結(jié)束時(shí)要執(zhí)行的操作。只是幾個(gè)想法:


@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);

}


查看完整回答
反對(duì) 回復(fù) 2023-11-10
  • 1 回答
  • 0 關(guān)注
  • 173 瀏覽

添加回答

了解更多

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)