Android自定義View實現(xiàn)動畫效果詳解
幀動畫
幀動畫就是給定一個完整動畫的所有關(guān)鍵幀,由大腦想象中間的變化過程的一種動畫。
<?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/ic_loading_0" android:duration="100"/> <item android:drawable="@drawable/ic_loading_1" android:duration="100"/> <item android:drawable="@drawable/ic_loading_2" android:duration="100"/> <item android:drawable="@drawable/ic_loading_3" android:duration="100"/> <item android:drawable="@drawable/ic_loading_4" android:duration="100"/> <item android:drawable="@drawable/ic_loading_5" android:duration="100"/> <item android:drawable="@drawable/ic_loading_6" android:duration="100"/> <item android:drawable="@drawable/ic_loading_7" android:duration="100"/> </animation-list>
將這個xml文件放到drawable文件夾下,就可以引用上了。
補間動畫
補間動畫就是指定動畫開始和結(jié)束的狀態(tài),中間的變化,由計算機自動補齊。補間動畫有4種類型,平移動畫(Translate)、透明度動畫(Alpha)、旋轉(zhuǎn)動畫(Rotate)、縮放動畫(Scale)。
旋轉(zhuǎn)動畫
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fillAfter="true" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:toDegrees="180"> </rotate>
透明度動畫
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromAlpha="1.0" android:toAlpha="0.5"> </alpha>
平移動畫
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="100%" android:toYDelta="100%"> </translate>
縮放動畫
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromXScale="1.0" android:fromYScale="1.0" android:toXScale="1.2" android:toYScale="1.2" android:pivotX="50%" android:pivotY="50%"> </scale>
屬性動畫
屬性動畫,就是監(jiān)聽一個屬性的變化值,來完成的動畫。簡言之,就是不斷修改該對象的某個屬性值,然后讓動畫框架來監(jiān)聽它。
ValueAnimator
ValueAnimator.ofFloat(0f, 1f)
創(chuàng)建一個從0逐漸變化到1點動畫。
ObjectAnimator
new ObjectAnimator().addListener(new AnimatorListenerAdapter() { @Override public void onAnimationCancel(Animator animation) { super.onAnimationCancel(animation); } @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); } @Override public void onAnimationRepeat(Animator animation) { super.onAnimationRepeat(animation); } @Override public void onAnimationStart(Animator animation) { super.onAnimationStart(animation); } @Override public void onAnimationPause(Animator animation) { super.onAnimationPause(animation); } @Override public void onAnimationResume(Animator animation) { super.onAnimationResume(animation); } });
監(jiān)聽動畫的狀態(tài)。
TypeEvaluator估值器
public interface TypeEvaluator<T> { /** * This function returns the result of linearly interpolating the start and end values, with * <code>fraction</code> representing the proportion between the start and end values. The * calculation is a simple parametric calculation: <code>result = x0 + t * (x1 - x0)</code>, * where <code>x0</code> is <code>startValue</code>, <code>x1</code> is <code>endValue</code>, * and <code>t</code> is <code>fraction</code>. * * @param fraction The fraction from the starting to the ending values * @param startValue The start value. * @param endValue The end value. * @return A linear interpolation between the start and end values, given the * <code>fraction</code> parameter. */ public T evaluate(float fraction, T startValue, T endValue); }
使用估值器來計算兩個邊緣狀態(tài)的所有中間值。
ObjectAnimator.ofObject(target, propertyName, evaluator, values);
target參數(shù)為監(jiān)聽的對象,propertyName為監(jiān)聽的該對象的某個屬性的名字,也可以監(jiān)聽該屬性的setXxx()方法。evaluator傳入一個估值器,values為你要估值的所有關(guān)鍵的點的值,然后通過這些給定的點,來估計一定時間內(nèi)這個點在中間時刻的值。
Interpolator插值器
public interface Interpolator extends TimeInterpolator { // A new interface, TimeInterpolator, was introduced for the new android.animation // package. This older Interpolator interface extends TimeInterpolator so that users of // the new Animator-based animations can use either the old Interpolator implementations or // new classes that implement TimeInterpolator directly. }
用它的實現(xiàn)類來決定動畫的變化速率,比如先快后慢、先慢后快、勻速等。比較常用的有LinearInterpolator(勻速)、AccelerateInterpolator(加速)、DecelerateInterpolator(減速)。ValueAnimator和ObjectAnimator都可以通過調(diào)用
public abstract void setInterpolator(TimeInterpolator value);
設(shè)置插值器,因為這個方法是Animator抽象類的。
到此這篇關(guān)于Android自定義View實現(xiàn)動畫效果詳解的文章就介紹到這了,更多相關(guān)Android自定義View內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Sql Server2008遠程過程調(diào)用失敗的解決方法
這篇文章主要為大家詳細介紹了Sql Server2008遠程過程調(diào)用失敗的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07Android程序開發(fā)之自定義設(shè)置TabHost,TabWidget樣式
這篇文章主要介紹了Android程序開發(fā)之自定義設(shè)置TabHost,TabWidget樣式的相關(guān)資料,需要的朋友可以參考下2016-03-03詳解Android——藍牙技術(shù) 帶你實現(xiàn)終端間數(shù)據(jù)傳輸
藍牙技術(shù)在智能硬件方面有很多用武之地,本篇文章主要介紹了Android——藍牙技術(shù),實現(xiàn)兩個終端間數(shù)據(jù)的傳輸,有興趣的朋友可以了解一下。2016-12-12Android Studio使用教程(四):Gradle基礎(chǔ)
這篇文章主要介紹了Android Studio使用教程(四):Gradle基礎(chǔ),本文講解了什么是Gradle、安裝Gradle、Gradle 基本概念等內(nèi)容,需要的朋友可以參考下2015-05-05Android開發(fā)使用URLConnection進行網(wǎng)絡(luò)編程詳解
這篇文章主要介紹了Android開發(fā)使用URLConnection進行網(wǎng)絡(luò)編程,結(jié)合實例形式分析了Android URLConnection對象創(chuàng)建、屬性、方法及相關(guān)使用技巧,需要的朋友可以參考下2018-01-01Android使用fragment實現(xiàn)左側(cè)導(dǎo)航
這篇文章主要為大家詳細介紹了Android使用fragment實現(xiàn)左側(cè)導(dǎo)航,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02