Android中的动画有哪几类?各自的特点和区别是什么?
生活随笔
收集整理的這篇文章主要介紹了
Android中的动画有哪几类?各自的特点和区别是什么?
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在 android.view.animation包中有四種基本的動畫 ,透明/伸縮/移動/旋轉。 動畫類型
Android的animation由四種類型組成
XML中
| alpha | 漸變透明度動畫效果 |
| scale | 漸變尺寸伸縮動畫效果 |
| translate | 畫面轉換位置移動動畫效果 |
| rotate | 畫面轉移旋轉動畫效果 |
| AlphaAnimation | 漸變透明度動畫效果 |
| ScaleAnimation | 漸變尺寸伸縮動畫效果 |
| TranslateAnimation | 畫面轉換位置移動動畫效果 |
| RotateAnimation | 畫面轉移旋轉動畫效果 |
Animation主要有兩種動畫模式:
一種是tweened animation(漸變動畫)
| XML中 | JavaCode |
| alpha | AlphaAnimation |
| scale | ScaleAnimation |
| XML中 | JavaCode |
| translate | TranslateAnimation |
| rotate | RotateAnimation |
Frame動畫,傳統(tǒng)的動畫方法,通過順序的播放排列好的圖片來實現(xiàn),類似電影。 在項目過程中,在xml中設置了播放的圖片和間隔如下: <?xml version="1.0" encoding="utf-8"?>
<animation-list
? ? ? ? xmlns:android="http://schemas.android.com/apk/res/android"
? ? ? ? android:oneshot="false">
? ? <item android:drawable="@drawable/wait_1" android:duration="80" />
? ? <item android:drawable="@drawable/wait_2" android:duration="80" />
? ? <item android:drawable="@drawable/wait_3" android:duration="80" />
? ? <item android:drawable="@drawable/wait_4" android:duration="80" />
? ? <item android:drawable="@drawable/wait_5" android:duration="80" />
? ? <item android:drawable="@drawable/wait_6" android:duration="80" />
? ? <item android:drawable="@drawable/wait_7" android:duration="80" />
? ? <item android:drawable="@drawable/wait_8" android:duration="80" />
? ? <item android:drawable="@drawable/wait_9" android:duration="80" />
? ? <item android:drawable="@drawable/wait_10" android:duration="80" />
? ? <item android:drawable="@drawable/wait_11" android:duration="80" />
? ? <item android:drawable="@drawable/wait_12" android:duration="80" />
</animation-list>
類似于一個等待的過程,不斷地加載圖片,達到動畫的效果。
Android 中的動畫有幀動畫,補間動畫,屬性動畫,她們的忑點和使用方法如下:
###幀動畫> 一張張圖片不斷的切換,形成動畫效果
* 在drawable目錄下定義xml文件,子節(jié)點為animation-list,在這里定義要顯示的圖片和每張圖片的顯示時長? ??
? ?? ???<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
? ?? ?? ?? ?<item android:drawable="@drawable/g1" android:duration="200" />
? ?? ?? ?? ?<item android:drawable="@drawable/g2" android:duration="200" />
? ?? ?? ?? ?<item android:drawable="@drawable/g3" android:duration="200" />
? ?? ???</animation-list>
* 在屏幕上播放幀動畫
? ?? ???ImageView iv = (ImageView) findViewById(R.id.iv);
? ?? ???//把動畫文件設置為imageView的背景
? ?? ???iv.setBackgroundResource(R.drawable.animations);
? ?? ???AnimationDrawable ad = (AnimationDrawable) iv.getBackground();
? ?? ???//播放動畫? ?? ???
? ?? ???ad.start();
###補間動畫
* 原形態(tài)變成新形態(tài)時為了過渡變形過程,生成的動畫就叫補間動畫
* 位移、旋轉、縮放、透明
#####位移:
* 參數(shù)10指的是X的起點坐標,但不是指屏幕x坐標為10的位置,而是imageview的 真實X + 10
* 參數(shù)150指的是X的終點坐標,它的值是imageview的 真實X + 150? ??
? ?? ???//創(chuàng)建為位移動畫對象,設置動畫的初始位置和結束位置
? ?? ???TranslateAnimation ta = new TranslateAnimation(10, 150, 20, 140);
* x坐標的起點位置,如果相對于自己,傳0.5f,那么起點坐標就是 真實X + 0.5 * iv寬度
* x坐標的終點位置,如果傳入2,那么終點坐標就是 真實X + 2 * iv的寬度
* y坐標的起點位置,如果傳入0.5f,那么起點坐標就是 真實Y + 0.5 * iv高度
* y坐標的終點位置,如果傳入2,那么終點坐標就是 真實Y + 2 * iv高度
? ?? ???TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2);
* 動畫播放相關的設置
? ?? ???//設置動畫持續(xù)時間
? ?? ???ta.setDuration(2000);
? ?? ???//動畫重復播放的次數(shù)
? ?? ???ta.setRepeatCount(1);
? ?? ???//動畫重復播放的模式
? ?? ???ta.setRepeatMode(Animation.REVERSE);
? ?? ???//動畫播放完畢后,組件停留在動畫結束的位置上
? ?? ???ta.setFillAfter(true);
? ?? ???//播放動畫
? ?? ???iv.startAnimation(ta);
#####縮放:
* 參數(shù)0.1f表示動畫的起始寬度是真實寬度的0.1倍
* 參數(shù)4表示動畫的結束寬度是真實寬度的4倍
* 縮放的中心點在iv左上角
? ?? ???ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4);
* 參數(shù)0.1f和4意義與上面相同
* 改變縮放的中心點:傳入的兩個0.5f,類型都是相對于自己,這兩個參數(shù)改變了縮放的中心點
* 中心點x坐標 = 真實X + 0.5 * iv寬度
* 中心點Y坐標 = 真實Y + 0.5 * iv高度
? ? ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
#####透明:
* 0為完全透明,1為完全不透明
? ?? ???AlphaAnimation aa = new AlphaAnimation(0, 0.5f);
#####旋轉:
* 20表示動畫開始時的iv的角度
* 360表示動畫結束時iv的角度
* 默認旋轉的圓心在iv左上角
? ?? ???RotateAnimation ra = new RotateAnimation(20, 360);
* 20,360的意義和上面一樣
* 指定圓心坐標,相對于自己,值傳入0.5,那么圓心的x坐標:真實X + iv寬度 * 0.5
* 圓心的Y坐標:真實Y + iv高度 * 0.5
? ?? ???RotateAnimation ra = new RotateAnimation(20, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
---
#屬性動畫
* 補間動畫,只是一個動畫效果,組件其實還在原來的位置上,xy沒有改變
###位移:
* 第一個參數(shù)target指定要顯示動畫的組件
* 第二個參數(shù)propertyName指定要改變組件的哪個屬性
* 第三個參數(shù)values是可變參數(shù),就是賦予屬性的新的值
* 傳入0,代表x起始坐標:當前x + 0
* 傳入100,代表x終點坐標:當前x + 100
? ?? ???//具有get、set方法的成員變量就稱為屬性
? ?? ???ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 100) ;
###縮放:
* 第三個參數(shù)指定縮放的比例
* 0.1是從原本高度的十分之一開始
* 2是到原本高度的2倍結束
? ?? ???ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "scaleY", 0.1f, 2);
###透明:
* 透明度,0是完全透明,1是完全不透明
? ?? ???
? ?? ???ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "alpha", 0.1f, 1);
###旋轉
* rotation指定是順時針旋轉
* 20是起始角度
* 270是結束角度
? ?? ???ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotation", 20, 270);
* 屬性指定為rotationX是豎直翻轉
* 屬性指定為rotationY是水平翻轉
? ?? ???ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotationY", 20, 180);
###可變參數(shù)
* 第三個參數(shù)可變參數(shù)可以傳入多個參數(shù),可以實現(xiàn)往回位移(旋轉、縮放、透明)
? ?? ???ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 70, 30, 100) ; 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結
以上是生活随笔為你收集整理的Android中的动画有哪几类?各自的特点和区别是什么?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BDS和GPS、电离层相关SSR数据解码
- 下一篇: android sina oauth2.