Android 動畫大概能夠分爲兩類,一類是 Tween 動畫(補間動畫),即經過對場景裏的對象不斷作圖像變換(平移、縮放、旋轉)產生動畫效果;第二類是 Frame 動畫,即順序播放設置好的每個圖像(逐幀動畫)。php
Animation表明抽象的動畫類,它的實現類有以下幾個:AlphaAnimation、ScaleAnimation、RotateAnimation、TranslateAnimation、AnimationSet。java
動畫類型 | 標籤 | java實現方式 |
---|---|---|
漸變透明度動畫 | alpha | AlphaAnimation |
漸變尺寸伸縮動畫 | scale | ScaleAnimation |
旋轉動畫 | rotate | RotateAnimation |
位移變化動畫 | transtate | TranslateAnimation |
漸變透明度動畫 | set | AnimationSet |
屬性 | 說明 |
---|---|
fromAlpha | 開始透明度(從0.0 --1.0 ,0.0表示全透明,1.0表示徹底不透明) |
toAlpha | 結束透明度(從0.0 --1.0 ,0.0表示全透明,1.0表示徹底不透明) |
屬性 | 說明 |
---|---|
fromXScale | 起始的X方向上相對自身的縮放比例,浮點值,好比1.0表明自身無變化,0.5表明起始時縮小一倍,2.0表明放大一倍 |
toXScale | 結尾的X方向上相對自身的縮放比例,浮點值 |
fromYScale | 起始的Y方向上相對自身的縮放比例,浮點值 |
toYScale | 結尾的Y方向上相對自身的縮放比例,浮點值 |
pivotX | 縮放起點X軸座標,能夠是數值、百分數、百分數p 三種樣式,好比 50、50%、50%p,當爲數值時,表示在當前View的左上角,即原點處加上50px,作爲起始縮放點;若是是50%,表示在當前控件的左上角加上本身寬度的50%作爲起始點;若是是50%p,那麼就是表示在當前的左上角加上父控件寬度的50%作爲起始點x軸座標 |
pivotY | 縮放起點Y軸座標(同pivotX) |
屬性 | 說明 |
---|---|
fromDegrees | 開始旋轉的角度位置,正值表明順時針方向度數,負值代碼逆時針方向度數 |
toDegrees | 結束時旋轉到的角度位置,正值表明順時針方向度數,負值代碼逆時針方向度數 |
pivotX | 控件的左上角即爲控件的座標原點,這裏的起始點是在控件的原點的基礎上向X軸加上指定的像素,作爲起始點(可設置具體值也可設置百分比) |
pivotY | 控件的左上角即爲控件的座標原點,這裏的起始點是在控件的原點的基礎上向Y軸加上指定的像素,作爲起始點(可設置具體值也可設置百分比) |
屬性 | 說明 |
---|---|
fromXDelta | 起始點X軸座標,能夠是數值、百分數、百分數p 三種樣式,好比 50、50%、50%p(同pivotX) |
fromYDelta | 起始點Y軸從標,能夠是數值、百分數、百分數p 三種樣式(同pivotY) |
toXDelta | 結束點X軸座標 |
toYDelta | 結束點Y軸座標 |
屬性 | 說明 |
---|---|
duration | 動畫持續時間 |
fillAfter | 動畫結束後是否保掛當前透明度 |
fillBefore | 還原到開始動畫前的狀態 |
repeatCount | 重複次數 |
repeatMode | 重複類型,有reverse和restart兩個值,reverse表示倒序回放,restart表示從新放一遍,必須與repeatCount一塊兒使用才能看到效果 |
在res文件夾下,新建一個anim文件夾,而後再新建一個.xml動畫文件,以下:android
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fillAfter="true">
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"/>
<scale android:fromXScale="0.0" android:toXScale="1.4" android:fromYScale="0.0" android:toYScale="1.4" android:pivotX="50%" android:pivotY="50%"/>
<rotate android:fromDegrees="0" android:toDegrees="720" android:pivotX="50%" android:pivotY="50%"/>
</set>
複製代碼
代碼使用動畫動畫
Animation scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.動畫文件名);從XML文件中獲取動畫
控件.startAnimation(scaleAnimation);
複製代碼
逐幀動畫也就是依次顯示靜態圖片,就是播放電影同樣,一般咱們使用xml的形式去定義,固然也可使用代碼控制。this
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">//oneshot:是否只播放一次
<item
android:drawable="@mipmap/lottery_1"
android:duration="200"/>//duration:這一幀持續的時間
<item
android:drawable="@mipmap/lottery_2"
android:duration="200"/>
<item
android:drawable="@mipmap/lottery_3"
android:duration="200"/>
</animation-list>
複製代碼
xml中直接使用spa
<ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:src="@drawable/frame" />
複製代碼
代碼控制rest
imageView = (ImageView)findViewById(R.id.iv);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
animationDrawable.start();
animationDrawable.stop();
複製代碼
在代碼中定義動畫資源code
AnimationDrawable anim = new AnimationDrawable();
for (int i = 1; i <= 6; i++) {
int id = getResources().getIdentifier("lottery_" + i, "mipmap", getPackageName());
Drawable drawable = getResources().getDrawable(id);
anim.addFrame(drawable, 200);//添加幀
}
anim.setOneShot(false);//設置爲循環播放
imageView.setImageDrawable(anim);
anim.start();
複製代碼