標籤:android style blog http io ar color sp java
其實動畫這個東西我已經瞭解過很長一段時間了,但是一直沒系統的整理過。關於android中的各種動畫雖然都會用,但總怕自己會慢慢遺忘。這回看了幾篇動畫分析的文章,自己也學到了一些東西,在此就梳理一下。
注意:所有view的動畫就是被限制在它的父控制項中的,即使你做了view的移動,它也不可能顯示在父控制項的外邊。也就是說父控制項是一個舞台,演員可以在舞台上到處走動,但如果超過了舞台,那麼觀眾就看不到了。
一、View Animation(Tween Animation)
View Animation(Tween Animation):也可稱為補間動畫(Tween Animation),給出兩個主要畫面格,通過一些演算法將給定屬性值在給定的時間內在兩個主要畫面格間漸層。
View animation只能應用於View對象,而且只支援一部分屬性,如支援縮放旋轉而不支援背景顏色的改變。
對於View animation,它只是改變了View對象繪製的位置,注意這是“繪製”,而不是實際的位置。比如你讓一個button變成它的兩倍大小,但是它能接受你點擊的地區還是原來的button地區,沒有做任何改變。
View Animation支援設定多種動畫樣式,也可以設定這些動畫的執行順序,支援通過xml和代碼兩種方式來設定動畫效果。
動畫舉例
【 By XML 】
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true" android:startOffset="50"> <alpha android:duration="200" android:fromAlpha="1.0" android:toAlpha="0.0" /></set>
Animation aimation = AnimationUtils.loadAnimation(this, R.anim.anim); final ImageView imageView = (ImageView) findViewById(R.id.imageView_id); imageView.startAnimation(aimation);
可以對動畫添加監聽器
aimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { animation = null; } @Override public void onAnimationRepeat(Animation animation) { } });
如果你是用AnimationSet設定動畫的話,animationSet也是繼承自Animation,所以也有setAnimationListener的方法
詳細代碼可以參考:http://www.cnblogs.com/tianzhijiexian/p/3983616.html
【 By JAVA 】
通過java代碼來做的,可以參考這篇文章。其實也就是各種類繼承Animation,然後有自己獨特的方法,也可以通過AnimationSet進行各種設定。
http://www.cnblogs.com/tianzhijiexian/p/3981241.html
二、Drawable Animation(Frame Animation)
Drawable Animation(Frame Animation):幀動畫,就像GIF圖片,通過一系列Drawable依次顯示來類比動畫的效果。在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>
必須以<animation-list>為根項目,以<item>表示要輪換顯示的圖片,duration屬性工作表示各項顯示的時間。XML檔案要放在/res/drawable/目錄下。
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.main); imageView = (ImageView) findViewById(R.id.imageView1); imageView.setBackgroundResource(R.drawable.drawable_anim); AnimationDrawable anim = (AnimationDrawable) imageView.getBackground(); } public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { anim.stop(); anim.start(); return true; } return super.onTouchEvent(event); }
我在實驗中遇到兩點問題:
- 要在代碼中調用Imageview的setBackgroundResource方法,如果直接在XML布局檔案中設定其src屬性當觸發動畫時會FC。
- 在動畫start()之前要先stop(),不然在第一次動畫之後會停在最後一幀,這樣動畫就只會觸發一次。
- 最後一點是SDK中提到的,不要在onCreate中調用start,因為AnimationDrawable還沒有完全跟Window相關聯,如果想要介面顯示時就開始動畫的話,可以在onWindowFoucsChanged()中調用start()。
三、Property Animation
Android動畫學習筆記