您好,登錄后才能下訂單哦!
在 Android 中,要實(shí)現(xiàn)按鈕(Button)的動畫序列播放,可以使用 AnimationSet
類來組合多個(gè)動畫
首先,在 res/anim
目錄下創(chuàng)建一個(gè)新的 XML 文件,例如 button_animation.xml
。如果 anim
目錄不存在,請創(chuàng)建它。
在 button_animation.xml
文件中,定義一個(gè) AnimationSet
,并添加多個(gè)動畫。例如,我們可以添加一個(gè)平移動畫和一個(gè)縮放動畫:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="1000"
android:fromXDelta="0%"
android:toXDelta="50%"
android:fillAfter="true" />
<scale
android:duration="1000"
android:fromXScale="1.0"
android:toXScale="1.5"
android:fromYScale="1.0"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true" />
</set>
Java 示例:
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
// ...
Button button = findViewById(R.id.my_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.button_animation);
button.startAnimation(animation);
}
});
Kotlin 示例:
import android.view.animation.AnimationUtils
// ...
val button: Button = findViewById(R.id.my_button)
button.setOnClickListener {
val animation: Animation = AnimationUtils.loadAnimation(applicationContext, R.anim.button_animation)
button.startAnimation(animation)
}
現(xiàn)在,當(dāng)用戶點(diǎn)擊按鈕時(shí),將按順序播放平移和縮放動畫。你可以根據(jù)需要添加更多動畫效果。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。