您可以點擊 按鈕設置alpha動畫。你必須把輝光當做背景來使用按鈕,當你按下按鈕時,按鈕的背景將會像動畫一樣重複動畫,並重復計數1,這樣看起來陰影就會出現,並且 消失。集動畫時間爲300ms
在動畫文件夾alpha_animation.xml
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
</set>
佈局文件就像波紋管
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnGlowBg"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:background="@drawable/drawable_glow"
android:padding="20dp"
/>
<Button
android:id="@+id/btnPinButton"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:padding="20dp"
android:text="10"/>
</RelativeLayout>
活動代碼
public class TestActivity extends AppCompatActivity {
Button btnGlowBg;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_test);
btnGlowBg = (Button) findViewById(R.id.btnGlowBg);
btnGlowBg.setVisibility(View.GONE);
findViewById(R.id.btnPinButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnGlowBg.setVisibility(View.VISIBLE);
final Animation startAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha_animation);
btnGlowBg.startAnimation(startAnimation);
}
});
}
}