Android動畫機制

2021-08-28 02:47:05 字數 4164 閱讀 3241

幀動畫、補間動畫、屬性動畫、過度動畫

animation框架定義了透明度、旋轉、縮放、位移等幾種常見的動畫

實現原理:

通過矩陣運算完成幀動畫,如果動畫沒有完成,就繼續呼叫invalidate() 函式,啟動下次繪製來驅動動畫,從而完成整個動畫的繪製。

public class mainactivity extends activity 

}

public class mainactivity extends activity 

//位移動畫

public void translate(view v)

//旋轉動畫

public void rotate(view v)

//縮放動畫

public void scale(view v)

//透明動畫

public void alpha(view v)

//所有動畫一起飛

public void fly(view v)

}

使用objectanimator進行更精細化控制,只控制乙個物件的乙個屬性值

使多個objectanimator組合到animatorset形成乙個動畫

呼叫setframedelay()設定動畫幀之間的間隙時間,調整幀率,減少動畫繪製過程中頻繁繪製,在不影響動畫效果的情況下減少cpu資源消耗

1.objectanimator

內部是通過反射機制實現的,所以該屬性一定要具有 set和get方法

一些常用的可以直接使用的屬性:

public class mainactivity extends activity 

});}

public void translate(view v)

public void scale(view v)

public void alpha(view v)

public void rotate(view v)

public void fly(view v)

//使用xml檔案配置屬性動畫

public void xml(view v)

}

可以用xml配置屬性動畫只需要在res目錄下建立乙個property animator屬性動畫檔案

2. 如果屬性沒有set 和 get方法的解決方法

/**

* created at: 2016/8/5 14:21.

* by author: mwp

* 描述:使用屬性動畫時,給沒有set和get方法的屬性包裝工具

*/ private view mtarget;

this.mtarget = target;

}/**包裝寬度屬性*/

public int getwidth()

public void setwidth(int width)

//使用方法

public void use()

}

3. valueanimator(值動畫)

valueanimator animator = valueanimator.ofint(0,100);

animator.settarget(view);

animator.setduration(1000).start();

//最核心的方法

animator.addupdatelistener(new valueanimator.animatorupdatelistener()

});

4. 動畫監聽

監聽動畫的過程執行一些操作

objectanimator anim = objectanimator.offloat(view, "alpha",0.5f);

anim.addlistener(new animator.animatorlistener()

@override

public void onanimationend(animator animation)

@override

public void onanimationcancel(animator animation)

@override

public void onanimationrepeat(animator animation)

});

anim.addlistener(new animatorlisteneradapter()

});

5. view的animate方法

在3.0之後,google給view增加了animate方法來直接驅動屬性動畫

view.animate()

.alpha(0)

.scalex(1)

.x(300)

.y(200)

.setduration(1000)

.withstartaction(new runnable()

}).withendaction(new runnable()

});}

}).start();

6. android 布局動畫

android:animatelayoutchanges="true"

但是這個預設的動畫效果無法替換

linearlayout ll = (linearlayout)findviewbyid(r.id.ll);

//設定過渡動畫

scaleanimation sa = new scaleanimation(0,1,0,1);

sa.setduration(2000);

//設定布局動畫的顯示屬性,第二個引數是每個子view顯示的delay時間

layoutanimationcontroller lac = new layoutanimationcontroller(sa,0.5f);

lac.setorder(layoutanimationcontroller.order_normal);//順序,隨機,倒序

//為viewgroup設定布局動畫

ll.setlayoutanimation(lac);

7. interpolators(插值器 )

就是現有的透明度,旋轉,平移,縮放等行為組合起來仍然不能滿足你的話,可以自定義一些更炫的動畫

public class customanim extends animation 

// 暴露介面-設定旋轉角度

public void setrotatey(float rotatey)

@override

float interpolatedtime,

transformation t)

}

可伸縮向量圖形(scalable vector graphics)

定義用於網路的基於向量的圖形

使用xml格式定義圖形

影象在放大或改變尺寸的情況下其圖形質量不會有損失

與bitmap對比,svg最大的優點就是方法不失真,而且不需要為不同解析度設計多套圖示

public class droptest extends activity 

public void llclick(view view) else

}private void animateopen(final view view)

private void animateclose(final view view)

});animator.start();

}private valueanimator createdropanimator(

final view view, int start, int end)

});return animator;

}}

Android之動畫機制

特殊的動畫屬性 插值器估值器 注意事項 動畫區別 view動畫 作用物件是view,通過對場景裡的物件不停做圖形變換 從而產生動畫效果 幀動畫屬性動畫 動態改變物件的屬性從而達到動畫效果 1.layoutanimation 給viewgroup的子元素加上效果 2.activity切換效果 over...

Android動畫 View動畫

1 使用view,首先要建立xml檔案。res anim filename.xml 使用動畫 button button animation animation animationutils.loadanimation this,r.anim.filename button.startanimati...

Android動畫 幀動畫

首先在res中新建乙個drawable資料夾,將需要展示的放在裡面,同樣的還有展示的fight.xml檔案,如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...