温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android_之动画1

发布时间:2020-07-11 14:36:16 阅读:636 作者:chuxin2009 栏目:移动开发

    近一周以来一直在搞android动画,被android动画搞的焦头烂额,不过最艰难的时间已经过去,现在写一篇博客,来给自己做一个梳理和总结。

    Android 3.0以后新增了属性动(property animation),至此Android包含三种动画类型,即补间动画(Tween Animation),逐帧动画(Drawable animation),属性动画(Property animation),今天这篇博客着重对属性动画做一个详细阐述。

  1.      补间动画(Tween Animation)

    补间动画类似于之前学过的Flash动画,设定好初始帧和结束帧以后,中间的值由系统自动进行插值,然后可以形成一种动画效果,补间动画需要用户设定好初始值和结束值,系统自动计算之间值。对于这种动画来说,建议用户使用XML文件配置动画,anim.xml文件应存放于res/anim文件夹下,xml文件必须以<alpha><scale><translate><rotate><set>作为根元素。官方文件中提供的xml示例代码:

<set android:shareInterpolator="false">    <scale        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:fromXScale="1.0"        android:toXScale="1.4"        android:fromYScale="1.0"        android:toYScale="0.6"        android:pivotX="50%"        android:pivotY="50%"        android:fillAfter="false"        android:duration="700" />    <set android:interpolator="@android:anim/decelerate_interpolator">        <scale           android:fromXScale="1.4"           android:toXScale="0.0"           android:fromYScale="0.6"           android:toYScale="0.0"           android:pivotX="50%"           android:pivotY="50%"           android:startOffset="700"           android:duration="400"           android:fillBefore="false" />        <rotate           android:fromDegrees="0"           android:toDegrees="-45"           android:toYScale="0.0"           android:pivotX="50%"           android:pivotY="50%"           android:startOffset="700"           android:duration="400" />    </set>

让动画跑起来也是十分简单的,定义好动画xml文件以后,使用如下代码使用对象和动画的绑定

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, 动画文件);spaceshipImage.startAnimation(hyperspaceJumpAnimation);

  补间动画只是启动空间显示的位置,但是没有移动或者改变空间在布局当中实际文字,例如一个Button在空间中的位置(100,100),X轴方向移动n个单位以后,Button实际位置依然在(100,100)。 

2. 逐帧动画(Frame animation)

    逐帧动画类似于电影播放的原理,一个图片资源代表一帧,以一定时间间隔进行切换图片资源,形成一种动画效果。一般情况下,逐帧动画也是通过xml文件定义的,下面是官方提供的一个示例代码:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="true">    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /></animation-list>

其中有一个属性是非常重要的,android:oneshot="true",若为true,则代表动画完成以后,停留在最后一帧,反之停留在第一帧。

让动画开始也是非常简单的,看官方代码:

AnimationDrawable rocketAnimation;public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  ImageView rocketImage = (ImageView) findViewById(R.id.rocket_p_w_picpath);  rocketImage.setBackgroundResource(R.drawable.rocket_thrust);  rocketAnimation = (AnimationDrawable) rocketImage.getBackground();}public boolean onTouchEvent(MotionEvent event) {  if (event.getAction() == MotionEvent.ACTION_DOWN) {    rocketAnimation.start();    return true;  }  return super.onTouchEvent(event);}

有一点是非常重要的,不能在onCreate()函数中启动动画,因为在OnCreate函数中,AnimationDrawable还没有完全和window连接起来,所以如果你想一开始就加载动画,可以在 onWindowFocusChanged()  启动动画。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI