Android动画分为Tween动画和Frame动画,近期学习了,体tween动画,现在讲学习的心得以及相关知识介绍如下。
Tween又称为补间动画,可以把对象进行缩小、放大、旋转和渐变等操作。
//设置播放时间 animation.setDuration(2000); //设置重复的次数,记着是重复的次数 animation.setRepeatCount(2); //设置重复的模式,有两种RESTART:重新开始与REVERSE:反向开始 animation.setRepeatMode(AlphaAnimation.REVERSE); //启动播放 iv.startAnimation(animation);
第三:实例,
布局文件我们这样写,
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.ftf.tween.MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:onClick="click" android:text="透明度" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" /> <Button android:onClick="click2" android:text="缩放" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" /> <Button android:onClick="click3" android:text="旋转" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" /> <Button android:onClick="click4" android:text="平移" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" /> <Button android:onClick="click5" android:text="组合" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <ImageView android:id="@+id/iv" android:src="@drawable/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
// 透明度变化
public void click(View view) { AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f); animation.setDuration(2000); animation.setRepeatCount(2); animation.setRepeatMode(AlphaAnimation.REVERSE); iv.startAnimation(animation); } public void click2(View view) { ScaleAnimation animation = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(2000); animation.setRepeatCount(2); animation.setRepeatMode(AlphaAnimation.REVERSE); iv.startAnimation(animation); } public void click3(View view) { RotateAnimation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(2000); animation.setRepeatCount(2); animation.setRepeatMode(AlphaAnimation.REVERSE); iv.startAnimation(animation); } public void click4(View view) { TranslateAnimation animation = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 1.0f); animation.setDuration(2000); animation.setRepeatCount(2); animation.setRepeatMode(AlphaAnimation.REVERSE); iv.startAnimation(animation); } public void click5(View view) { //设置混合模式,也即多重播放效果放在一起。 AnimationSet set = new AnimationSet(false); TranslateAnimation pa = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 1.0f); pa.setDuration(2000); pa.setRepeatCount(2); pa.setRepeatMode(AlphaAnimation.REVERSE); RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(2000); ra.setRepeatCount(2); ra.setRepeatMode(AlphaAnimation.REVERSE); ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(2000); sa.setRepeatCount(2); sa.setRepeatMode(AlphaAnimation.REVERSE); set.addAnimation(sa); set.addAnimation(ra); set.addAnimation(pa); iv.startAnimation(set); }