Tween动画的AlphaAnimation动画 :是通过改变View的透明度来实现动画效果的
常用的构造方法如下:
AlphaAimation(float fromAlpha , float toAlpha )
【参数说明】:
fromAlpha : 动画开始的透明度 一般是0.0f 最小的透明度
toAlpha :动画结束的透明度 可以使0.0f~1.0f 1.0f为最大的透明度了
【常用方法】
1: setDuration(4000) ******* 设置动画持续的时间 时间为毫秒 4000为4秒
2:startNow() ************动画立即开始
3: cancel () **********动画立即结束
好了 废话不说了 马上上代码吧
XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${packageName}.${activityClass}" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start"
android:onClick="start"
android:background="@drawable/button"
android:id="@+id/button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stop"
android:onClick="stop"
android:background="@drawable/button"
android:id="@+id/button2"/>
</LinearLayout>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ball"/>
</LinearLayout>
上面就定义了两个Button ,一个用于开始动画 、一个用于停止动画、还有一个ImageView、动画的实行
Java代码
package com.android.my4;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener{
/**
*透明度动画
/*
private ImageView image;
private Button start; //声明所用到的View
private Button stop;
private AlphaAnimation alpha;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView)findViewById(R.id.image); // 通过Id获取组件
start = (Button)findViewById(R.id.button1);
stop = (Button)findViewById(R.id.button2);
alpha = new AlphaAnimation(0.0f, 1.0f); // 设置透明度动画的配置 从透明度0.0f到1.0f
}
//定义单击start按钮的监听器 并定义实现动画开始方法
public void start(View e){
alpha.setDuration(5000); //设置动画的持续时间为5秒
start.startAnimation(alpha); // 开始动画
}
public void stop(View e){ //定义停止动画的方法
alpha.cancel(); //动画停止
}
public void onClick(View v) {
}
}
好了 一个简单地透明图动画就写好 其实很简单的
上面的动画只有一个 其实可以给一个View添加几个动画效果的
这就要用到AnimationSet动画集合 类
【方法】 add() 方法
AnimationSet set = new AnimationSet();
set.add(Animation animation ) ******** 添加动画模式
【方法】 start() 方法
set.start();
好了 下一遍在介绍AnimationSet动画结合类