Android自定義View實(shí)現(xiàn)動(dòng)畫效果詳解
幀動(dòng)畫
幀動(dòng)畫就是給定一個(gè)完整動(dòng)畫的所有關(guān)鍵幀,由大腦想象中間的變化過程的一種動(dòng)畫。
<?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>
將這個(gè)xml文件放到drawable文件夾下,就可以引用上了。
補(bǔ)間動(dòng)畫
補(bǔ)間動(dòng)畫就是指定動(dòng)畫開始和結(jié)束的狀態(tài),中間的變化,由計(jì)算機(jī)自動(dòng)補(bǔ)齊。補(bǔ)間動(dòng)畫有4種類型,平移動(dòng)畫(Translate)、透明度動(dòng)畫(Alpha)、旋轉(zhuǎn)動(dòng)畫(Rotate)、縮放動(dòng)畫(Scale)。
旋轉(zhuǎn)動(dòng)畫
<?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>
透明度動(dòng)畫
<?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>
平移動(dòng)畫
<?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>
縮放動(dòng)畫
<?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>
屬性動(dòng)畫
屬性動(dòng)畫,就是監(jiān)聽一個(gè)屬性的變化值,來完成的動(dòng)畫。簡言之,就是不斷修改該對象的某個(gè)屬性值,然后讓動(dòng)畫框架來監(jiān)聽它。
ValueAnimator
ValueAnimator.ofFloat(0f, 1f)
創(chuàng)建一個(gè)從0逐漸變化到1點(diǎn)動(dòng)畫。
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)聽動(dòng)畫的狀態(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); }
使用估值器來計(jì)算兩個(gè)邊緣狀態(tài)的所有中間值。
ObjectAnimator.ofObject(target, propertyName, evaluator, values);
target參數(shù)為監(jiān)聽的對象,propertyName為監(jiān)聽的該對象的某個(gè)屬性的名字,也可以監(jiān)聽該屬性的setXxx()方法。evaluator傳入一個(gè)估值器,values為你要估值的所有關(guān)鍵的點(diǎn)的值,然后通過這些給定的點(diǎn),來估計(jì)一定時(shí)間內(nèi)這個(gè)點(diǎn)在中間時(shí)刻的值。
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. }
用它的實(shí)現(xiàn)類來決定動(dòng)畫的變化速率,比如先快后慢、先慢后快、勻速等。比較常用的有LinearInterpolator(勻速)、AccelerateInterpolator(加速)、DecelerateInterpolator(減速)。ValueAnimator和ObjectAnimator都可以通過調(diào)用
public abstract void setInterpolator(TimeInterpolator value);
設(shè)置插值器,因?yàn)檫@個(gè)方法是Animator抽象類的。
到此這篇關(guān)于Android自定義View實(shí)現(xiàn)動(dòng)畫效果詳解的文章就介紹到這了,更多相關(guān)Android自定義View內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Sql Server2008遠(yuǎn)程過程調(diào)用失敗的解決方法
這篇文章主要為大家詳細(xì)介紹了Sql Server2008遠(yuǎn)程過程調(diào)用失敗的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android程序開發(fā)之自定義設(shè)置TabHost,TabWidget樣式
這篇文章主要介紹了Android程序開發(fā)之自定義設(shè)置TabHost,TabWidget樣式的相關(guān)資料,需要的朋友可以參考下2016-03-03Flutter實(shí)現(xiàn)可以縮放拖拽的圖片示例代碼
這篇文章主要給大家介紹了關(guān)于利用Flutter實(shí)現(xiàn)可以縮放拖拽的圖片的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06詳解Android——藍(lán)牙技術(shù) 帶你實(shí)現(xiàn)終端間數(shù)據(jù)傳輸
藍(lán)牙技術(shù)在智能硬件方面有很多用武之地,本篇文章主要介紹了Android——藍(lán)牙技術(shù),實(shí)現(xiàn)兩個(gè)終端間數(shù)據(jù)的傳輸,有興趣的朋友可以了解一下。2016-12-12Android Studio使用教程(四):Gradle基礎(chǔ)
這篇文章主要介紹了Android Studio使用教程(四):Gradle基礎(chǔ),本文講解了什么是Gradle、安裝Gradle、Gradle 基本概念等內(nèi)容,需要的朋友可以參考下2015-05-05Android開發(fā)使用URLConnection進(jìn)行網(wǎng)絡(luò)編程詳解
這篇文章主要介紹了Android開發(fā)使用URLConnection進(jìn)行網(wǎng)絡(luò)編程,結(jié)合實(shí)例形式分析了Android URLConnection對象創(chuàng)建、屬性、方法及相關(guān)使用技巧,需要的朋友可以參考下2018-01-01android二級(jí)listview列表實(shí)現(xiàn)代碼
今天來實(shí)現(xiàn)以下大眾點(diǎn)評(píng)客戶端的橫向listview二級(jí)列表,感興趣的朋友可以研究下2013-01-01Android使用fragment實(shí)現(xiàn)左側(cè)導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了Android使用fragment實(shí)現(xiàn)左側(cè)導(dǎo)航,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02