【Android学习】Android动画效果--Animations(2)
来源:互联网 发布:淘宝店铺很少自然流量 编辑:程序博客网 时间:2024/05/19 22:26
在Android开发中,很可能需要用到一些复杂的动画,而前面所学到的四个基本动画不能满足需要,则此时这个控件需要绑定多个动画,比如某张图片需要在缩小的同时淡出,这时就需要绑定Alpha和Scale。也就是AnimationSet。
当然,有时候也需要用到Interpolator,而对于Interpolator:
Interpolator定义了动画变化的速率,在Animations框架当中定义了以下几种Interpolator
1、AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候加速
2、AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速
3、CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线
4、DecelerateInterpolator:在动画开始的时候速率改变比较慢,然后开始减速
5、LinearInterpolator:动画以匀速开始改变
在绑定多种动画以及设定Interpolator有两种方法:
1、在java代码中设定:
AnimationSet animationSet = new AnimationSet(false);AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);alpha.setInterpolator(new DecelerateInterpolator());RotateAnimation rotate = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);rotate.setInterpolator(new AccelerateInterpolator());animationSet.addAnimation(alpha);animationSet.addAnimation(rotate);animationSet.setDuration(2000);animationSet.setStartOffset(500);imageView.startAnimation(animationSet);
2、在xml文件中设定:
alpha.xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"android:shareInterpolator="true"><alpha android:fromAlpha="1.0"android:toAlpha="0.0"android:startOffset="500"android:duration="2000" /><rotate android:fromDegrees="0"android:toDegrees="360"android:pivotX="50%"android:pivotY="50%"android:duration="2000" /></set>
MainActivity.java:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); imageView.startAnimation(animation);
<------------------------------------------------------------------------------>
Frame-By-Frame Animations的使用方法
1、在res/drawable当中创建一个XML文件,用于定义Animations的动画序列
<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false"><item android:drawable="@drawable/animation1" android:duration="500" /><item android:drawable="@drawable/animation2" android:duration="500" /><item android:drawable="@drawable/animation3" android:duration="500" /><item android:drawable="@drawable/animation4" android:duration="500" /></animation-list>
2、为ImageView设置背景资源
imageView.setBackgroundResource(R.drawable.anim_nv);
3、通过InageView得到AnimationDrawable
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
4、开始执行动画
animationDrawable.start();
具体实现如下:
MainAcitivity.java:
public class MainActivity extends Activity {private Button button = null;private ImageView imageView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView = (ImageView)findViewById(R.id.imageViewId); button = (Button)findViewById(R.id.buttonId); button.setOnClickListener(new ButtonListener()); } private class ButtonListener implements OnClickListener{@Overridepublic void onClick(View v) {imageView.setBackgroundResource(R.drawable.anim_nv);AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();animationDrawable.start();} }}
anim_nv.xml:
<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false"><item android:drawable="@drawable/animation1" android:duration="500" /><item android:drawable="@drawable/animation2" android:duration="500" /><item android:drawable="@drawable/animation3" android:duration="500" /><item android:drawable="@drawable/animation4" android:duration="500" /></animation-list>
main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><Button android:id="@+id/buttonId" android:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_alignParentBottom="true"android:text="测试动画效果" /><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="100dip"/></LinearLayout></RelativeLayout>
0 0
- 【Android学习】Android动画效果--Animations(2)
- 【Android学习】Android动画效果--Animations(1)
- 【Android学习】Android动画效果--Animations(3)
- android Animations 动画效果(一)
- android Animations 动画效果(二)
- android Animations 动画效果(三)
- android Animations 动画效果(四)
- Android动画(Animations)
- android动画效果之Animations(代码实现动画)
- android Tween Animations(动画效果-代码实现)的使用
- Android学习笔记 - 动画篇 (Animations)
- Android Animations动画之简单学习
- Android学习之Animations动画介绍
- android之Animations(动画)
- Android Animations动画使用
- android动画Animations
- Android之 Animations动画
- Android Animations动画详解
- 提问的智慧
- 线程池
- 时间复杂度为O(N),空间复杂度为O(1)的排序算法
- 深复制体验(二)
- 爽翻了?360安全浏览器7.0速度体会!
- 【Android学习】Android动画效果--Animations(2)
- 两台windows笔记本创建家庭组实现文件共享
- ZOJ 3768(暴搜+数学)
- java备份mysql
- 笔试题
- Socket通信总结(附C++实现)
- 编写的windows程序,崩溃时产生crash dump文件的办法
- redis.conf中文版
- 用UltraEidt修改dmp文件把内容导到指定的表空间