Android动画学习笔记之补间动画

这篇文章主要为大家详细介绍了Android动画学习笔记之补间动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android补间动画展示的具体代码,供大家参考,具体内容如下

首先看看补间动画的共同属性:

Duration:动画持续的时间(单位:毫秒)  
fillAfter:设置为true,动画转化在动画被结束后被应用 
fillBefore:设置为true,动画转化在动画开始前被应用 
interpolator:动画插入器(加速、减速插入器) 
repeatCount:动画重复的次数 
repeatMode:顺序动画(restart)/倒序动画(reverse) 
startOffset:动画之间时间间隔 

对于动画的创建一般有两种方式:

第一种是在res/新建一个anim文件夹,然后在其下面分别建立四种动画 
第二种方式是通过java代码的方式创建 

在补间动画中我们通常有以下四种动画:

位移动画

创建方式一:

在anim文件下新建一个translate资源文件

    //通过以下代码注册该动画 private void creatTranslateByInflate(){ Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate); mCircle.startAnimation(animation); }

创建方式二:(代码创建)

 private void creatTranslateByCode() { TranslateAnimation animation = new TranslateAnimation(0, 100, 0, 0); animation.setDuration(2000); animation.setRepeatMode(Animation.REVERSE); animation.setRepeatCount(2); mCircle.startAnimation(animation); }

旋转动画

创建方式一:

   终止的角度) //这里就是顺时针的旋转180 android:pivotX="50%" //旋转中轴的x点,50%表示以控件宽为基准,在控件的中间x点 android:pivotY="50%" //旋转中轴的y点,50%表示以控件高为基准,在控件的中间y点 android:duration="2000" android:repeatMode="reverse" android:repeatCount="2" >  //通过以下代码注册该动画 private void createRotateByInflate(){ Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate); mCircle.startAnimation(animation); }

创建方式二:(代码创建)

 private void createRotateByCode() { float pivotX = mCircle.getWidth() / 2.0f; float pivotY = mCircle.getHeight() / 2.0f; RotateAnimation animation = new RotateAnimation(0, 180, pivotX, pivotY); animation.setDuration(2000); animation.setRepeatMode(Animation.REVERSE); animation.setRepeatCount(2); mCircle.startAnimation(animation); }

缩放动画

创建方式一:

    //通过以下代码注册该动画 private void createScaleByInflate(){ Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale); mCircle.startAnimation(animation); }

创建方式二:(代码创建)

 private void createScaleByCode() { //创建动画Animation.RELATIVE_TO_PARENT 以父容器为参照物 //Animation.RELATIVE_TO_SELF 以自己为参照物, 如果以父容器为参照物会导致控件移动 float pivotX = mCircle.getWidth() / 2.0f; float pivotY = mCircle.getHeight() / 2.0f; ScaleAnimation animation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f, pivotX,pivotY); animation.setDuration(2000); animation.setRepeatMode(Animation.REVERSE); animation.setRepeatCount(2); mCircle.startAnimation(animation); }

渐变动画

创建方式一:

    //通过以下代码注册该动画 private void createAlphaByInflate(){ Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha); mCircle.startAnimation(animation); }

创建方式二:(代码创建)

 private void createAlphaByCode() { AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f); animation.setDuration(2000); animation.setRepeatMode(Animation.REVERSE); animation.setRepeatCount(2); mCircle.startAnimation(animation); }

以上的四种可以单独使用也可以结合起来使用,如果要结合起来使用的话,直接在anim文件夹下创建set集合,然后将需要结合的动画

放在一起即可

如下示例:

   

基本的创建方式,以及基本属性都在这里了,至于如何实现一个具有美感的效果图,那就看个人的设计感了。

最后看看运行结果图

以上就是Android动画学习笔记之补间动画的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动