1.main.xml
四个按钮,一个图片,布局上,有点不一样。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:text="scale动画效果"
android:id="@+id/scaleButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</Button>
<Button
android:text="rotate动画效果"
android:id="@+id/rotateButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/scaleButtonId">
</Button>
<Button
android:text="alpha动画效果"
android:id="@+id/alphaButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/rotateButtonId">
</Button>
<Button
android:text="translate动画效果"
android:id="@+id/translate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/alphaButtonId">
</Button>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/imageViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="100dp"
android:src="@drawable/icon"/>
</LinearLayout>
</RelativeLayout>
2.声明对象,获取对象,绑定监听器
3.最简单的一个,AlphaButtonListener,改变透明度
//创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//创建一个AlphaAnimation对象,从完全不透明(1)到完全透明(0)
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
//设置动画执行的时间,单位毫秒
alphaAnimation.setDuration(1000);
//将AlphaAnimation对象添加到AnimationSet当中
animationSet.addAnimation(alphaAnimation);
//使用imageView的startAnimation方法开始执行动画
imageView.startAnimation(animationSet);
4.TranslateButtonListener 移动图片
AnimationSet animationSet = new AnimationSet(true);
//以自己为参照,x轴移动半个自己的x长度,y轴移动一个自己的y长度
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(animationSet);
5.ScaleButtonListener 缩放图片
AnimationSet animationSet = new AnimationSet(true);
//x轴和y轴都变为原来的0.1,以自己为参照,0.5x长度,0.5y长度处(就是中心点)为中心缩放
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
animationSet.addAnimation(scaleAnimation);
animationSet.setDuration(2000);
imageView.startAnimation(animationSet);
5.RotateButtonListener 旋转图片
AnimationSet animationSet = new AnimationSet(true);
//从0度旋转到360度,以父控件为参照,一个父控件x长度和当前y的交点为圆心来旋转
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_PARENT, 0f);
rotateAnimation.setDuration(5000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
6.此外,四个常用的方法
//setDuration(long durationMills)设置动画持续时间,单位毫秒
//setFillAfter(boolean fillAfter)如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
//setFillBefore(boolean fillBefore)如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态
//setStartOffSet(long starytOffSet)设置动画执行之前的等待时间
//setRepeatCount(int repeatCount)设置动画重复执行的次数
代码