Android中的动画
最近一直在学习Android语言的基础知识,在视频中看到了关于Android动画的讲解,就把它记下来,方便日后工作需要。Android的动画大致分两种。一种叫做补间动画(View动画)、一种叫做属性动画。两种动画的不同点:补间动画的效果不会改变控件的真实坐标。属性动画的特点是可以改变组件的真实坐标。同种动画的相同点:都会动。
先介绍一下补间动画的
1、透明动画 AlphaAnimation
//1.0意味着完全不透明,0.0意味着完全透明
AlphaAnimation animation=new AlphaAnimation(1.0f,0.0f);
//设置动画执行时间
animation.setDuration(2000);
//这是动画重复次数
animation.setRepeatCount(1);
//设置重复的模式
animation.setRepeatMode(Animation.REVERSE);
//开启动画
imageView.startAnimation(animation);
2、旋转动画 RotateAnimation
//开始旋转的角度到结束的角度
//RotateAnimation animation=new RotateAnimation(0,360);
//开始旋转的角度到结束的角度,旋转点 X轴、Y轴
RotateAnimation animation=new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//设置动画执行时间
animation.setDuration(2000);
//这是动画重复次数
animation.setRepeatCount(1);
//设置重复的模式
animation.setRepeatMode(Animation.REVERSE);
//开启动画
imageView.startAnimation(animation);
3、缩放动画 ScaleAnimation
ScaleAnimation animation=new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//设置动画执行时间
animation.setDuration(2000);
//这是动画重复次数
animation.setRepeatCount(1);
//设置重复的模式
animation.setRepeatMode(Animation.REVERSE);
//开启动画
imageView.startAnimation(animation);
4、位移动画 TranslateAnimation
TranslateAnimation animation=new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT,0.2f);
//设置动画执行时间
animation.setDuration(2000);
animation.setFillAfter(true);
//开启动画
imageView.startAnimation(animation);
属性动画:ObjectAnimator
//位移动画
public void translate(View v){
//创建属性动画
/**
* target 执行的目标 谁执行动画
* propertyName 属性名字 The name of the property being animated.
* float... values 可变参数
*/
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 10, 50,20,150);
oa.setDuration(2000);
oa.start(); //开始动画
}
//缩放动画
public void scale(View v){
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleY", 0.1f, 2, 1, 2);
oa.setDuration(2000);
oa.start();
}
//实现透明的效果
public void alpha(View v){
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.5f, 0, 1,0,1);
oa.setDuration(2000);
oa.start();
}
//实现旋转的效果
public void rotate(View v){
// ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotation", 0, 180, 90, 360);
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotationY", 0, 180, 90, 360);
oa.setDuration(2000);
oa.start();
}
//一起飞
public void fly(View v){
AnimatorSet as = new AnimatorSet();
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 10, 50, 20, 100);
ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "scaleY", 0.1f, 2, 1, 2);
ObjectAnimator oa3 = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.5f, 0, 1);
ObjectAnimator oa4 = ObjectAnimator.ofFloat(iv, "rotationY", 0, 180, 90, 360);
as.setDuration(2000);
as.setTarget(iv);
//往集合中添加动画
//挨个飞
as.playSequentially(oa, oa2, oa3, oa4);
//一起飞
// as.playTogether(oa, oa2, oa3, oa4);
as.start();
}
//使用xml的方式创建属性动画
public void playxml(View v){
ObjectAnimator oa = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.oanimator);
//设置执行目标
oa.setTarget(iv);
oa.start();//开始执行
}