視圖動畫和幀動畫

轉載請註明出處:http://blog.csdn.net/ZhouLi_CSDN/article/details/45971421

View Animaiton

  • 通常使用xml文件定義動畫,這樣易讀,重用。
  • xml寫在res/anim/路徑下。
    下面是例子:
<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>
</set>

pivotX:“50”for 50%相對於父視圖,“50%”for 50%相對於自己。 使用:

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);

設置動畫啓動時間:

Animation.setStartTime(), 然後View.setAnimation().
注意:不管動畫怎麼移動,大小如何變化,擁有動畫的視圖不會調整自己容納它,視圖會超過他的範圍,並且不會被裁減。當超過了它的父view的時候纔會發生裁減。

Drawable Animation

類:AnimationDrawable
如果視同xml定義放在res/drawable/路徑下:
舉例:

<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>
  • oneShot:設置爲true,只播放一次,停止在最後一幀。
  • 設置爲false,循環播放。
  • 使用:
    它可以被添加爲一個view的background,然後調用啓動動畫。
  • 舉例:
AnimationDrawable rocketAnimation;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
  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方法中使用start()方法,因爲AnimationDrawable還沒有被關聯到window。如果你想讓動畫啓動後就生效,可以在activity的onWindowFocusChanged()方法中start(),當android將你的window設置爲focus時會調用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章