Android学习——Tween动画
2015-09-14 22:02
507 查看
Tweened Animations:该类Animations提供了旋转、移动、伸展和淡出等效果。
Alpha——淡入淡出
Scale——缩放效果
Rotate——旋转
Translate——移动效果。
Animation有四个子类:AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation
1.AlphaAnimation
在布局文件中添加一个ImageView和一个Button:
方式1:程序里设置
方式二:配置文件里设置
在res中新建anim/alpha1.xml:
在程序中启动动画:
两种方式,运行效果相同。
2. RotateAnimation
newRotateAnimation(fromDegrees,toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue);
参数1:fromDegrees:旋转起始角度
参数2:toDegrees:旋转结束角度
以下4个参数用来设置旋转圆心:
参数3:pivotXType:旋转类型,表示以哪个位置为旋转的基点横坐标。
共有3个:
Animation.ABSOLUTE,一个绝对坐标
Animation.RELATIVE_TO_SELF 当前控件的左上角横坐标
Animation.RELATIVE_TO_PARENT.当前父控件的左上角横坐标 参数4:pivotXValue: 0-1f之间的一个float值,表示实际旋转时圆心相对基点的水平偏移量,0表示不偏移,0.5f是半个控件长度 ,1表示偏移当前控件宽度
参数5:pivotYType: 同参数3.
参数6:pivotYValue: 同参数4
具体使用也有两种方式:
rotate1.xml:
3. ScaleAnimation
scale.xml:
4. TranslateAnimation
trans1.xml:
5. AnimationSet
sets.xml:
6. Interpolator动画修改器
Interpolator:基本动画的修改器
AccelerateInterpolator:动画从开始到结束,变化率是一个加速的过程。
DecelerateInterpolator:动画从开始到结束,变化率是一个减速的过程。
CycleInterpolator:动画从开始到结束,变化率是循环给定次数的正弦曲线。
AccelerateDecelerateInterpolator:动画从开始到结束,变化率是先加速后减速的过程。
LinearInterpolator:动画从开始到结束,变化率是线性变化。
OvershootInterpolator:向前甩一定值后再回到原来位置
BounceInterpolator:动画结束的时候弹起
AnticipateInterpolator :开始的时候向后然后向前甩
AnticipateOvershootInterpolator :开始的时候向后然后向前甩一定值后返回最后的值
anim.setInterpolator(传入具体对象使用)
布局文件:
sets.xml:
java代码:
运行中效果:
6. LayoutAnimationController
布局文件:
controller.xml:
sets1.xml:
java代码:
运行效果:
Alpha——淡入淡出
Scale——缩放效果
Rotate——旋转
Translate——移动效果。
Animation有四个子类:AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation
1.AlphaAnimation
在布局文件中添加一个ImageView和一个Button:
<ImageView android:id="@+id/imgv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="97dp" android:src="@drawable/cyddz" /> <Button android:id="@+id/btn_alpha" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/imgv" android:layout_marginTop="126dp" android:text="Alpha动画" android:onClick="alpha"/>
ImageView imgv; Button btn_alpha; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imgv = (ImageView) this.findViewById(R.id.imgv); btn_alpha = (Button) this.findViewById(R.id.btn_alpha); }
方式1:程序里设置
public void alpha(View v){ //新建对象,透明度1到0.3 AlphaAnimation anim = new AlphaAnimation(1f, 0.3f); //设置周期 anim.setDuration(4000); //设置重复次数 anim.setRepeatCount(2); //设置重复类型:REVERSE翻转,RESTART重新开始 anim.setRepeatMode(Animation.REVERSE); //动画执行后,控件将停留在执行结束的状态 anim.setFillAfter(true); //设置延迟:2s后开始 anim.setStartOffset(2000); //启动动画 imgv.startAnimation(anim); }
方式二:配置文件里设置
在res中新建anim/alpha1.xml:
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="4000" android:fillAfter="true" android:fromAlpha="1.0" android:repeatCount="2" android:repeatMode="reverse" android:startOffset="2000" android:toAlpha="0.3" > </alpha>
在程序中启动动画:
//方式二: AlphaAnimation anim = (AlphaAnimation) AnimationUtils.loadAnimation(this, R.anim.alpha1); imgv.startAnimation(anim);
两种方式,运行效果相同。
2. RotateAnimation
newRotateAnimation(fromDegrees,toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue);
参数1:fromDegrees:旋转起始角度
参数2:toDegrees:旋转结束角度
以下4个参数用来设置旋转圆心:
参数3:pivotXType:旋转类型,表示以哪个位置为旋转的基点横坐标。
共有3个:
Animation.ABSOLUTE,一个绝对坐标
Animation.RELATIVE_TO_SELF 当前控件的左上角横坐标
Animation.RELATIVE_TO_PARENT.当前父控件的左上角横坐标 参数4:pivotXValue: 0-1f之间的一个float值,表示实际旋转时圆心相对基点的水平偏移量,0表示不偏移,0.5f是半个控件长度 ,1表示偏移当前控件宽度
参数5:pivotYType: 同参数3.
参数6:pivotYValue: 同参数4
具体使用也有两种方式:
//方式一: //新建对象,旋转角度从0到90,默认逆时针旋转,旋转中心为图片的左上角 //RotateAnimation anim = new RotateAnimation(0f,90f); //RotateAnimation anim = new RotateAnimation(0, 90, 25, 25); RotateAnimation anim = new RotateAnimation(0,90,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); //设置周期 anim.setDuration(4000); //设置重复次数 anim.setRepeatCount(2); //设置重复类型:REVERSE翻转,RESTART重新开始 anim.setRepeatMode(Animation.REVERSE); //动画执行后,控件将停留在执行结束的状态 anim.setFillAfter(true); //设置延迟:1s后开始 anim.setStartOffset(1000); //启动动画 imgv.startAnimation(anim); //方式二: RotateAnimation anim = (RotateAnimation) AnimationUtils.loadAnimation(this, R.anim.rotate1); imgv.startAnimation(anim);
rotate1.xml:
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="4000" android:fillAfter="true" android:fromDegrees="0" android:pivotX="0.5" android:pivotY="0.5" android:repeatCount="2" android:repeatMode="reverse" android:startOffset="1000" android:toDegrees="90" > </rotate>
3. ScaleAnimation
scale.xml:
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="4000" android:fillAfter="true" android:fromXScale="1" android:fromYScale="1" android:pivotX="0.5" android:pivotY="0.5" android:repeatCount="2" android:repeatMode="reverse" android:startOffset="1000" android:toXScale="0.5" android:toYScale="0.5" > </scale>
//方式一: //新建对象,宽和高分别从1到0.5,默认图片的左上角为(0,0)点 //ScaleAnimation anim = new ScaleAnimation(1f,0.5f, 1f, 0.5f); //ScaleAnimation anim = new ScaleAnimation(1f,0.5f, 1f, 0.5f,25,25); ScaleAnimation anim = new ScaleAnimation(1.0f,0.5f,1.0f,0.5f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); //设置周期 anim.setDuration(4000); //设置重复次数 anim.setRepeatCount(2); //设置重复类型:REVERSE翻转,RESTART重新开始 anim.setRepeatMode(Animation.REVERSE); //动画执行后,控件将停留在执行结束的状态 anim.setFillAfter(true); //设置延迟:1s后开始 anim.setStartOffset(1000); //启动动画 imgv.startAnimation(anim); //方式二: // ScaleAnimation anim = (ScaleAnimation) AnimationUtils.loadAnimation(this, R.anim.scale1); // imgv.startAnimation(anim);
4. TranslateAnimation
trans1.xml:
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="100%" android:fromYDelta="0" android:toYDelta="100%" android:fillAfter="true" android:duration="4000" android:repeatCount="2" android:repeatMode="reverse" android:startOffset="1000" > </translate>
//方式一: //新建对象,设置起始位置的x,y和终止位置的x,y TranslateAnimation anim=new TranslateAnimation( 0, -100, 0, 100); // TranslateAnimation anim=new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, // Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0, // Animation.RELATIVE_TO_SELF, 1f); //设置周期 anim.setDuration(4000); //设置重复次数 anim.setRepeatCount(2); //设置重复类型:REVERSE翻转,RESTART重新开始 anim.setRepeatMode(Animation.REVERSE); //动画执行后,控件将停留在执行结束的状态 anim.setFillAfter(true); //设置延迟:1s后开始 anim.setStartOffset(1000); //启动动画 imgv.startAnimation(anim); //方式二: // TranslateAnimation anim = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.trans1); // imgv.startAnimation(anim);
5. AnimationSet
sets.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <rotate android:duration="3000" android:fromDegrees="0" android:repeatCount="-1" android:repeatMode="reverse" android:toDegrees="90" /> <alpha android:duration="3000" android:fromAlpha="1" android:repeatCount="-1" android:repeatMode="reverse" android:toAlpha="0.5" /> </set>
public void sets(View v){ //创建四种动画 ScaleAnimation anim1 = new ScaleAnimation(1f, 0.5f, 1f, 0.5f, 35, 35); TranslateAnimation anim2 = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1f); AlphaAnimation anim3 = new AlphaAnimation(1.0f, 0.3f); RotateAnimation anim4 = new RotateAnimation(0,360); //设置重复,只能一个个设置,不能给动画集整体设置 setReapeat(anim1); setReapeat(anim2); setReapeat(anim3); setReapeat(anim4); //创建动画集,并添加四种动画 AnimationSet as = new AnimationSet(false); as.addAnimation(anim1); as.addAnimation(anim2); as.addAnimation(anim3); as.addAnimation(anim4); //设置动画集整体其他效果 as.setDuration(4000); as.setFillAfter(true); //启动动画 imgv.startAnimation(as); //方式二: // Animation anim=AnimationUtils.loadAnimation(this, R.anim.sets); // imgv.startAnimation(anim); } public void setReapeat(Animation ani) { ani.setRepeatCount(2); ani.setRepeatMode(Animation.REVERSE); }
6. Interpolator动画修改器
Interpolator:基本动画的修改器
AccelerateInterpolator:动画从开始到结束,变化率是一个加速的过程。
DecelerateInterpolator:动画从开始到结束,变化率是一个减速的过程。
CycleInterpolator:动画从开始到结束,变化率是循环给定次数的正弦曲线。
AccelerateDecelerateInterpolator:动画从开始到结束,变化率是先加速后减速的过程。
LinearInterpolator:动画从开始到结束,变化率是线性变化。
OvershootInterpolator:向前甩一定值后再回到原来位置
BounceInterpolator:动画结束的时候弹起
AnticipateInterpolator :开始的时候向后然后向前甩
AnticipateOvershootInterpolator :开始的时候向后然后向前甩一定值后返回最后的值
anim.setInterpolator(传入具体对象使用)
布局文件:
<RelativeLayout 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" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv1" android:layout_marginLeft="32dp" android:layout_marginTop="152dp" android:layout_toRightOf="@+id/tv1" android:onClick="go" android:text="开始" /> <TextView android:id="@+id/tv1" android:layout_width="50dp" android:layout_height="30dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="84dp" android:background="#ff0000" android:text="xxxxx" /> </RelativeLayout>
sets.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/bounce_interpolator" android:shareInterpolator="true" > <rotate android:duration="3000" android:fromDegrees="0" android:repeatCount="-1" android:repeatMode="reverse" android:toDegrees="90" /> <alpha android:duration="3000" android:fromAlpha="1" android:repeatCount="-1" android:repeatMode="reverse" android:toAlpha="0.5" /> </set>
java代码:
public void go(View v){ //方式一: // AnimationSet as=new AnimationSet(true); // Interpolator inter=new BounceInterpolator (); // as.setInterpolator(inter); // TranslateAnimation anim2 = new TranslateAnimation( // Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.85f, // Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0f); // anim2.setDuration(3000); // anim2.setRepeatCount(-1); // anim2.setRepeatMode(Animation.REVERSE); // // as.addAnimation(ani2); // tv.startAnimation(as); //方式二: Animation anim=AnimationUtils.loadAnimation(this, R.anim.sets); tv.startAnimation(anim); }
运行中效果:
6. LayoutAnimationController
布局文件:
<ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView>
controller.xml:
<?xml version="1.0" encoding="utf-8"?> <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:animation="@anim/sets1" android:delay="1" />
sets1.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <rotate android:duration="3000" android:fromDegrees="90" android:repeatCount="0" android:toDegrees="0" /> <alpha android:duration="3000" android:fromAlpha="0" android:toAlpha="1" /> <translate android:duration="3000" android:fromXDelta="100%" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="0" /> </set>
java代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv = (ListView) this.findViewById(R.id.listView1); String[] strs = new String[15]; for (int i = 0; i < strs.length; i++) { strs[i] = "xxxxxxxxxx" + i; } ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strs); lv.setAdapter(adapter); //方式一 // TranslateAnimation anim2 = new TranslateAnimation( // Animation.RELATIVE_TO_PARENT, 1f, Animation.RELATIVE_TO_PARENT, 0f, // Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0f); // anim2.setDuration(3000); // // LayoutAnimationController controller=new LayoutAnimationController(anim2); // lv.setLayoutAnimation(controller); // controller.start(); // //lv.startLayoutAnimation(); //------------------------------------ //方式二 LayoutAnimationController controller=AnimationUtils.loadLayoutAnimation(this, R.anim.controller); lv.setLayoutAnimation(controller); controller.start(); }
运行效果:
相关文章推荐
- Android 中的inflate转自安卓中文网作者:kezz 来源:Android中文网
- Gradle in Android Studio (3) - 项目中的Gradle
- Android自己定义组件系列【8】——面膜文字动画
- android学习之路之listView的优化问题
- Android DbUtils之查询数据库
- android listview中getView重复被调用的问题
- Gradle in Android Studio (2) - 配置Gradle
- Android Studio 使用第三方类库(加载远程依赖)
- Android JNI机制
- Android-对话框
- Android ViewManger解析 从ViewRoot 源码分析invalidate
- Android View 中invalidate() 你所不知道的那些事
- Gradle in Android Studio (1) - 构建系统概述
- Android中如何获取屏幕的高度和宽度
- PackageManager的使用
- Android获取手机最近任务列表
- android实现沉浸式状态栏
- Android(java)学习笔记250:ContentProvider使用之获得系统联系人信息02(掌握)
- Android——Handler和AsyncTask的使用
- Android--Touch 事件的分发和消费机制