一驾胆、概念
透明度動(dòng)畫(huà)渠啤,子類(lèi):AlphaAnimation,標(biāo)簽:<alpha>
它可以使View具有透明度的動(dòng)畫(huà)效果扑媚。
二腰湾、實(shí)現(xiàn)
1. XML實(shí)現(xiàn)
<alpha>標(biāo)簽常用屬性如下:
android:fromAlpha:透明度起始值,比如0.1疆股;
android:toAlpha:透明度結(jié)束值费坊,比如1。
//res/anim/alpha_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fromAlpha="0.1"
android:toAlpha="1"
android:duration="3000"
android:repeatCount="2"
/>
//代碼旬痹,MAinActivity
private void alphaAnimationXML() {
Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha_animation);
mAnimate_tv.setAnimation(alphaAnimation);
mAnimate_tv.startAnimation(alphaAnimation);
}
private void stopAnimation() {
mAnimate_tv.clearAnimation();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
alphaAnimationXML();
}
@Override
protected void onDestroy() {
super.onDestroy();
stopAnimation();
}
2. 代碼實(shí)現(xiàn)
構(gòu)造方法:
public AlphaAnimation(float fromAlpha, float toAlpha)
參數(shù)說(shuō)明:
fromAlpha:透明度起始值附井,比如0.1;
toAlpha:透明度結(jié)束值两残,比如1永毅。
//布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500px"
android:layout_height="500px"
android:layout_marginLeft="100px"
android:background="@color/colorAccent">
<TextView
android:id="@+id/animate_tv"
android:layout_width="wrap_content"
android:layout_height="200px"
android:gravity="center"
android:layout_centerInParent="true"
android:textColor="@color/colorAccent"
android:text="Hello Android!"
android:background="@color/colorPrimaryDark"/>
</RelativeLayout>
//代碼,MainActivity
private void alphaAnimationCode() {
mAnimation = new AlphaAnimation(0.1f, 1f);
mAnimation.setDuration(3000);
mAnimation.setRepeatCount(2);
mAnimation.setFillAfter(true);
mAnimate_tv.setAnimation(mAnimation);
mAnimate_tv.startAnimation(mAnimation);
}
private void stopAnimation() {
mAnimate_tv.clearAnimation();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
alphaAnimationCode();
}
@Override
protected void onDestroy() {
super.onDestroy();
stopAnimation();
}