Animation图片渐变动画 Demo

最终实现效果:

main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
android:id="@+id/iv_animation_logo"
android:contentDescription="@string/animationContentDescription"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/animation_logo"/>

</RelativeLayout>

AnimationDemoActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.royal.animationDemo;  

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;

/**
* 图片渐变动画
*/
public class AnimationDemoActivity extends Activity {

public static final int ANIMATION_TIME = 5000;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// 去掉界面任务条
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);

//图片渐变模糊度始终
AlphaAnimation aa = new AlphaAnimation(0.1f,1.0f);
//渐变时间
aa.setDuration(ANIMATION_TIME);
//展示图片渐变动画
this.findViewById(R.id.iv_animation_logo).startAnimation(aa); //也可以使用setAnimation(aa);

//渐变过程监听
aa.setAnimationListener(new AnimationListener() {

/**
* 动画开始时
*/
@Override
public void onAnimationStart(Animation animation) {
System.out.println("动画开始...");
}

/**
* 重复动画时
*/
@Override
public void onAnimationRepeat(Animation animation) {
System.out.println("动画重复...");
}

/**
* 动画结束时
*/
@Override
public void onAnimationEnd(Animation animation) {
System.out.println("动画结束...");
}
});
}
}

string.xml

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>  
<resources>
<string name="app_name">AnimationDemo</string>
<string name="animationContentDescription">渐变图片动画描述</string>
</resources>

打印结果:

android中提供了4中动画:
AlphaAnimation 透明度动画效果
ScaleAnimation 缩放动画效果
TranslateAnimation 位移动画效果
RotateAnimation 旋转动画效果
四种动画及动画集合的使用见源码【百度网盘-AnimationDemo-0.2(Android动画)】

扩展阅读:Android 动画之AlphaAnimation应用详解