Android动画详解之Android动画属性和实现方法(一)
16lz
2021-01-26
动画简介
(一)、概要:
3.0以前,android支持两种动画模式,补间动画(tween animation),帧动画(frame animation),在android3.0中又引入了一个新的动画系统:属性动画(property animation)。
这三种动画模式在SDK中被称为view animation,drawable animation,property animation。
(二)、动画资源分类:
属性动画:Property Animation
帧动画:Frame Animation (Drawable Animation)
补间动画:Tween Animation (View Animation)
透明度补间动画
缩放补间动画
旋转补间动画
移动补间动画
(三)、补间动画
View Animation就是一系列View形状的变换,如大小的缩放、透明度的改变、水平位置的改变、旋转位置改变,动画的定义既可以用java代码定义也可以用XML定义。建议用XML定义。
用XML定义的动画放在/res/anim/文件夹内,XML文件的根元素为 , 二级节点可为,,,。
AlphaAnimation:透明度(alpha)渐变效果,对应标签。
TranslateAnimation:位移渐变,需要指定移动点的开始和结束坐标,对应 标签。
ScaleAnimation:缩放渐变,可以指定缩放的参考点,对应 标签。
RotateAnimation:旋转渐变,可以指定旋转的参考点,对应 标签。
AnimationSet:组合渐变,支持组合多种渐变效果,对应 标签。
动画插入器
AccelerateInterpolator 加速,开始时慢中间加速
DecelerateInterpolator 减速,开始时快然后减速
AccelerateDecelerateInterpolator 先加速后减速,开始结束时慢,中间加速
AnticipateInterpolator 反向,先向相反方向改变一段再加速播放
AnticipateOvershootInterpolator 反向加超越,先向相反方向改变,再加速播放,会超出目的值然后缓慢移动至目的值
BounceInterpolator 跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100
CycleIinterpolator 循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2* mCycles*Math.PI* input)
LinearInterpolator 线性,线性均匀改变
OvershottInterpolator 超越,最后超出目的值然后缓慢改变到目的值
(五)XML方式实现补间动画
移动
旋转
附上源码: 源码
(一)、概要:
3.0以前,android支持两种动画模式,补间动画(tween animation),帧动画(frame animation),在android3.0中又引入了一个新的动画系统:属性动画(property animation)。
这三种动画模式在SDK中被称为view animation,drawable animation,property animation。
(二)、动画资源分类:
属性动画:Property Animation
帧动画:Frame Animation (Drawable Animation)
补间动画:Tween Animation (View Animation)
透明度补间动画
缩放补间动画
旋转补间动画
移动补间动画
(三)、补间动画
View Animation就是一系列View形状的变换,如大小的缩放、透明度的改变、水平位置的改变、旋转位置改变,动画的定义既可以用java代码定义也可以用XML定义。建议用XML定义。
用XML定义的动画放在/res/anim/文件夹内,XML文件的根元素为
AlphaAnimation:透明度(alpha)渐变效果,对应标签。
TranslateAnimation:位移渐变,需要指定移动点的开始和结束坐标,对应
ScaleAnimation:缩放渐变,可以指定缩放的参考点,对应
RotateAnimation:旋转渐变,可以指定旋转的参考点,对应
AnimationSet:组合渐变,支持组合多种渐变效果,对应
动画插入器
AccelerateInterpolator 加速,开始时慢中间加速
DecelerateInterpolator 减速,开始时快然后减速
AccelerateDecelerateInterpolator 先加速后减速,开始结束时慢,中间加速
AnticipateInterpolator 反向,先向相反方向改变一段再加速播放
AnticipateOvershootInterpolator 反向加超越,先向相反方向改变,再加速播放,会超出目的值然后缓慢移动至目的值
BounceInterpolator 跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100
CycleIinterpolator 循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2* mCycles*Math.PI* input)
LinearInterpolator 线性,线性均匀改变
OvershottInterpolator 超越,最后超出目的值然后缓慢改变到目的值
(四)JAVA代码实现补间动画
每个都做了注释,使用和参数的填写
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ //点击TAG private final int ANIM_TRANSLATE = 0,ANIM_ROATE = 1,ANIM_SCALE = 2,ANIM_ALPH = 3,ANIM_ALL = 4; private Button button_translate,button_roate,button_scale,button_alpha,button_all; private ImageView myImage; private Animation animation; /** * 绑定 */ //private Animation animation; private void initView(){ button_translate = (Button) findViewById(R.id.button_translate); button_roate = (Button) findViewById(R.id.button_roate); button_scale = (Button) findViewById(R.id.button_scale); button_alpha = (Button) findViewById(R.id.button_alpha); button_all = (Button) findViewById(R.id.button_all); button_translate.setTag(ANIM_TRANSLATE); button_roate.setTag(ANIM_ROATE); button_scale.setTag(ANIM_SCALE); button_alpha.setTag(ANIM_ALPH); button_all.setTag(ANIM_ALL); myImage = (ImageView) findViewById(R.id.myImage); } /** * 点击事件 */ private void initCtrl(){ button_translate.setOnClickListener(this); button_alpha.setOnClickListener(this); button_roate.setOnClickListener(this); button_scale.setOnClickListener(this); button_all.setOnClickListener(this); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initCtrl(); } /** * 动画插入器 AccelerateInterpolator加速,开始时慢中间加速 DecelerateInterpolator减速,开始时快然后减速 AccelerateDecelerateInterolator先加速后减速,开始结束时慢,中间加速 AnticipateInterpolator反向,先向相反方向改变一段再加速播放 AnticipateOvershootInterpolator反向加超越,先向相反方向改变,再加速播放,会超出目的值然后缓慢移动至目的值 BounceInterpolator跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100 CycleIinterpolator循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2* mCycles*Math.PI* input) LinearInterpolator线性,线性均匀改变 OvershottInterpolator超越,最后超出目的值然后缓慢改变到目的值 */ private void TrandlateAnim(){ // 起始坐标x,y和终点坐标x,y animation = new TranslateAnimation(0,200,0,200); //3秒完成动画 animation.setDuration(2000); //如果fillAfter的值为真的话,动画结束后,控件停留在执行后的状态 animation.setFillAfter(false); animation.setInterpolator(new AccelerateInterpolator()); //启动动画 myImage.startAnimation(animation); //动画的监听 animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { Log.e("ceshi", "移动动画开始"); } @Override public void onAnimationEnd(Animation animation) { Log.e("ceshi", "移动动画结束"); } @Override public void onAnimationRepeat(Animation animation) { Log.e("ceshi", "移动动画重复"); } }); } /** * 旋转 */ private void RoateAnim(){ //旋转起始度数,旋转的点 animation = new RotateAnimation(0, 30, myImage.getWidth(), myImage.getHeight()); animation.setInterpolator(new DecelerateInterpolator()); animation.setDuration(2000); animation.setFillAfter(false); myImage.startAnimation(animation); } private void AlphAnim(){ //构造方法的两个参数是指,开始时候的透明度和结束时候的透明度 animation = new AlphaAnimation(1.0f, 0.7f); animation.setInterpolator(new AccelerateDecelerateInterpolator()); animation.setDuration(2000); animation.setFillAfter(false); myImage.startAnimation(animation); } private void ScaleAnim(){ /*float fromX 动画起始时 X坐标上的伸缩尺寸 float toX 动画结束时 X坐标上的伸缩尺寸 float fromY 动画起始时Y坐标上的伸缩尺寸 float toY 动画结束时Y坐标上的伸缩尺寸 int pivotXType 动画在X轴相对于物件位置类型 float pivotXValue 动画相对于物件的X坐标的开始位置 int pivotYType 动画在Y轴相对于物件位置类型 float pivotYValue 动画相对于物件的Y坐标的开始位置 */ animation = new ScaleAnimation(1.0f, 0f, 1.0f, 0f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setInterpolator(new AnticipateInterpolator()); animation.setDuration(2000); animation.setFillAfter(false); myImage.startAnimation(animation); } /** * 动画集合 */ private void AllAnim(){ // 是否多个animation使用同一个动画插入器 AnimationSet animationSet = new AnimationSet(false); ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f); scaleAnimation.setDuration(2000); animationSet.addAnimation(scaleAnimation); AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f); alphaAnimation.setDuration(2000); animationSet.addAnimation(alphaAnimation); TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 200); alphaAnimation.setDuration(2000); animationSet.addAnimation(translateAnimation); RotateAnimation rotateAnimation = new RotateAnimation(0, 30, myImage.getWidth(), myImage.getHeight()); animationSet.addAnimation(rotateAnimation); myImage.startAnimation(animationSet); } @Override public void onClick(View view) { switch ((Integer)view.getTag()){ case ANIM_TRANSLATE: TrandlateAnim(); break; case ANIM_ALPH: AlphAnim(); break; case ANIM_ROATE: RoateAnim(); break; case ANIM_SCALE: ScaleAnim(); break; case ANIM_ALL: AllAnim(); break; } }}
(五)XML方式实现补间动画
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ //点击事件 private final int ANIM_TRANSLATE = 0,ANIM_ROATE = 1,ANIM_SCALE = 2,ANIM_ALPH = 3,ANIM_ALL = 4; private Button button_translate,button_roate,button_scale,button_alpha,button_all; private ImageView myImage; private Animation animation; /** * 初始化按钮 */ //private Animation animation; private void initView(){ button_translate = (Button) findViewById(R.id.button_translate); button_roate = (Button) findViewById(R.id.button_roate); button_scale = (Button) findViewById(R.id.button_scale); button_alpha = (Button) findViewById(R.id.button_alpha); button_all = (Button) findViewById(R.id.button_all); button_translate.setTag(ANIM_TRANSLATE); button_roate.setTag(ANIM_ROATE); button_scale.setTag(ANIM_SCALE); button_alpha.setTag(ANIM_ALPH); button_all.setTag(ANIM_ALL); myImage = (ImageView) findViewById(R.id.myImage); } /** * 点击事件 */ private void initCtrl(){ button_translate.setOnClickListener(this); button_alpha.setOnClickListener(this); button_roate.setOnClickListener(this); button_scale.setOnClickListener(this); button_all.setOnClickListener(this); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initCtrl(); } /** * 移动 */ private void TrandlateAnim(){ animation = AnimationUtils.loadAnimation(this, R.anim.translate); myImage.startAnimation(animation); } /** * 透明度 */ private void AlphAnim(){ animation = AnimationUtils.loadAnimation(this, R.anim.alph); myImage.startAnimation(animation); } /** * 旋转 */ private void RoateAnim(){ animation = AnimationUtils.loadAnimation(this, R.anim.rotate); myImage.startAnimation(animation); } /** * 缩放 */ private void ScaleAnim(){ animation = AnimationUtils.loadAnimation(this, R.anim.scale); myImage.startAnimation(animation); } /** * 组合动画 */ private void AllAnim(){ animation = AnimationUtils.loadAnimation(this, R.anim.set); myImage.startAnimation(animation); } @Override public void onClick(View view) { switch ((Integer)view.getTag()){ case ANIM_TRANSLATE: TrandlateAnim(); break; case ANIM_ALPH: AlphAnim(); break; case ANIM_ROATE: RoateAnim(); break; case ANIM_SCALE: ScaleAnim(); break; case ANIM_ALL: AllAnim(); break; } }}
移动
缩放
旋转
透明度
组合动画
附上源码: 源码
更多相关文章
- Android(安卓)ImageView ScaleTypes介绍
- 分享:android图片浏览器—类微信朋友圈相片浏览【android代码下载
- android SystemUI显示问题。
- Android(安卓)关于 Activity 之间的切换动画
- Android动画之三:Property Animation(上)
- Android(安卓)Metro菜单
- Android基本的动画& animation工具类
- android图形图像函数汇集
- android 划屏动画