android动画集合
2016-03-30 16:19
453 查看
1.补间动画
无需逐一定义每一帧,只要定义开始、结束的帧,和指定动画持续时间。
补间动画有4种(均为Animation抽象类子类):
AlphaAnimation(透明度,0~1)
ScaleAnimation(大小缩放,X、Y轴缩放,还包括缩放中心pivotX、pivotY)
TranslationAnimation(位移,X、Y轴位移)
RotateAnimation(旋转,包括缩放中心pivotX、pivotY)
指定3个信息后,动画是匀速的,效果同逐帧动画。
上例中还有一个属性,可以控制速度,即为Interpolator(插值),有以下几种(Interpolator的实现类):
LinearInterpolator(匀速)
AccelerateInterpolator(先慢后快)
AccelerateDecelerateInterpolator(先慢中快后慢)
DecelerateInterpolator(先快后慢)
CycleInterpolator(循环播放,速度为正弦曲线)
AnticipateInterpolator(先回撤,再匀速向前)
OvershootInterpolator(超过,拉回)
BounceInterpolator(回弹)
不仅可以在代码中创建Animation对象,很多情况下,是采用动画资源文件来定义补间动画。资源目录:res/anim/anim.xml
然后在代码中使用AnimationUtils工具类加载动画资源,返回一个Animation对象
2.属性动画
继承关系:
Animator(属性动画的基类)→ValueAnimator→ObjectAnimator
如何使用:
1.调用ObjectAnimator的静态工厂方法创建动画(ofInt、ofFloat、ofObject)
2.调用SetXxx()设置动画持续时间、插值方式、重复次数等。
3.监听事件
4.调用Animator对象的start()方法启动动画
利用addListener(listener)给Animator对象设置监听器。监听事件有很多种,如果参数直接给一个
3.属性动画实例(点击父菜单图标,弹出多个子菜单图标)
文/chenlijian(简书作者)
原文链接:http://www.jianshu.com/p/ec62b81df7ed
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
无需逐一定义每一帧,只要定义开始、结束的帧,和指定动画持续时间。
补间动画有4种(均为Animation抽象类子类):
AlphaAnimation(透明度,0~1)
ScaleAnimation(大小缩放,X、Y轴缩放,还包括缩放中心pivotX、pivotY)
TranslationAnimation(位移,X、Y轴位移)
RotateAnimation(旋转,包括缩放中心pivotX、pivotY)
public void move(View view){ //定义一个位移补间动画,X轴从0变化到100,Y轴不变 TranslateAnimation animation = new TranslateAnimation(0, 200, 0, 0); //设置动画持续时间 animation.setDuration(1000); //设置动画结束后效果保留 animation.setFillAfter(true); //控制动画先慢后快 animation.setInterpolator(new AccelerateInterpolator()); //找到对象,开启动画 mImageView = (ImageView) findViewById(R.id.imageView1); mImageView.startAnimation(animation);
指定3个信息后,动画是匀速的,效果同逐帧动画。
上例中还有一个属性,可以控制速度,即为Interpolator(插值),有以下几种(Interpolator的实现类):
LinearInterpolator(匀速)
AccelerateInterpolator(先慢后快)
AccelerateDecelerateInterpolator(先慢中快后慢)
DecelerateInterpolator(先快后慢)
CycleInterpolator(循环播放,速度为正弦曲线)
AnticipateInterpolator(先回撤,再匀速向前)
OvershootInterpolator(超过,拉回)
BounceInterpolator(回弹)
不仅可以在代码中创建Animation对象,很多情况下,是采用动画资源文件来定义补间动画。资源目录:res/anim/anim.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 定义缩放动画 --> <scale android:fromXScale="1.o" android:fromYScale="1.0" android:toXScale="0.01" android:toYScale="0.01" android:pivotX="50%" android:pivotY="50%" android:fillAfter="true" android:duration="1000"/> <!-- 定义透明度动画 --> <alpha android:fromAlpha="1" android:toAlpha="0.05" android:duration="3000"/> <!-- 定义旋转动画 --> <rotate android:fromDegrees="0" android:toDegrees="1800" android:pivotX="50%" android:pivotY="50%" android:duration="2000"/> </set>
然后在代码中使用AnimationUtils工具类加载动画资源,返回一个Animation对象
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim);
2.属性动画
继承关系:
Animator(属性动画的基类)→ValueAnimator→ObjectAnimator
如何使用:
1.调用ObjectAnimator的静态工厂方法创建动画(ofInt、ofFloat、ofObject)
2.调用SetXxx()设置动画持续时间、插值方式、重复次数等。
3.监听事件
4.调用Animator对象的start()方法启动动画
//标准示范,单个动画属性(ObjectAnimator)执行 public void move1(View view){ //直接调用ObjectAnimator的工厂方法(静态方法),可以创建ObjectAnimator实例 //前2个参数分别要指定:操作的具体的对象、操作的对象的属性。本例中,具体对象为ImageView,属性为X轴位移 //第3个参数,是操作的对象的属性,其动画变化的范围的。本例中,X轴位移:0f~200f ObjectAnimator.ofFloat(mImageView, "translationX", 0f,200f) .setDuration(2000) .start(); } //同一个对象设置多个属性动画,会同时执行。可见,属性动画实例调用start方法后,是一个异步的过程 public void move2(View view){ ObjectAnimator.ofFloat(mImageView, "translationX", 0f,200f).setDuration(2000).start(); ObjectAnimator.ofFloat(mImageView, "translationY", 0f,200f).setDuration(2000).start(); ObjectAnimator.ofFloat(mImageView, "rotation", 0f,360f).setDuration(2000).start(); } //(多个动画同时时推荐),使用PropertyValuesHolder,先将多个属性动画hold住,再一起开启动画,可以实现同样的效果。如此做法对动画进行了优化,使用多个属性的时候,更加节省系统资源。 public void move3(View view){ PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("translationX", 0f,200f); PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY", 0f,200f); PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("rotation", 0f,360f); ObjectAnimator.ofPropertyValuesHolder(mImageView, p1,p2,p3).setDuration(2000).start(); } //(多个动画需求不同时时推荐),利用AnimatorSet,组合多个Animation,可以对多个动画属性进行顺序控制 //同时执行:set.playTogether(animator1,animator2,animator3) //顺序执行:set.playSequentially(animator1,animator2,animator3) //分布执行:play().with(); play().after(); public void move4(View view){ ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageView, "translationX", 0f,200f); ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageView, "translationY", 0f,200f); ObjectAnimator animator3 = ObjectAnimator.ofFloat(mImageView, "rotation", 0f,360f); AnimatorSet set = new AnimatorSet(); set.play(animator1).with(animator2); set.play(animator3).after(animator1); set.setDuration(1000).start(); }
利用addListener(listener)给Animator对象设置监听器。监听事件有很多种,如果参数直接给一个
new AnimatorListener,系统会直接override全部的监听事件让我们覆写
onAnimationStart
onAnimationRepeat
onAnimationEnd
onAnimationCancel。如果只想监听个别事件,参数写成
new AnimatorListenerAdapter,然后Alt+Shift+S,选择需要覆写的实现方法即可。
//对动画设置监听事件 public void move5(View view){ ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 0f,1.0f); animator.setDuration(1000).start(); animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); mImageView.setVisibility(View.VISIBLE); ObjectAnimator.ofFloat(mImageView, "alpha", 0f,1.0f).setDuration(1000).start(); } }); }
3.属性动画实例(点击父菜单图标,弹出多个子菜单图标)
public class AnimatorPath extends Activity implements OnClickListener{ private int [] res = {R.id.imageView0,R.id.imageView1,R.id.imageView2, R.id.imageView3,R.id.imageView4,R.id.imageView5, R.id.imageView6,R.id.imageView7,}; private List<ImageView> imageViewList = new ArrayList<ImageView>(); private boolean flag = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.animator_path); //用集合的方式遍历ImageView,并添加进集合 for(int i =0 ; i< res.length; i++){ ImageView imageView =(ImageView) findViewById(res[i]); imageView.setOnClickListener(this); imageViewList.add(imageView); } } //给帧布局文件中ImageView添加点击事件(使用Animator动画,动画后的ImageView也可以设置点击监听) @Override public void onClick(View v) { switch (v.getId()) { case R.id.imageView0: if(flag){ startAnim(); flag = false; }else{ endAnim(); flag = true; } break; default: break; } } //设置每个小球向前不同的距离,此处需求几个动画同时执行,就使用了最简单的实现方式 private void startAnim() { for(int i =0; i< res.length; i++){ ObjectAnimator animator = ObjectAnimator.ofFloat(imageViewList.get(i), "translationY",0f, 100f*i); animator.setDuration(500).setStartDelay(150*i); animator.setInterpolator(new BounceInterpolator()); animator.start(); } } private void endAnim() { for(int i = 0; i<res.length; i++){ ObjectAnimator animator = ObjectAnimator.ofFloat(imageViewList.get(i), "translationY", 100f*i,0f); animator.setDuration(250).setStartDelay(150*i); animator.start(); } } }
文/chenlijian(简书作者)
原文链接:http://www.jianshu.com/p/ec62b81df7ed
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories