一、概念
透明度动画,子类:AlphaAnimation,标签:<alpha>
它可以使View具有透明度的动画效果。
二、实现
1. XML实现
<alpha>标签常用属性如下:
android:fromAlpha:透明度起始值,比如0.1;
android:toAlpha:透明度结束值,比如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. 代码实现
构造方法:
public AlphaAnimation(float fromAlpha, float toAlpha)
参数说明:
fromAlpha:透明度起始值,比如0.1;
toAlpha:透明度结束值,比如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();
}