AlphaAnimation(渐变透明度动画)
AlphaAnimation的类继承结构与ScaleAnimation类继承结构类似,这里就不写出来了。
AlphaAnimation一般用于制作淡入淡出效果。
AlphaAnimation有两个关键属性 android:fromAlpha (动画起始帧透明度)、android:toAlpha(动画尾帧透明度)。
废话不多说,下面通过实例来介绍AlphaAnimation的使用步骤(创建工程前先准备一张名为ting_frame_3的图片)。
1、在res/anim(若anim文件夹不存在,则创建该文件夹)下创建alpha_anim.xml , 内容如下
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1500"
android:fromAlpha="0.1"
android:repeatCount="-1"
android:repeatMode="reverse"
android:startOffset="0"
android:toAlpha="0.8" >
</alpha>
2、在res/layout下创建文件alpha_anim_layout.xml , 内容如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/alpha_anim_img"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/app_name"
android:src="@drawable/ting_frame_3"
android:layout_gravity="center" />
</LinearLayout>
3、用于显示动画的activity , AlphaAnimActivity.java内容如下
import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class AlphaAnimActivity extends Activity {
Animation mAlphaAnim ;
private ImageView mAlphaImg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alpha_anim_layout);
init();
mAlphaAnim = AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
}
private void init(){
mAlphaImg = (ImageView) findViewById(R.id.alpha_anim_img);
}
@Override
protected void onPause() {
super.onPause();
if (mAlphaAnim != null) {
mAlphaAnim.cancel();
}
}
@Override
protected void onResume() {
super.onResume();
if (mAlphaAnim != null) {
mAlphaImg.startAnimation(mAlphaAnim);
}
}
}
AnimationUtils.loadAnimation(this, R.anim.alpha_anim); 加载定义在res/anim下面的资源 alpha_anim.xml
mAlphaImg.startAnimation(mAlphaAnim); 对View启动动画
AlphaAnimActivity是应用打开时启动的第一个activity。
注意:动画的启动一般不放在onCreate函数中,若是放在onResume中依然出现动画停留在第一帧,则将动画的启动放到onWindowFocusChanged中。